luogu#P7813. 谜
谜
Background
$\text{Even if I have to walk alone through the endless crowd.}$
Problem Description
In a number triangle of size :
- Row is .
- Row is .
- Row is .
- Row is .
- Row contains numbers, which are .
The figure below shows a number triangle with .

Let denote the -th number in the -th row.
It is known that can directly reach or . Conversely, or can also directly reach .
Now choose any number as the starting point. When you continuously pass through distinct numbers, find the maximum possible sum of these numbers, modulo .
Input Format
This problem contains multiple test cases.
The first line contains a positive integer , the number of queries.
The next lines each contain two positive integers .
Output Format
Output lines. Each line contains one integer, the answer modulo .
1
5 5
61
5
2676 1930
5148 3667
5453 4764
16734806 16332913
26943973 33293903
909411538
587883333
823595806
727601062
965648555
Hint
Sample Explanation
For sample #1, as shown in the figure in the statement, one feasible plan is: start from , $13\rightarrow9\rightarrow14\rightarrow10\rightarrow15$, with sum .
Constraints
This problem uses bundled testdata.
| Subtask | Score | ||
|---|---|---|---|
For of the testdata: , $1\le\color{red}\dfrac{K+1}{2}\le N\color{black}\le10^9$.
Translated by ChatGPT 5