Problem 642
Knight Dialer
Medium · 20 pts · Dynamic Programming
Problem
A chess knight stands on a phone keypad (1 2 3 / 4 5 6 / 7 8 9 / * 0 #) and may only stand on the digit keys. It moves like a knight: two squares in one direction and one square sideways. Count the distinct phone numbers of length N it can dial: place it on any digit, then make N - 1 valid jumps, dialling every digit it lands on. Print the count modulo 1000000007. Keep the number of ways to end on each of the 10 digits. 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 integer N.
Output
Print the number of distinct numbers of length N modulo 1000000007.
Constraints
1 <= N <= 5000
Example input
1
Example output
10
Explanation
For N = 1 the knight can start on any of the 10 digits and dial just that digit, so the answer is 10. For N = 2 there are 20 valid two-digit numbers, such as 04, 06, 16 and 18.