What a sequence!
Time limit2sMemory limit512 MB
For each of up to a million queries, compute a_p mod p where a is defined by a_{n+2} = k a_{n+1} + a_n with a_0 = 0, a_1 = 1, given an odd prime p and k in {1,3,5,7}.
- Level
Medium7 of 10
- Topics
- Math, Number theory, Matrix, Implementation
- Solved
- No attempts yet
Problem
The sequence is defined by the recurrence
\begin{align*} a_{n+2} &= k\cdot a_{n+1} + a_{n} \\ a_{0} &= 0 \\ a_{1} &= 1 \end{align*}
Given and an odd prime , find the value of .
Input
The first line contains one integer , the number of test cases.
Each test case consists of a single line with two natural numbers and , where is an odd prime.
Output
For each test case, print one line with the value of .
Constraints
- The total number of digits of over all test cases does not exceed .