How to Win Moore’s Nim k – Strategies and Algorithms

algorithmsbinarycombinatorial-game-theory

Nim is a game where there are several heaps of pebbles. Players take turns selecting a heap, then removing any nonzero number of pebbles from that heap. Whoever takes the last pebble wins.

In Nim, the winning strategy (framed with an eye toward finding the Nim$_k$ analog) is…

  1. Convert all heap sizes to base $2$.

  2. Without modifying their digits, treat those base $2$ heap sizes as base $2$ (a.k.a., do nothing, this is a placeholder step for portability to Nim$_k$).

  3. Sum (mod $2$) over all those base $2$ heap sizes. Call the result $X$.

  4. Select $1$ heap such that the sum (mod $2$) of its base $2$ heap size and $X$ is less than its base $2$ heap size.

  5. Remove objects from the selected heap such that the sum (mod $2$) of its base $2$ heap size and $X$ equals its original base $2$ heap size.

Henry Moore proposed a variant of Nim called Nim$_k$, for some positive integer $k$. In the variant, during each turn the players select up to $k$ heaps, then remove some pebbles from each of the selected heap (you can remove a different number of pebbles from each heap).

I am wondering if the above strategy for Nim can be modified to work for Nim$_k$. I tried to translate the above steps to a Nim$_k$ setting. This is what I got:

  1. Convert all heap sizes to base $2$.

  2. Without modifying their digits, treat those base $2$ heap sizes as base $k + 1$.

  3. Sum (mod $k + 1$) over all those base $k + 1$ heap sizes. Call the result $X$.

  4. ????

  5. ?????

When I played Nim$_2$ here, I got the heaps $15$, $3$, $8$, and $18$. I converted to $01111_2$, $00011_2$, $01000_2$, and $10010_2$, then modified the base to $01111_3$, $00011_3$, $01000_3$, and $10010_3$, and finally summed these (mod $3$) to $12102$. I'm not sure if I'm supposed to treat this as $12102_3$ or $12102_2$, or how to use it to win the game.

How can I generalize Step 4 and Step 5 and complete this algorithm?

Best Answer

I’ll call the sum of the binary pile sizes performed mod $k+1$ in each digit the “$k$-sum” for short.

The task is to show that if the $k$-sum is non-zero we can always reduce at most $k$ piles such that the $k$-sum becomes $0$, and conversely if the $k$-sum is $0$ it’s impossible to reduce at most $k$ piles and leave the $k$-sum unchanged.

To get a feel for how this works, recall how it works for Nim$_1$ (a.k.a. Nim). The $1$-sum is the XOR of all the binary pile sizes. There is at least one pile that has a $1$ in the highest digit in which the $1$-sum has a $1$. Form the XOR of that pile’s size with the $1$-sum to determine how many stones to leave in it. Since these two numbers both have a $1$ in the highest digit, the resulting number has a $0$ in that digit and is thus less than the current number, so it can indeed be achieved by taking away some number of stones.

Now for Nim$_k$. We will select up to $k$ piles to take stones from and construct the number of stones they should be left with as we go along. Let $d$ be the highest non-zero digit in the $k$-sum. Select any $d$ piles that have a $1$ in that digit. (They must exist, since the sum has $d$ in that digit.) Set that digit to $0$ in the binary sizes of those piles. Now the $k$-sum has a $0$ in that digit. Note that in the following we can modify the remaining binary digits of the selected piles in whatever way we want, since the resulting size will always be less than the current size because it lacks the $1$ in the highest digit. (This is the same argument as the one for Nim above.)

Let the next lower digit in the $k$-sum be $e$. Say $m$ of the $d$ piles we selected have a $0$ in this digit and $n=d-m$ have a $1$. Then by modifying these digits we can deal with $e\in[-m,n]\bmod k+1$, since we can add up to $m$ $1$s or subtract up to $n$ $1$s. To deal with the remaining cases with $e\in[n+1,k-m]$, we need to select $e-n$ further piles that have a $1$ in the present digit. Since $m+n=d$ and $e\le k-m$, we have $d+e-n\le k$, so we’re allowed to select $e-n$ further piles; also, since the present digit is $e$, there must be at least $e$ piles with a $1$ in this digit, and since that includes only $n$ of the piles selected so far, there must be at least $e-n$ left to select. Now set the present digit to $0$ in the $n$ previously selected and the $e-n$ newly selected piles, thus bringing this digit to $0$ in the $k$-sum. Again, note that in the following we can modify the remaining digits of the newly selected piles in whatever way we want, since the resulting size will always be less than the current size because it lacks the $1$ in the present digit. That may not be the highest digit for these piles, but the higher digits remain unchanged.

We can continue like this, descending through the digits. In each step, we’ve already selected $d\le k$ piles whose remaining digits we can modify arbitrarily; this allows us to deal with $d+1$ cases of the present digit, and the remaining $k-d$ cases can be dealt with by selecting an admissible number of new piles that have $1$ in the present digit.

Note that if the $k$-sum is already $0$ to begin with, the algorithm would lead us to select $0$ piles, and indeed any modification of up to $k$ piles will change the $k$-sum, since the highest digit modified can only be changed from a $1$ to a $0$ (otherwise the pile size would increase), and changing up to $k$ $1$s to $0$s changes the $k$-sum in that digit.

Let’s apply this algorithm to the given example. We have $k=2$ and the pile sizes $01111_2$, $00011_2$, $01000_2$ and $10010_2$. The $2$-sum is $12102_3$. The highest digit is $1$, so we need to select one pile with a $1$ in that digit. There’s only one to select, namely $10010_2$. We set its highest digit to $0$, yielding $00010_2$.

For the next digit, we have $m=1$ and $n=0$, so we can deal with $e\in[-1,0]\bmod3$ using the one pile selected so far. Indeed the next digit is $e=2\equiv-1\bmod3$, so we set this digit to $1$ in the selected pile, yielding $01010_2$. Now there are three $1$s in this digit, so the $2$-sum is $0$ in this digit as required.

For the next digit, we again have $m=1$ and $n=0$, so we could again deal with $e\in[-1,0]\bmod3$, but now we have $e=1$, so we need to select another pile that has a $1$ in this digit. Again, there’s only one choice, $01111_2$. (The reason we only have one choice in each case is that those digits didn’t “wrap around” in the $2$-sum, so the number of piles with a $1$ is exactly the value of the digit.) We set the present digit to $0$ in the newly selected pile, yielding $01011_2$ and bringing the present digit to $0$ in the $2$-sum. So we’ve now selected $2$ piles, as many as we’re allowed to select, and we’ve changed their sizes to $01010_2$ and $01011_2$, respectively. From now on, we can deal with all cases by further modifying the piles already selected.

The next digit in the $2$-sum is already $0$, so no modification required there.

The last digit is $2$, so we have to either turn one $0$ into a $1$ or two $1$s into $0$s; exactly one of these must be possible. Indeed, we have a $0$ in $01010_2$; we set it to $1$, yielding $01011_2$ and bringing the final digit in the $2$-sum to $0$.

The end result is that we’ve modified $10010_2$ to $01011_2$ and $01111_2$ to $01011_2$, so we need to take $18-11=7$ and $15-11=4$ stones from those piles, respectively. The resulting pile sizes are $01011_2$, $00011_2$, $01000_2$ and $01011_2$, and their $2$-sum is $0$ as required.

Related Question