#P9510. 『STA - R3』高维立方体

    ID: 10592 远端评测题 1500ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>数学O2优化矩阵加速Fibonacci 数列

『STA - R3』高维立方体

Problem Description

The Fibonacci sequence is defined as follows:

$$\operatorname{fib}(n)=\begin{cases}1&n\le 2\\\operatorname{fib}(n-1)+\operatorname{fib}(n-2)&n>2\end{cases}$$

Now we define a function (note that when n<1n<1, the value of this function is 00):

f(n)=i=1nfib2(i)f(n)=\sum_{i=1}^n\operatorname{fib}^2(i)

Since computing the prefix sum of the Fibonacci sequence is too easy, you need to compute:

$$\sum_{i=1}^n\operatorname{fib}(i)\cdot(f(i-2)+\operatorname{fib}^2(i)+\operatorname{fib}(i))$$

The answer should be taken modulo the given pp.

Note: fib2(x)\operatorname{fib}^2(x) means the square of fib(x)\operatorname{fib}(x).

Input Format

This problem contains multiple test cases.

The first line contains an integer TT, the number of test cases.

For each test case, a line contains two integers n,pn,p.

Output Format

For each test case, output one integer per line, the result modulo pp.

3
2 100
3 100
4 100
4
18
60

Hint

Sample explanation:

For the first test case, 1×(0+12+1)+1×(0+12+1)=41\times(0+1^2+1)+1\times(0+1^2+1)=4.

For the second test case, $1\times(0+1^2+1)+1\times(0+1^2+1)+2\times(1+2^2+2)=18$.

Constraints

This problem uses bundled tests.

  • Subtask 1 (5 points): n107n \le 10^7, p=109+7p=10^9+7.
  • Subtask 2 (20 points): T104T\le 10^4, n108n \le 10^8, p=109+7p=10^9+7.
  • Subtask 3 (5 points): p=2p=2.
  • Subtask 4 (15 points): p5p\le 5.
  • Subtask 5 (30 points): T104T\le 10^4, n108n \le 10^8.
  • Subtask 6 (25 points): no special constraints.

For all testdata, 1T2×1051\le T\le 2\times 10^5, 1n10181\le n\le 10^{18}, 2p109+72\le p\le 10^9+7

Translated by ChatGPT 5