1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
| #include <bits/stdc++.h>
using namespace std;
bool ask(int u, int v) {
bool res = 0;
cout << "? " << u << " " << v << endl;
cin >> res;
return res;
}
void answer(int u) { cout << "! " << u << endl; }
void solve() {
int n;
cin >> n;
vector<pair<int, int>> vec;
vector<int> vis(n + 1);
for (int i = 1; i + 1 <= n; i += 2) {
if (ask(i, i + 1)) {
vec.emplace_back(i, i + 1);
vis[i] = vis[i + 1] = 1;
}
}
if (vec.size() > 1) {
answer(1);
return;
}
if (n % 2) {
int u = 1;
while (vis[u])
u++;
if (ask(n, u)) {
vec.emplace_back(n, u);
vis[n] = vis[u] = 1;
}
}
if (vec.size() != 1) {
answer(1);
return;
}
auto [u, v] = vec.front();
int p = 1;
while ((p == u or p == v) and p <= n)
p++;
int q = 1;
while ((q == p or q == u or q == v) and q <= n)
q++;
bool res1 = ask(u, p);
bool res2 = ask(v, p);
bool res3;
if (res1 == 0 and res2 == 0) {
answer(1);
return;
}
if (res1) {
res3 = ask(u, q);
}
if (res2) {
res3 = ask(v, q);
}
if (res3)
answer(2);
else
answer(1);
return;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int T = 1;
cin >> T;
while (T--)
solve();
return 0;
}
|