Nim
Nim is a two-player combinatorial game played with heaps of stones under the normal play convention: players alternate turns removing any positive number of stones from a single heap, and the playe...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Nim is a game played with heaps of stones, where two players take it in turn to remove any number of stones from any heap until no stones remain.
We’ll consider the three-heap normal-play version of Nim, which works as follows:
-
At the start of the game there are three heaps of stones.
-
On each player’s turn, the player may remove any positive number of stones from any single heap.
-
The first player unable to move (because no stones remain) loses.
If \((n_1, n_2, n_3)\) indicates a Nim position consisting of heaps of size \(n_1\), \(n_2\) and \(n_3\) then there is a simple function, which you may look up or attempt to deduce for yourself, \(X(n_1, n_2, n_3)\) that returns:
-
zero if, with perfect strategy, the player about to move will eventually lose; or
-
non-zero if, with perfect strategy, the player about to move will eventually win.
For example \(X(1, 2, 3) = 0\) because, no matter what the current player does, the opponent can respond with a move that leaves two heaps of equal size, at which point every move by the current player can be mirrored by the opponent until no stones remain; so the current player loses. To illustrate:
-
current player moves to \((1, 2, 1)\)
-
opponent moves to \((1, 0, 1)\)
-
current player moves to \((0, 0, 1)\)
-
opponent moves to \((0, 0, 0)\), and so wins.
For how many positive integer \(n \leq 2^{30}\) does \(X(n, 2n, 3n) = 0\)?
Problem 301: Nim
Mathematical Development
Definition. For a non-negative integer , let denote its binary representation, where and .
Lemma 1 (Carry-free addition). For non-negative integers and , the identity holds if and only if , where denotes bitwise AND.
Proof. Write and with . Bitwise XOR computes columnwise addition modulo 2 without propagating carries: . The ordinary sum satisfies only when no carry is generated, which occurs precisely when for all , i.e., for all . This is equivalent to .
Theorem 1 (Characterization of losing positions). if and only if has no two consecutive 1-bits in its binary representation.
Proof. Since , the condition is equivalent to . By Lemma 1, this holds if and only if .
Now observe that multiplication by 2 corresponds to a left shift by one bit: bit of equals bit of . Therefore
where . This vanishes if and only if for all , which is precisely the condition that no two consecutive bits of are both 1.
Lemma 2 (Fibonacci counting). Let denote the number of binary strings of length (including leading zeros) with no two consecutive 1-bits. Then , where is the Fibonacci sequence defined by and for .
Proof. We proceed by strong induction. A valid -bit string either:
- begins with : the remaining bits form any valid -bit string, yielding choices; or
- begins with : the second bit must be (to avoid consecutive 1s), and the remaining bits form any valid -bit string, yielding choices.
Hence for . The base cases are (strings ) and (strings ). Since and , the recurrence and initial conditions match, giving by induction.
Corollary. The number of non-negative integers in with no two consecutive 1-bits is .
Proof. The integers in correspond bijectively to -bit strings (with leading zeros). Apply Lemma 2.
Theorem 2 (Main result). The number of positive integers satisfying is .
Proof. By the Corollary, the qualifying integers in number . Excluding yields qualifying positive integers below . The integer has binary representation , which trivially has no two consecutive 1-bits. Including it yields .
Editorial
The position (n, 2n, 3n) is a P-position iff n XOR 2n XOR 3n = 0, which holds iff n has no two consecutive 1-bits in binary. The count of such n in [1, 2^30] equals the Fibonacci number F(32).
Pseudocode
Set a <- 1; b <- 1 // F(1) = F(2) = 1
For i from 3 to 32:
(a, b) <- (b, a + b) // advance Fibonacci pair
Return b // F(32)
Complexity Analysis
- Time: — computing requires exactly 30 additions of bounded integers.
- Space: — two integer variables suffice.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <cstdio>
/*
* Problem 301: Nim
*
* (n, 2n, 3n) is a P-position iff n XOR 2n XOR 3n = 0,
* equivalently n has no two consecutive 1-bits in binary.
* Count of such n in [1, 2^30] = F(32) = 2178309.
*/
int main() {
long long a = 1, b = 1;
for (int i = 3; i <= 32; i++) {
long long c = a + b;
a = b;
b = c;
}
printf("%lld\n", b);
return 0;
}
"""
Problem 301: Nim
The position (n, 2n, 3n) is a P-position iff n XOR 2n XOR 3n = 0,
which holds iff n has no two consecutive 1-bits in binary.
The count of such n in [1, 2^30] equals the Fibonacci number F(32).
"""
def solve():
a, b = 1, 1
for _ in range(30):
a, b = b, a + b
print(b)
solve()