#P7325. [WC2021] 斐波那契

[WC2021] 斐波那契

Description

As everyone knows, student Xiaocong is good at calculations, especially at computing binomial coefficients. But after studying binomial coefficients thoroughly, Xiaocong lost interest in them and started researching sequences.

We define F0=aF_0 = a, F1=bF_1 = b, and Fi=(Fi1+Fi2)modmF_i = (F_{i-1} + F_{i-2}) \bmod m (i2i \ge 2).

Now you are given nn queries. For each query, find the smallest integer pp such that Fp=0F_p = 0.

Input Format

The first line contains two integers n,mn, m, representing the number of queries and the modulus used in each computation.

The next nn lines each contain two integers a,ba, b, representing the values of F0F_0 and F1F_1 in one query.

Output Format

For each query, output one integer pp on a single line as the answer. If such a pp does not exist, output -1.

4 5
0 1
1 2
2 3
3 4

0
3
2
-1

1 6
4 4

3

见附件中的 fib/fib3.in
见附件中的 fib/fib3.ans
见附件中的 fib/fib4.in
见附件中的 fib/fib4.ans

Hint

Constraints

For all testdata: 1n,m1051 \le n, m \le {10}^5, 0a,b<m0 \le a, b < m.

The specific limits for each test point are shown in the table below:

Test Point ID n,mn, m \le Special Constraints
121 \sim 2 10001000 None.
343 \sim 4 105{10}^5 mm is prime.
565 \sim 6 m=p1p2pkm = p_1 p_2 \cdots p_k, where the pip_i are pairwise distinct primes.
7107 \sim 10 None.

Translated by ChatGPT 5