TOURNAMENT
Problem
In an online billiards championship, each player plays against various other players in the order determined by the system. Each win earns the player some points, and these points are accumulated for each player - losses, on the other hand, do not give points. To make the game more interesting, the organizers of the championship have decided on the following evaluation system:
- The points for each win increase according to the number of consecutive wins achieved by a player. The first win gives point, the second (consecutive) points, the third (again consecutive) points, the fourth points, and so on.
- In case of a loss, consecutive wins naturally end. Thus, the first subsequent win will again give point.
- If a player loses two consecutive times, they are eliminated from the championship.
Let there be two natural numbers . Find in how many different ways a player can accumulate points, where , before being eliminated from the championship.
For example, if and , a player can accumulate , , or points in the following twelve () ways, where the numbers denote a win that gives as many points as the value of the number, and the symbol denotes a loss:
1 x 1 x x 1 x 1 2 x x
x 1 x 1 x x x 1 x 1 2 x x
1 x 1 x 1 x x 1 2 x x
x 1 x 1 x 1 x x x 1 2 x x
1 x 1 x 1 x 1 x x 1 2 x 1 x x
x 1 x 1 x 1 x 1 x x x 1 2 x 1 x x
Input:
The first line of the input will contain an integer , the number of queries you need to answer. Each of the next lines of the input will contain two numbers and , separated by a space.
Output:
The output should consist of exactly lines, each containing exactly one integer: the answer to the corresponding query in order. Because the answer can be a very large number for large values of and , you should print the remainder (modulo) of this number divided by .
Constraints
- Time limit: sec.
- Memory limit: MB.
Attention: Both the size of the input data and the size of the output data can be large. We recommend reading the data efficiently (see <cstdio> and scanf).
Notes
- For test cases with a total value of , it will be and .
- For test cases with a total value of , it will be and .
- For test cases with a total value of , it will be .
Example of Input - Output Files:
STDIN (tournament.in)
4
2 4
3 3
17 42
1742 4217
STDOUT (tournament.out)
12
4
126321690
376826466
Explanation of the example
The first query, with and , is the one explained in detail above.
Comments