RCPC Simulare 2023 day 2 | E. Fiboxor

This was the problem page during the contest. Access the current page here.
Time limit: 1s Memory limit: 64MB Input: Output:

Consider the following integer sequence ff:

f1=f2=1f_1 = f_2 = 1, fk=∣fk−1−fk−2∣⊕(fk−1+fk−2)f_k = |f_{k-1} - f_{k-2}| \oplus (f_{k - 1} + f_{k - 2}), for k≥3k \geq 3.

Now, you are wondering, for qq triples of integers (l,r,Ml, r, M), what is the sum of all fif_i, l≤i≤rl \leq i \leq r, modulo MM. That is, find (∑i=lrfi)\displaystyle(\sum_{i = l}^rf_i) and print it's remainder modulo MM.

By ∣x∣|x| we denote the absolute value of xx and by ⊕\oplus we denote the bitwise XOR operation.

Input data

In the first line of input, you will read one integer qq (1≤q≤2⋅1051 \leq q \leq 2 \cdot 10^5).

In the next qq lines, you will read three integers ll, rr (1≤l≤r≤1091 \leq l \leq r \leq 10^9) and MM (108<M<10910^8 < M < 10^9, MM is prime).

Output data

Output qq lines, the ithi^{th} of them representing the answer for the ithi^{th} triple (l,r,Ml, r, M).

Example

stdin

4
1 3 998244353
2 4 998244353
4232324 12345678 998244353
92345678 998244353 998244353

stdout

4
5
652671816
367684397

Explanation

The first four values of ff are: f1=1,f2=1,f3=2,f4=2f_1 = 1, f_2 = 1, f_3 = 2, f_4 = 2.

Thus, the answer for the first question is 1+1+2=41 + 1 + 2 = 4 and the answer for the second question is 1+2+2=51 + 2 + 2 = 5.

Log in or sign up to be able to send submissions!