Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Forget the “battery identities may change” wording for a second. Every 0 answer still means the interactor is claiming there is some valid set of working batteries avoiding all pairs you already tested.
Put the batteries on a circle in order . If there are working batteries, look at the circular gaps between consecutive working batteries.
Those gaps sum to , so at least one gap is at most . Pigeonhole principle doing honest work, for once.
So if you test every pair at circular distance , then every pair at circular distance , and so on, by distance you must have tested two consecutive working batteries.
You do not need to know . Just keep increasing the distance . For the true hidden , success happens by , after at most queries.
The annoying part of this problem is that is hidden. The clean part is that we can use a strategy that works for every possible at once.
Place batteries around a circle in that order.
For a distance , define a query pattern:
where indices wrap around modulo . So for fixed , we ask all pairs that are exactly steps clockwise apart.
The algorithm is just:
Stop immediately when the answer is 1.
Why this has to find a working pair
Suppose the true number of working batteries is .
Take any valid configuration of working batteries and list them in clockwise order:
Now look at the circular gaps between consecutive working batteries:
These gaps are positive integers, and together they wrap around the whole circle, so their sum is exactly
Therefore, at least one gap is at most
Otherwise, if all gaps were bigger than , their sum would exceed . Not happening.
So among the working batteries, there is some consecutive pair whose clockwise distance is at most
Our algorithm tests every pair at distance , then every pair at distance , and so on. By the time it finishes distance
it must have tested that consecutive working pair. The flashlight turns on, and we are done.
Why adaptivity does not break this
The interactor can adapt, but it still has to keep some configuration of exactly working batteries consistent with every 0 answer so far.
If the algorithm finished all distances up to with only 0s, then every valid set of batteries would have all circular gaps larger than . But we just proved that is impossible.
So before or during that point, the interactor is forced to answer 1. The adaptive part sounds scary, but the circle gap argument pins it down anyway. Nice try, jury.
Query count
For each distance , we ask exactly queries.
For the real value , success happens by distance
So the number of queries is at most
And since
we get
The left side is an integer, so
That is exactly the required limit.
Complexity
At most possible directed distance queries are attempted, and , so this is tiny. The real limit is the interactive query bound, and we satisfy it.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
bool found = false;
for (int d = 1; d < n && !found; d++) {
for (int u = 1; u <= n && !found; u++) {
int v = (u + d - 1) % n + 1;
cout << u << ' ' << v << endl;
int res;
cin >> res;
if (res == -1) return 0;
if (res == 1) found = true;
}
}
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
bool found = false;
for (int d = 1; d < n && !found; d++) {
for (int u = 1; u <= n && !found; u++) {
int v = (u + d - 1) % n + 1;
cout << u << ' ' << v << endl;
int res;
cin >> res;
if (res == -1) return 0;
if (res == 1) found = true;
}
}
}
return 0;
}