Tigger's bounces
InterviewTime limit1sMemory limit256 MB
Count length-K walks on an R by C grid where each step stays put or moves to a side neighbor, summed over all starts, modulo P per query.
- Level
Easy3 of 10
- Topics
- Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
Tigger loves to bounce. Today he spent the whole day in a rectangular field that is cells wide and cells long.
With one bounce Tigger lands on one of the four cells that share a side with the cell he is on, above, below, left or right, or he lands again on the same cell. He never lands outside the field.
Write down the cells Tigger lands on in order and you get a sequence of length . The first landing may be on any cell of the field, and every landing after that follows the rule above. Once Tigger lands for the th time, the day's bouncing is over.
Write a program that counts the different ways Tigger can bounce and prints the count modulo .
Input
The first line has a positive integer , the number of questions. is at most .
Each of the next lines holds one question: the positive integers , , , in that order, separated by single spaces.
, ,
Output
Print lines, one answer per question, in the order the questions are given. Each line holds the number of different ways Tigger can bounce, modulo .
Hint
Take , , . On a field every cell has three possible next landings, counting the cell itself. There are four choices for the first cell and three choices for each of the two later bounces, so the count is .