Domino Covering
Time limit6sMemory limit512 MB
Count the domino tilings of an n by m grid for up to 20000 queries, with n at most 35, m up to 10^18, and a prime modulus p given per query.
- Level
Hard9 of 10
- Topics
- Matrix, Dynamic programming, Divide and conquer, Math
- Solved
- No attempts yet
Statement
Elizur has an empty grid. He wants to cover the whole grid with some and dominoes. Each domino covers exactly two adjacent squares, and each square is covered by exactly one domino. Two squares are adjacent if they share a side.
The grid can be covered if and only if at least one of and is even. Otherwise, some square must always be left empty. He wants to know how many ways there are to cover the entire grid. Two coverings are different if there exist a domino from one covering and a domino from the other such that they cover the same square in one place but cover different squares elsewhere.
The answer can be very large, so output it modulo a prime .
Input
The first line contains one integer (), the number of queries.
Each of the next lines contains three integers (), (), and (, is prime).
At most 1000 queries satisfy or .
Output
For each query, output one line containing the number of covering ways modulo .
Hint
The figure shows all 11 possible coverings of a grid.
