WARNING: The text below provides a guidance for a Project Euler problem.

• If you pride yourself in tackling problems entirely on your own, you are encouraged to exit this post right now.

• If you are simply looking for a hint for the problem, please visit the »Project Euler’s official forum«. You may find some hints here, but if you don’t want your problem spoiled, scroll cautiously.

• If you are looking for a readily available copy/paste answer, you will not find it here. All of the code snippets in the post have been stripped of some crucial lines (these are clearly marked). It is my aim that anyone able to replicate the answer based on such snippets will have to understand the solution conceptually. At that point, by virtue of learning something new, I believe he deserves it anyway.

• Guidance for future problems will not be published before 100 people have solved the problem and there are at least two more recent problems.



Scatterstone Nim

The problem tasks us with finding the number of winning positions in a certain variation of »Nim« defined by parameters $n, k$. We will, in the beginning, focus on one particular given pair $n, k$. To reiterate, the game goes like this:

• Two players start the game with some number of piles of $n$ stones in total.
• They take turns, where they pick one pile and split it into $p\ (2 \leq p \leq k)$ nonzero piles.
• Whoever doesn’t have any valid move anymore, loses.

This being a combinatorial impartial game, the »Sprague-Grundy theorem« guarantees that we will be able to analyse it with »Grundy numbers (nimbers)«. Consider the set $T$ of (sorted) tuples $t = (t_1, t_2, \dots, t_k)$ with $\sum_{i=1}^k t_i = n$; these will represent all possible positions in our game ($t_i$ is the size of $i$-th pile). Each position $t$ will be associated with its Grundy number, which is standardly defined recursively as:

The $\text{mex}$ in the above expression stands for the minimal excludant: $\text{mex}\ S := \min \{i \not\in S\ \mid\ i = 0, 1, 2, \dots\}$. The standard combinatorial game theory tells us that position $t$ will be losing if and only if $\mathcal G(t) = 0$, and moreover, we can understand playing the position $t$ as playing the games in positions $(t_1), (t_1), \dots, (t_k)$ simultaneously. The nimber addidion gives us the relation ($\oplus$ denotes the logical xor):

The calculation of Grundy numbers using $(1)$ is quite expensive, but the above tells us that we may find any $\mathcal G(t)$ readily just by precalculating all $\mathcal G((i))$ for $i = 1, \dots, n$. For the precalculation of these singleton positions, we shall use $(1)$. But before dwelling unnecessarily into brute force, there are helpful simplifications worth noting:

Theorem: If $k = 2$, then $\mathcal G((i)) = (i + 1) \text{ mod } 2$.

Proof. Comes from the fact that we can split an even pile only into two piles of equal parity, and that we can split an odd pile only into two piles of distinct parity.

Theorem: If $k \ge 4$, then $\mathcal G((i)) = i - 1$.

Proof. The claim holds for $i = 1$, since $\mathcal G(1) = 0$. We will now show by induction that if we have a pile of size $i$, positions with Grundy numbers $j = 0, 1, \dots, i - 2$ are reachable. In particular, we may perform splits (for any valid $r \ge 1$):

Thus the only case for $k$ where we actually need to compute the Grundy numbers is $k = 3$. In that case, letting $P_k(n)$ denote the set of integer partitions of $n$ of cardinality $k$, we use:

Once we have computed all Grundy numbers, it will be quite easy to write a recursive function to count the number $f(n, k)$ of winning positions; these are just the ones with Grundy number $> 0$. Specifically, we are looking for the number of integer partitions of $n$ such that the xor sum of the Grundy numbers of components is nonzero.

This piece of Python code returns the answer in about 9 seconds when run using PyPy.