#P6634. [ZJOI2020] 密码

[ZJOI2020] 密码

Description

Bob likes Alice.

Alice and Bob want to communicate with encryption, so they designed an encryption algorithm for identity verification by themselves. You know that this encryption algorithm is not reliable, and you have intercepted the messages between Alice and Bob. Now you want to recover Alice’s secret key.

Alice and Bob agree on a large prime number pp, a random range value errerr, and an integer secret key xx generated uniformly at random in 0p10 \sim p - 1. Among them, the values of pp and errerr are public, while the value of xx is only known to Alice and Bob.

When Bob wants to verify Alice’s identity, Bob generates mm numbers aia_i uniformly at random in 0p10 \sim p - 1 and sends them to Alice. For each aia_i, Alice returns to Bob the value of aixa_i x modulo pp. To prevent eavesdropping, Alice adds a disturbance uniformly at random in err2-\lceil \frac {err}2 \rceil to err2\lceil \frac {err}2 \rceil to the result.

That is, Alice returns to Bob mm equations of the form aix+bici(modp)a_i x + b_i \equiv c_i \pmod p, where bib_i is a non-public number generated uniformly at random in err2-\lceil \frac {err}2 \rceil to err2\lceil \frac {err}2 \rceil, aia_i is a randomly generated number, and ai,p,err,cia_i, p, err, c_i are public numbers.

You have obtained these mm equations returned by Alice (that is, mm pairs of aia_i and cic_i). You need to find the value of xx.

Input Format

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

For each test case, the first line contains three integers m,p,errm, p, err. The next mm lines each contain two integers ai,cia_i, c_i. Their meanings are the same as in the statement.

Output Format

Output TT lines.
For each test case, output an integer between 00 and p1p - 1 as the answer. The testdata guarantees that a solution exists and the solution is unique.

见下发文件。该样例满足题目中提到的所有随机生成的性质。


Hint

For the first 10%10\% of the testdata, err106err \le 10^6.
For the first 20%20\% of the testdata, err108err \le 10^8.
For the first 30%30\% of the testdata, err1011err \le 10^{11}.
For the first 40%40\% of the testdata, err1012err \le 10^{12}.
For another 20%20\% of the testdata, p1016p \le 10^{16} and m=2000m = 2000.
For 100%100\% of the testdata, 1015p101810^{15} \le p \le 10^{18}, 50m200050 \le m \le 2000, 1err0.01p1 \le err \le 0.01p, 1T51 \le T \le 5, 0ai,cip10 \le a_i, c_i \le p - 1, and it is guaranteed that pp is prime.

Translated by ChatGPT 5