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.
Do not use Nim XOR. The opponent chooses the pile, so the mover needs a good reply for every pile the opponent might point at.
Write the recurrence: a position is winning iff for every non-empty pile, there exists some reduction of that pile that leaves a losing position.
Look at piles of size . If the opponent chooses a -pile, the mover has no choice: that pile disappears. So the number of -piles can create a forced parity chain.
If there are no -piles, the mover is usually happy: when a pile is chosen, they can reduce it to , creating exactly one forced -pile for the opponent. The only single-pile case is even easier: take it all.
Let be the number of piles equal to . If , Alice wins. If , Alice wins iff is odd. Otherwise, Alice wins iff is even.
Flip the recurrence
This is not normal Nim. XOR is bait. In normal Nim, the current player chooses the pile. Here the opponent chooses the pile, so the quantifiers flip.
Call a position winning if the player to move can force a win. Then a non-empty position is winning iff:
And a position is losing iff the opponent can point at some pile where every legal reduction leaves a winning position.
The empty position is losing.
Why piles of size matter
If the opponent points at a pile of size , the mover has no choice. They must remove it.
That means -piles are forced moves. No strategy, no finesse, just bonk, pile gone.
Let be the number of piles equal to .
There are three cases.
Case 1: No pile has size
If , Alice just removes the only pile and wins.
If , no matter which pile Bob chooses, Alice can reduce it to exactly . The new state has exactly one -pile and at least one bigger pile. As we will prove below, that is losing for the next player.
So if , the position is winning.
Case 2: All piles have size
Then every move is forced: each turn removes exactly one pile.
So the winner is determined by parity:
Thus, when , Alice wins iff is odd.
Case 3: Some, but not all, piles have size
Now there is at least one -pile and at least one larger pile.
If is odd, the position is losing.
Why? The opponent chooses a -pile. The mover is forced to delete it, so the number of -piles becomes , which is even. The position still has a larger pile, unless there were no larger piles, but this case explicitly has one. An even number of -piles in a mixed position is winning for the next player.
So with odd , the opponent has a pile choice that gives the mover no good option. That is exactly losing.
If is even, the position is winning. We must answer every possible pile choice:
So when the position is mixed, Alice wins iff is even.
Final rule
Let be the number of piles equal to .
Alice.Alice iff is even.That is it. The whole game is just the tyranny of size- piles. Brutal, but clean.
Complexity
We only count piles equal to .
per test case, with total .
#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;
int ones = 0;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
if (x == 1) ones++;
}
bool alice;
if (ones == 0) alice = true;
else if (ones == n) alice = (n % 2 == 1);
else alice = (ones % 2 == 0);
cout << (alice ? "Alice" : "Bob") << '\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--) {
int n;
cin >> n;
int ones = 0;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
if (x == 1) ones++;
}
bool alice;
if (ones == 0) alice = true;
else if (ones == n) alice = (n % 2 == 1);
else alice = (ones % 2 == 0);
cout << (alice ? "Alice" : "Bob") << '\n';
}
return 0;
}