#P1762D. GCD Queries
GCD Queries
Description
This is an interactive problem.
There is a secret permutation of . Your task is to find indices and (, possibly ) such that or . In order to find it, you are allowed to ask at most queries.
In one query, you give two integers and (, ) and receive the value of .
Note that the permutation is fixed before any queries are made and does not depend on the queries.
denotes the greatest common divisor (GCD) of integers and . Note that for all positive integers .
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains a single integer ().
After reading the integer for each test case, you should begin the interaction.
It is guaranteed that the sum of over all test cases does not exceed .
Interaction
The interaction for each test case begins by reading the integer .
To make a query, output "? " (, ) without quotes. Afterwards, you should read a single integer — the answer to your query . You can make at most such queries in each test case.
If you want to print the answer, output "! " () without quotes. After doing that, read or . If or , you'll receive , otherwise you'll receive . If you receive , your program must terminate immediately to receive a Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.
If you receive the integer instead of an answer or a valid value of , it means your program has made an invalid query, has exceeded the limit of queries, or has given an incorrect answer on the previous test case. Your program must terminate immediately to receive a Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.
After printing a query or the answer, do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- flush(output) in Pascal;
- stdout.flush() in Python;
- see documentation for other languages.
Hacks
To hack, use the following format.
The first line should contain a single integer ().
The first line of each test case should contain a single integer ().
The second line of each test case should contain space separated integers . should be a permutation of .
The sum of should not exceed .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains a single integer ().
After reading the integer for each test case, you should begin the interaction.
It is guaranteed that the sum of over all test cases does not exceed .
Note
In the first test, the interaction proceeds as follows.
Solution | Jury | Explanation |
There are 2 test cases. | ||
In the first test case, the hidden permutation is , with length . | ||
The solution requests , and the jury responds with . | ||
The solution knows that either or , and prints the answer. Since the output is correct, the jury responds with and continues to the next test case. | ||
In the second test case, the hidden permutation is , with length . | ||
The solution requests , and the jury responds with . | ||
The solution requests , and the jury responds with . | ||
The solution has somehow determined that , and prints the answer. Since the output is correct, the jury responds with . |
Note that the empty lines in the example input and output are for the sake of clarity, and do not occur in the real interaction.
After each test case, make sure to read or .