Codeforces Round 964 (Div. 4) G2. Ruler (hard version)

G2. Ruler (hard version)

time limit per test: 1 second

memory limit per test: 256 megabytes

This is the hard version of the problem. The only difference between the two versions is that in this version, you can make at most 7 queries.

This is an interactive problem. If you are unsure how interactive problems work, then it is recommended to read the guide for participants.

We have a secret ruler that is missing one number x (2≤x≤999). When you measure an object of length y, the ruler reports the following values:

If y<x, the ruler (correctly) measures the object as having length y.

If y≥x, the ruler incorrectly measures the object as having length y+1.

The ruler above is missing the number 4, so it correctly measures the first segment as length 3 but incorrectly measures the second segment as length 6 even though it is actually 5.

You need to find the value of x. To do that, you can make queries of the following form:

? a b --- in response, we will measure the side lengths of an a×b rectangle with our ruler and multiply the results, reporting the measured area of the rectangle back to you. For example, if x=4 and you query a 3×5 rectangle, we will measure its side lengths as 3×6 and report 18 back to you.

Find the value of x. You can ask at most 7 queries.

Input

Each test contains multiple test cases. The first line of input contains a single integer t (1≤t≤1000) --- the number of test cases.

Interaction

There is no initial input for each test case. You should begin the interaction by asking a query.

To make a query, output a single line of the form ? a b (1≤a,b≤1000). In response, you will be told the measured area of the rectangle, according to our secret ruler.

When you are ready to print the answer, output a single line of the form ! x (2≤x≤999). After that, proceed to process the next test case or terminate the program if it was the last test case. Printing the answer does not count as a query.

The interactor is not adaptive, meaning that the answer is known before the participant asks the queries and doesn't depend on the queries asked by the participant.

If your program makes more than 7 queries for one set of input data, makes an invalid query, or prints the wrong value of x, then the response to the query will be −1. After receiving such a response, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

After printing a query do not forget to output the end of line and flush the output. Otherwise, you may get Idleness limit exceeded verdict. 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 the documentation for other languages.

Hacks

To make a hack, use the following format.

The first line should contain a single integer t (1≤t≤1000) --- the number of test cases.

The only line of each test case should contain a single integer x (2≤x≤999) --- the missing number on the ruler.

Example

Input

2

18

25

9999

Output

? 3 5

? 4 4

! 4

? 99 100

! 100

Note

In the first test, the interaction proceeds as follows.

|----------|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Solution | Jury | Explanation |
| | 2 | There are 2 test cases. |
| ? 3 5 | 18 | Secretly, the jury picked x=4. The solution requests the 3×5 rectangle, and the jury responds with 3×6=18, as described in the statement. |
| ? 4 4 | 25 | The solution requests the 4×4 rectangle, which the jury measures as 5×5 and responds with 25. |
| ! 4 | | The solution has somehow determined that x=4, and outputs it. Since the output is correct, the jury continues to the next test case. |
| ? 99 100 | 1 | Secretly, the jury picked x=100. The solution requests the 99×100 rectangle, which the jury measures as 99×101 and responds with 9999. |
| ! 100 | | The solution has somehow determined that x=100, and outputs it. Since the output is correct and there are no more test cases, the jury and the solution exit. |

Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interaction.

【思路分析】

三分。

cpp 复制代码
#include<bits/stdc++.h>

#define i64 long long

using namespace std;

inline i64 query(i64 a, i64 b) {
    i64 tmp;
    cout << "? " << a << " " << b << endl;
    cin >> tmp;
    return tmp;
}

void solve() {
    i64 l = 2, r = 999;
    while (l < r) {
        i64 mid1 = l + (r - l) / 3, mid2 = r - (r - l) / 3;
        i64 res = query(mid1, mid2);
        if (res == mid1 * mid2) {
            l = mid2 + 1;
        } else if (res == mid1 * (mid2 + 1)) {
            l = mid1 + 1;
            r = mid2;
        } else r = mid1;
    }
    cout<<"! "<<r<<endl;
    cout.flush();
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
相关推荐
寻找码源41 分钟前
【头歌实训:利用kmp算法求子串在主串中不重叠出现的次数】
c语言·数据结构·算法·字符串·kmp
Matlab精灵42 分钟前
Matlab科研绘图:自定义内置多款配色函数
算法·matlab
诚丞成1 小时前
滑动窗口篇——如行云流水般的高效解法与智能之道(1)
算法
带多刺的玫瑰2 小时前
Leecode刷题C语言之统计不是特殊数字的数字数量
java·c语言·算法
爱敲代码的憨仔2 小时前
《线性代数的本质》
线性代数·算法·决策树
yigan_Eins3 小时前
【数论】莫比乌斯函数及其反演
c++·经验分享·算法
阿史大杯茶3 小时前
AtCoder Beginner Contest 381(ABCDEF 题)视频讲解
数据结构·c++·算法
დ旧言~3 小时前
【高阶数据结构】图论
算法·深度优先·广度优先·宽度优先·推荐算法
张彦峰ZYF3 小时前
投资策略规划最优决策分析
分布式·算法·金融
The_Ticker4 小时前
CFD平台如何接入实时行情源
java·大数据·数据库·人工智能·算法·区块链·软件工程