#P4454. [CQOI2018] 破解D-H协议
[CQOI2018] 破解D-H协议
Description
Assume the two parties are named Alice and Bob. The protocol works as follows (where denotes the modulo operation):
-
The protocol specifies a fixed prime and a primitive root modulo , . The values and are public and do not need to be kept secret.
-
Alice generates a random number , computes , and sends to Bob over the insecure channel.
-
Bob generates a random number , computes , and sends to Alice over the insecure channel.
-
Bob computes the key from the received , and Alice computes from the received .
-
Both parties obtain the same , namely . is the encryption key for later communication.
In this process, only and can be eavesdropped, while , , and remain secret. Given the four numbers , , , and , it is not easy to compute , so can serve as a secure key.
Of course, security is relative. The security of the protocol depends on the sizes of the values. Typically, , , and are chosen as large integers with hundreds of bits to prevent compromise. However, if Alice and Bob are lazy in programming and, to avoid implementing big-integer arithmetic, choose values less than , then cracking their key becomes relatively easy.
Given pairs of eavesdropped and , you need to try to crack the key .
Input Format
The first line contains two space-separated positive integers and .
The second line contains a positive integer , indicating that Alice and Bob performed sessions (i.e., ran the protocol times).
Then follow lines. Each line contains two space-separated positive integers and , representing the eavesdropped values and for one session.
Output Format
Output lines. Each line contains a single positive integer , which is the key you cracked for that session.
3 31
3
27 16
21 3
9 26
4
21
25
Hint
For of the testdata, .
For of the testdata, , , .
Statement fixed by @Starrykiller.
Translated by ChatGPT 5
京公网安备 11011102002149号