Several heaps of stones. A move removes any positive number of stones from one heap. The player who cannot move loses (normal play).
Bouton’s theorem
The first player wins iff the nim-sum .
bool firstPlayerWins(const vector<int>& heaps) {
int x = 0;
for (int h : heaps) x ^= h;
return x != 0;
}Finding a winning move
Let . Find any heap with (at least one exists — the heap whose bit at ‘s highest set bit is 1) and reduce it to . The new nim-sum is 0.
pair<int,int> winningMove(const vector<int>& a) {
int S = 0;
for (int h : a) S ^= h;
if (!S) return {-1, -1};
for (int i = 0; i < (int)a.size(); i++)
if ((a[i] ^ S) < a[i]) return {i, a[i] ^ S}; // heap i, new size
return {-1, -1};
}Why XOR
Two facts prove the theorem:
- From , every move gives . Changing one heap changes the XOR, since when .
- From , some move gives . Take the highest set bit of ; some heap has that bit set, and for it (the bit turns off), so reducing it to is legal.
Terminal position (all heaps empty) has and is losing — which anchors the induction. ∎
Misère Nim
The player who takes the last stone loses.
Play exactly as in normal Nim, unless every heap has size 1. In that case the first player wins iff the number of heaps is even.
bool misereWins(const vector<int>& a) {
int S = 0, big = 0;
for (int h : a) { S ^= h; if (h > 1) big++; }
if (big == 0) return a.size() % 2 == 0; // all heaps are 1
return S != 0;
}The intuition: with a heap of size , the winner can always steer the endgame to leave an odd or even number of 1-heaps as needed, so the normal-play strategy carries over until only 1-heaps remain.
Why Nim is the universal game
Sprague-Grundy: every position in every finite impartial game under normal play is equivalent to a single Nim heap, whose size is that position’s Grundy number. A sum of games is equivalent to a Nim position with those heap sizes.
So solving Nim solves all impartial games — you only have to compute the Grundy values.
The variants
| Variant | Rule | Solution |
|---|---|---|
| Nim | remove any number from one heap | XOR |
| Misère Nim | last stone loses | as above |
| Moore’s Nim | remove from up to heaps | write heaps in binary; losing iff every bit column sums to |
| Staircase Nim | move coins one step down a staircase | XOR of the odd-indexed positions |
| Wythoff’s game | two heaps; remove from one, or equally from both | losing iff |
| Subtraction game | remove stones | Grundy values, often periodic |
| Turning games / Mock Turtles | flip coins | Grundy theory |
| Nim with a move limit | at most per move | |
| Lasker’s Nim | remove, or split a heap in two | has a period-4 pattern |
See Nim Variants for the details.
Nim with a bounded move
If each move removes between 1 and stones, then , so the position is losing iff . This is the “subtraction game” and is the most common Nim variation in contests.
Staircase Nim — the reduction worth knowing
Coins on stairs ; a move slides any number of coins from step to step ; coins on step 0 are dead. This is equivalent to Nim on the odd-indexed steps: moves from odd to even steps are real Nim moves, and moves from even to odd steps can always be mirrored by the opponent.
Many “move tokens toward the end” games reduce to staircase Nim.
See also: Grundy Numbers · Nim Variants · Sprague-Grundy