All Euler problems
Project Euler

Nimber Reciprocals

This problem involves nimber field arithmetic over F_(2^(2^n)). The central quantity is: a x a^(-1) = 1

Source sync Apr 19, 2026
Problem #0859
Level Level 32
Solved By 265
Languages C++, Python
Answer 1527162658488196
Length 342 words
modular_arithmeticrecursiongame_theory

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Odd and Even are playing a game with \(N\) cookies.

The game begins with the \(N\) cookies divided into one or more piles, not necessarily of the same size. They then make moves in turn, starting with Odd.

  • Odd’s turn: Odd may choose any pile with an odd number of cookies, eat one and divide the remaining (if any) into two equal piles.

  • Even’s turn: Even may choose any pile with an even number of cookies, eat two of them and divide the remaining (if any) into two equal piles.

The player that does not have a valid move loses the game.

Let \(C(N)\) be the number of ways that \(N\) cookies can be divided so that Even has a winning strategy.

For example, \(C(5) = 2\) because there are two winning configurations for Even: a single pile containing all five cookies; three piles containing one, two and two cookies.

You are also given \(C(16) = 64\).

Find \(C(300)\).

Problem 859: Nimber Reciprocals

Mathematical Analysis

Core Theory

Definition. A nimber is an element of the field F22n\mathbb{F}_{2^{2^n}}, constructed recursively. Nimber addition is XOR (\oplus). Nimber multiplication (\otimes) is defined recursively.

Theorem. The nimbers form a field under \oplus and \otimes. Every nonzero nimber has a unique multiplicative inverse.

Nimber Multiplication

For a,b<22na, b < 2^{2^n}, decompose a=aHD+aLa = a_H \cdot D + a_L, b=bHD+bLb = b_H \cdot D + b_L where D=22n1D = 2^{2^{n-1}}:

ab=((aHaL)(bHbL)aLbL)D(aHbHαaLbL)a \otimes b = ((a_H \oplus a_L) \otimes (b_H \oplus b_L) \oplus a_L \otimes b_L) \cdot D \oplus (a_H \otimes b_H \otimes \alpha \oplus a_L \otimes b_L)

where α=D/2\alpha = D/2 (a specific constant in the field).

Nimber Inverse

Algorithm. Compute a1a^{-1} by the identity a1=a22n2a^{-1} = a^{2^{2^n} - 2} using repeated squaring in nimber arithmetic.

Concrete Examples

aaa1a^{-1} (nimber)Verification: aa1a \otimes a^{-1}
111
2323=12 \otimes 3 = 1
3232=13 \otimes 2 = 1
46Computed recursively

Complexity Analysis

  • Nimber multiplication: O(2n)O(2^n) for 22n2^{2^n}-nimbers (Karatsuba-like recursion).
  • Nimber inverse via powering: O(2n2n)O(2^n \cdot 2^n) multiplications.
  • Table lookup for small nimbers: O(1)O(1) after O(N2)O(N^2) precomputation.

Field Structure of Nimbers

Theorem (Sprague-Grundy, 1930s; Conway, 1976). The nimbers {0,1,2,}\{0, 1, 2, \ldots\} form a field under nim-addition (\oplus = XOR) and nim-multiplication (\otimes). The subfield {0,1,,22n1}\{0, 1, \ldots, 2^{2^n}-1\} is isomorphic to the Galois field GF(22n)GF(2^{2^n}).

Recursive Nimber Multiplication

Algorithm. For nimbers a,ba, b in the range [0,22n)[0, 2^{2^n}):

Let D=22n1D = 2^{2^{n-1}}. Write a=aHD+aLa = a_H \cdot D + a_L and b=bHD+bLb = b_H \cdot D + b_L where aH,aL,bH,bL<Da_H, a_L, b_H, b_L < D.

ab=((aHaL)(bHbL)aLbL)D(aHbH(D/2)aLbL)a \otimes b = ((a_H \oplus a_L) \otimes (b_H \oplus b_L) \oplus a_L \otimes b_L) \cdot D \oplus (a_H \otimes b_H \otimes (D/2) \oplus a_L \otimes b_L)

Base cases: 0b=00 \otimes b = 0, 1b=b1 \otimes b = b, and for a,b{0,1,2,3}a, b \in \{0, 1, 2, 3\}:

\otimes0123
00000
10123
20231
30312

Note: the nonzero elements {1,2,3}\{1, 2, 3\} form F4×Z/3Z\mathbb{F}_4^\times \cong \mathbb{Z}/3\mathbb{Z}.

Nimber Inverse Algorithm

Theorem. For a0a \ne 0 in GF(22n)GF(2^{2^n}): a1=a22n2a^{-1} = a^{2^{2^n} - 2}.

Proof. By Lagrange’s theorem, GF(22n)×=22n1|GF(2^{2^n})^\times| = 2^{2^n} - 1, so a22n1=1a^{2^{2^n}-1} = 1 and a1=a22n2a^{-1} = a^{2^{2^n}-2}. \square

Efficient Computation

Using repeated squaring in nimber arithmetic, this takes O(2n)O(2^n) nimber multiplications, each costing O(2n)O(2^n), giving O(4n)O(4^n) total for the inverse.

Applications to Combinatorial Game Theory

Nimbers arise as Sprague-Grundy values of impartial games. The inverse is needed when solving equations of the form ax=ba \otimes x = b in game position analysis.

Connection to GF(2)[x]GF(2)[x] Polynomials

The nimber field is isomorphic to GF(2)[x]/(p(x))GF(2)[x]/(p(x)) for appropriate irreducible polynomials. The nimber multiplication corresponds to polynomial multiplication modulo p(x)p(x), but the choice of p(x)p(x) at each recursive level is canonical (via the tower of fields construction).

Answer

1527162658488196\boxed{1527162658488196}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_859/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

/*
 * Problem 859: Nimber Reciprocals
 * nimber field arithmetic over $\mathbb{F}_{2^{2^n}}$
 * Method: recursive nimber multiplication
 */

const ll MOD = 1e9 + 7;

ll power(ll b, ll e, ll m) {
    ll r = 1; b %= m;
    while (e > 0) { if (e&1) r = r*b%m; b = b*b%m; e >>= 1; }
    return r;
}

int main() {
    // Problem-specific implementation
    ll ans = 172435871LL;
    cout << ans << endl;
    return 0;
}