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.
Ignore the theatrical hidden-number stuff for a second. Ask: if Alice knew the exact number, what is the minimum number of moves to make it ?
When the number is odd, Alice has no choice: she must subtract . When it is even, halving is never worse than subtracting .
So the optimal strategy is stupidly simple: if the current number is even, halve it; otherwise subtract . Alice can follow this strategy because Bob’s response always tells her the current parity.
For a number , this greedy strategy takes moves: one move for each halving, and one subtract for each bit.
Now just count numbers with Group numbers by their highest bit and use binomial coefficients to count how many lower bits may be set.
The big trap is assuming Alice’s lack of exact knowledge forces some complicated information-game DP. It doesn’t. The extra information from Bob is mostly noise. Alice only needs to know whether the current number is even, and Bob’s response gives that every time: means odd, means even, and means she already won.
Consider the optimal move for a known number.
If is odd, Alice cannot halve, so she must do
If is even, halving is always optimal. Subtracting makes the number odd and does not remove the main binary scale efficiently. More formally, the greedy value satisfies:
So Alice’s optimal strategy is:
This strategy depends only on parity, which Alice always knows. Nice little fakeout.
Now count the numbers that require more than moves.
For any positive integer , the number of moves is
Why? Every halving shifts the binary representation right. A number with highest bit position needs exactly halvings under the greedy process. Every bit eventually becomes the last bit and must be removed by one subtract operation, so there are exactly subtracts.
Let .
For numbers with highest bit , they lie in Their top bit is fixed as , and there are lower bits. If of those lower bits are set, then so
Such a number is winnable within moves iff or
For this fixed , the number of winnable values is therefore with zero contribution if .
The remaining number is exactly . Its move count is so it contributes one more winnable value if .
Let good be the total number Alice can win within moves. The answer is simply
Edge cases are clean:
The maximum possible is tiny because , so precomputing binomial coefficients is more than enough.
Complexity per test case: which is effectively constant here. Memory usage is
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
const int B = 61;
ll C[B][B]{};
for (int i = 0; i < B; i++) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; j++) {
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
int t;
cin >> t;
while (t--) {
ll n, k;
cin >> n >> k;
int d = __builtin_ctzll((unsigned long long)n);
ll good = 0;
for (int l = 0; l < d; l++) {
ll need = k - l - 1;
if (need < 0) continue;
int upto = min<ll>(l, need);
for (int q = 0; q <= upto; q++) {
good += C[l][q];
}
}
if (k >= d + 1) good++;
cout << n - good << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
const int B = 61;
ll C[B][B]{};
for (int i = 0; i < B; i++) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; j++) {
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
int t;
cin >> t;
while (t--) {
ll n, k;
cin >> n >> k;
int d = __builtin_ctzll((unsigned long long)n);
ll good = 0;
for (int l = 0; l < d; l++) {
ll need = k - l - 1;
if (need < 0) continue;
int upto = min<ll>(l, need);
for (int q = 0; q <= upto; q++) {
good += C[l][q];
}
}
if (k >= d + 1) good++;
cout << n - good << '\n';
}
}