The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
Solutions
Solution 1: Recurrence
Thinking
A knight hops \(n-1\) times on a phone pad, \(n\le 5000\). Each digit has a fixed set of predecessors, so the count of length-\(i\) numbers ending at \(d\) depends only on the previous hop. Roll a \(10\)-wide array for \(n-1\) steps and sum modulo \(10^9+7\).
According to the problem description, we need to calculate the number of different phone numbers of length \(n\). Each digit can only follow certain fixed digits, which we can list as follows:
Current Digit
Previous Digits
0
4, 6
1
6, 8
2
7, 9
3
4, 8
4
0, 3, 9
5
6
0, 1, 7
7
2, 6
8
1, 3
9
2, 4
We can use a recurrence approach to calculate the number of different phone numbers of length \(n\). Let \(f[i]\) represent the number of different phone numbers of length \(i\). Initially, \(f[1] = 1\). For phone numbers of length \(i\), we can calculate them based on phone numbers of length \(i - 1\). Therefore, we can derive the recurrence relations:
Then, we update \(f\) to \(g\) and continue calculating the phone numbers of the next length until we calculate the number of phone numbers of length \(n\).
Finally, we sum all the elements in \(f\) and take the result modulo \(10^9 + 7\) to get the answer.
The time complexity is \(O(n)\), where \(n\) is the length of the phone number. The space complexity is \(O(|\Sigma|)\), where \(\Sigma\) is the set of digits, and in this problem \(|\Sigma| = 10\).
Solution 2: Matrix Exponentiation to Accelerate Recurrence
Thinking
Method 1 is linear in \(n\). The same recurrence is a constant linear map, written as a \(10\times 10\) matrix, and matrix exponentiation yields \(T(n)\) in \(O(\log n)\) multiplications.
Let's denote \(T(n)\) as a \(1 \times 10\) matrix \(\begin{bmatrix} F_0 & F_1 & F_2 \cdots F_9 \end{bmatrix}\), where \(F_i\) represents the number of phone numbers ending with digit \(i\). We want to derive \(T(n)\) from \(T(n - 1)\). In other words, we need a matrix \(\textit{base}\) such that \(T(n - 1) \times \textit{base} = T(n)\), i.e.:
We define the initial matrix \(res = \begin{bmatrix} 1 & 1 & 1 \cdots 1 \end{bmatrix}\), and multiply it by the matrix \(\textit{base}\) raised to the power of \(n - 1\) to obtain \(T(n)\). Finally, we sum all elements in \(T(n)\) and take the result modulo \(10^9 + 7\) to get the answer. The matrix \(\textit{base}^{n - 1}\) can be computed using matrix exponentiation, which has a time complexity of \(O(\log n)\).
The time complexity is \(O(\log n)\), and the space complexity is \(O(|\Sigma|^2)\), where \(\Sigma\) is the set of digits, and in this problem \(|\Sigma| = 10\).