#P16409. [Algo Beat Contest 004 E] Elusive Prime
[Algo Beat Contest 004 E] Elusive Prime
Background
Problem Description
This is an interactive problem.
You need to guess a hidden prime . Each time, you may query an integer , and the system will return the value of the Legendre symbol , defined as follows:
- If , return .
- Otherwise, if there exists an integer such that , return .
- Otherwise, return .
The Legendre symbol is a basic tool in number theory, and it satisfies Euler's criterion:
$$\left(\frac{a}{p}\right) \equiv a^{\frac{p-1}{2}} \pmod{p}$$Interaction
Your program must interact with the judge through standard input and output. First, your program should start querying. Each query should be in the following format:
? a
Here, is an integer satisfying . After each query, the judge will return an integer (, , or ), and your program should read this value from standard input.
When you are sure about , output the answer in the following format:
! p
Here, is the prime you guessed. After outputting the answer, your program must terminate immediately.
You may make at most queries. If the number of queries exceeds the limit, or the answer is wrong, or the format does not meet the requirements, the judge will consider it a wrong answer.
Note: After each output, you must flush the buffer, for example, use cout << endl or fflush(stdout) in C++, and print(..., flush=True) in Python.
You may ignore the running time of the interactive library.
1
-1
0
1
? 2
? 3
? 7
? 1
! 7
Hint
Sample Explanation #1
In the sample, the hidden prime is . Explanation:
- Query , and it returns , because .
- Query , and it returns , because is not a quadratic residue modulo .
- Query , and it returns , because is divisible by .
- Query , and it returns , because is always a quadratic residue.
- Finally, output the answer .
Constraints
- , and is prime.
Translated by ChatGPT 5
