GCD Guess
Contest: Codeforces Round 781 (Div. 2)
Difficulty rating: 2000
Tags: bitmasks, chinese remainder theorem, constructive algorithms, games, interactive, math, number theory

This is an interactive problem.

There is a positive integer $$$1 \le x \le 10^9$$$ that you have to guess.

In one query you can choose two positive integers $$$a \neq b$$$. As an answer to this query you will get $$$\gcd(x + a, x + b)$$$, where $$$\gcd(n, m)$$$ is the greatest common divisor of the numbers $$$n$$$ and $$$m$$$.

To guess one hidden number $$$x$$$ you are allowed to make no more than $$$30$$$ queries.

Input format:
The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) denoting the number of test cases.

The integer $$$x$$$ that you have to guess satisfies the constraints: ($$$1 \le x \le 10^9$$$).

Interaction format:
The hidden number $$$x$$$ is fixed before the start of the interaction and does not depend on your queries.

To guess each $$$x$$$ you can make no more than $$$30$$$ queries in the following way:

- "? a b" ($$$1 \le a, b \le 2 \cdot 10^9$$$, $$$a \neq b$$$).

For this query you will get $$$\gcd(x + a, x + b)$$$.

When you know $$$x$$$, print a single line in the following format.

- "! x" ($$$1 \le x \le 10^9$$$).

After that continue to solve the next test case.

If you ask more than $$$30$$$ queries for one $$$x$$$ or make an invalid query, the interactor will terminate immediately and your program will receive verdict Wrong Answer.

After printing each query do not forget to output end of line and flush the output buffer. Otherwise, you will get the Idleness limit exceeded verdict. To do flush use:

- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- flush(output) in Pascal;
- stdout.flush() in Python;
- Read documentation for other languages.

Hacks

To use hacks, use the following format of tests:

The first line should contain a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.

The first and only line of each test case should contain a single integer $$$x$$$ ($$$1 \le x \le 10^9$$$) denoting the integer $$$x$$$ that should be guessed.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
The first hidden number is $$$4$$$, that's why the answers for the queries are:

"? 1 2" — $$$\gcd(4 + 1, 4 + 2) = \gcd(5, 6) = 1$$$.

"? 12 4" — $$$\gcd(4 + 12, 4 + 4) = \gcd(16, 8) = 8$$$.

The second hidden number is $$$10^9$$$, that's why the answer for the query is:

"? 2000000000 1999999999" — $$$\gcd(3 \cdot 10^9, 3 \cdot 10^9 - 1) = 1$$$.

These queries are made only for understanding the interaction and are not enough for finding the true $$$x$$$.

Example input:
2

1

8


1

Example output:
? 1 2

? 12 4

! 4
? 2000000000 1999999999

! 1000000000

Editorial:
Solution 1
Let's iteratively find the remainder of $$$x \bmod$$$ each power of $$$2$$$. Initially, we know that $$$x \bmod 2^0 = x \bmod 1 = 0$$$. If we know that $$$x \bmod 2^k = r$$$, then how do we find $$$x \bmod 2^{k + 1}$$$? To do that let's ask $$$\gcd(x + 2^k - r, 2^{k + 1}) = \gcd(x + 2^k - r, x + 2^k - r + 2^{k + 1})$$$. If $$$\gcd = 2^{k + 1}$$$, then $$$x \bmod 2^{k + 1} = r + 2^{k - 1}$$$ else $$$x \bmod 2^{k + 1} = r$$$. Using this algorithm we will find $$$x \bmod 2^30$$$ which is just $$$x$$$. It takes exactly $$$30$$$ queries.
Solution 2
Let's consider a set of pairwise coprime numbers $$$\{23, 19, 17, 13, 11, 9, 7, 5, 4\}$$$. Their $$$\text{lcm} > 10^9$$$ that's why $$$x \bmod \text{lcm} = x$$$. Let's find $$$x \bmod$$$ each of these numbers. To do that, for each $$$1 \le i \le 23$$$ we can ask $$$\gcd(x + i, x + \text{lcm} + i)$$$ (the query is $$$(i, \text{lcm} + i)$$$). If the $$$\gcd$$$ is a multiple of some number from our set then $$$x \bmod$$$ this number is $$$-i$$$. After that we can use the chinese remainder theorem to find $$$x$$$ that gives the same remainders for numbers from the set. This solution asks only $$$23$$$ queries.
Observation 1: It's enough to make only $$$22$$$ queries, because if we did not find anything for $$$1 \le i \le 22$$$ then we can guarantee that $$$i = 23$$$ will do.
Observation 2: All moduli are small, that's why it is possible to use a simplified CRT (check the implementation).