Knight Moves 2
Time limit2sMemory limit512 MB
Count knight paths of length at most k on a 2n by 2n board that start at the top-left corner and end on any corner, modulo 1000007.
- Level
Hard8 of 10
- Topics
- Matrix, Dynamic programming, Graph
- Solved
- No attempts yet
Problem
A knight stands on the top-left square of a chessboard. In one move the knight goes two squares in one direction and one square in the perpendicular direction, so its row and column change by or . A move that leaves the board is not allowed.
The corner squares are the four squares at the corners of the board: top-left, top-right, bottom-left, and bottom-right.
Count the ways to make at most moves and finish on a corner square. Two ways are different if they use a different number of moves, or if they visit a different sequence of squares. The knight already stands on the top-left corner at the start, so making no move at all counts as one way.
Stated another way, for every with , count the paths that start on the top-left square, make exactly moves, and end on a corner square, then add all of those counts. A path may revisit a square, and it may enter a corner square and leave it again.
Input
The first line contains the number of test cases ().
Each of the next lines contains one test case: two integers and separated by a space (, ).
Output
For each test case, print the number of ways modulo on its own line.