Problem 657
Number of Ways to Stay in the Same Place
Hard · 30 pts · Dynamic Programming
Problem
A pointer starts at index 0 of an array of length ARRLEN. In each of exactly STEPS steps it moves one position left, one position right, or stays in place, and it must never leave the array. Count the ways for the pointer to be back at index 0 after all the steps, modulo 1000000007. The pointer can never get further than STEPS / 2 from the start and still return, so only the first min(ARRLEN, STEPS / 2 + 1) positions matter. Keep the counts in a 64-bit integer type (long long in C++, long in Java) and take the remainder after every addition and multiplication: adding or multiplying two remainders can be larger than a 32-bit int can hold (Python needs nothing special).
Input
A single line with STEPS and ARRLEN.
Output
Print the number of ways modulo 1000000007.
Constraints
1 <= STEPS <= 500; 1 <= ARRLEN <= 10^6
Example input
3 2
Example output
4
Explanation
With 3 steps and length 2, the ways are stay-stay-stay, right-left-stay, right-stay-left and stay-right-left. Each ends at index 0, so the answer is 4.