A15038 | GCD Guess
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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](<https://en.wikipedia.org/wiki/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.
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](<https://en.wikipedia.org/wiki/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.
输入格式
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$ ).
The integer $x$ that you have to guess satisfies the constraints: ( $1 \le x \le 10^9$ ).
输出格式
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.
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.
输入输出样例
输入 #1
2 1 8 1
输出 #1
? 1 2 ? 12 4 ! 4 ? 2000000000 1999999999 ! 1000000000
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$ .
"? 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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted