#P16409. [Algo Beat Contest 004 E] Elusive Prime

    ID: 18379 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度提高 上传者: 标签>数学交互题Special JudgeO2优化素数判断,质数,筛法随机化Ad-hoc

[Algo Beat Contest 004 E] Elusive Prime

Background

289caf553d8724a64acbc801aefdb6c8.png

Problem Description

This is an interactive problem.

You need to guess a hidden prime pp. Each time, you may query an integer aa, and the system will return the value of the Legendre symbol (ap)\left(\frac{a}{p}\right), defined as follows:

  • If pap \mid a, return 00.
  • Otherwise, if there exists an integer xx such that x2a(modp)x^2 \equiv a \pmod{p}, return 11.
  • Otherwise, return 1-1.

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, aa is an integer satisfying 1a1061 \le a \le 10^6. After each query, the judge will return an integer (00, 11, or 1-1), and your program should read this value from standard input.

When you are sure about pp, output the answer in the following format:

! p

Here, pp is the prime you guessed. After outputting the answer, your program must terminate immediately.

You may make at most 1717 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 p=7p=7. Explanation:

  • Query a=2a=2, and it returns 11, because 322(mod7)3^2\equiv 2\pmod{7}.
  • Query a=3a=3, and it returns 1-1, because 33 is not a quadratic residue modulo 77.
  • Query a=7a=7, and it returns 00, because 77 is divisible by pp.
  • Query a=1a=1, and it returns 11, because 11 is always a quadratic residue.
  • Finally, output the answer 77.

Constraints

  • 3p1063 \le p \le 10^6, and pp is prime.

Translated by ChatGPT 5