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 gcd. The target fraction is about value, so Bob is looking for states satisfying .
Look at three regions for the ratio: , , and . Alice can make the first two regions boring for Bob.
If , Alice can keep the ratio at least by mostly decreasing . Since , Bob never touches the target line.
If , Alice should decrease whenever possible. This keeps the ratio safely below ; Bob decreasing cannot magically land exactly on the target after that move.
In the remaining case, and . Bob can reply by decreasing the opposite variable from Alice, so after each full round both and drop by . After rounds, the state becomes a multiple of .
The losing-looking line is
because that is exactly . The whole game is about whether Bob can force the path to hit this line. No gcd, no fraction reduction ceremony. Just cross-multiply and move on.
From research, the official Codeforces tutorial page exposes the intended solution condition in code as p < q && min(p / 2, q / 3) >= q - p, and accepted/explanation sources give the equivalent cleaner form and . These are the same condition, since with :
and
So Bob wins exactly when
Now prove it, because vibes are not a proof.
First, suppose . Alice can keep the fraction at least whenever it matters. If , she decreases , making even larger. If Bob decreases , the best he can do is restore something like equality; he cannot force the ratio down to . Since , the target is never reached. If , the target is impossible anyway because is an integer. Alice wins.
Second, suppose , meaning . Alice decreases whenever . After her move, the value of drops by , so it is still negative. Bob has two possible replies:
But right after Alice's move it is at most , so after Bob decreases it is still at most . Thus Bob cannot hit . Once , the target is also impossible. Alice wins.
The only remaining region is
Let . Bob uses the simple annoying strategy: on every turn, he decreases the opposite variable from Alice. If Alice decreases , Bob decreases ; if Alice decreases , Bob decreases . Therefore after each complete Alice+Bob round, both numbers have decreased by exactly .
After full rounds, the state is
We want this to lie on the target line:
Cross-multiply:
In this region, . Also the target state is legal:
So after exactly rounds, the game reaches , whose fraction is . If , Bob wins immediately because the initial fraction is already . Otherwise, Bob forces it after some replies. Alice cannot dodge it; Bob is basically dragging both coordinates diagonally until the line catches them.
Therefore the answer is:
The constraints go up to , but and still fit in signed 64-bit integers. Using long long is fine.
Complexity per test case is time and memory.
#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--) {
ll p, q;
cin >> p >> q;
if (p < q && 3 * p >= 2 * q) {
cout << "Bob\n";
} else {
cout << "Alice\n";
}
}
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--) {
ll p, q;
cin >> p >> q;
if (p < q && 3 * p >= 2 * q) {
cout << "Bob\n";
} else {
cout << "Alice\n";
}
}
return 0;
}