Endless Knight (Large)
Time limit5sMemory limit512 MB
Count right-and-down knight paths from (1,1) to (H,W) on a board up to 1e8 wide, avoiding at most 10 rocks, modulo 10007.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Combinatorics, Math, Implementation
- Solved
- No attempts yet
Problem
Chess has a piece called the knight. It does not move in a straight line like the other pieces, it jumps in an L shape. Precisely, a knight jumps from square to square if and only if .
In this problem one knight has to cross a gigantic board of height and width , from the top left square to the bottom right square .
The rules are these.
- The knight only moves right and down. Every jump lands on a square whose row number and column number are both larger. Because of that, the goal is sometimes unreachable. On a board, for example, there is no way at all.
- squares of the board hold rocks. The knight may not land on such a square, although flying over one during a jump is allowed.
Count the distinct ways for the knight to go from to . Two ways are distinct when the sequence of squares the knight lands on differs. The count gets huge, so print its remainder modulo the prime 10007.
Input
The first line contains a single integer , the number of test cases. test cases follow.
The first line of each test case contains three integers , , and . Each of the next lines contains two integers and , the row number and the column number of one rock. Neither nor holds a rock, and no two rocks share a square.
Limits
Output
For each test case, print one line holding Case #X: followed by the number of ways to reach the goal square, taken modulo 10007. is the 1 based test case number.