Eel and Grid
Time limit1sMemory limit256 MB
An eel on a toroidal H by W grid walks right or down, painting cells, until it returns to a painted cell; count Hamiltonian-style walks that cover every cell and end at (0,0).
- Level
Hard8 of 10
- Topics
- Combinatorics, Math, Dynamic programming
- Solved
- No attempts yet
Problem
There is an grid. Let be the cell at the intersection of the -th row () and the -th column (). Initially, an eel is at cell . The eel repeats the following process.
- If the current cell is painted, end the process.
- If the current cell is not painted, paint the cell and move to another cell. If the current cell is , the new cell must be either or .
Count the number of ways to paint all cells and end the process at cell , modulo . Two ways are considered distinct if the paths traveled by the eel are distinct.
Input
Output
Print the answer modulo .
Constraints
Hint
The following picture shows the two ways in Sample 1:
