Let nim[n]
be the thread of a sequence of n
units. Then we have the following recurrence relation:
nim[n] = mex{nim[i] xor nim[j]: i, j >= 0, i + j < n}
(see Sprague-Grundy theorem for a general theory.)
From this repetition relation, you can try to calculate the first few members of the nim
sequence, and you will find that it looks like nim[n] = n
.
Then the evidence can easily be deduced, which I will leave to you.
Thus, a sequence of n
consecutive units is actually equivalent to a Nimes heap of size n
. Now itβs easy to find the result of any game.
For example, if we have 0101110
, then it is equivalent to two Nim piles of size 1
and 3
respectively. Therefore, the total nimber is 1 xor 3 = 2
, which is nonzero, so the first player wins.
As another example, if we have 1110011111000111111
, then the full nimber is 3 xor 5 xor 6 = 0
, so the first player loses.
EDIT: Regarding your question about changing piles, here are some more explanations.
Although the number of piles will change, the key point is as follows:
- If the status is zero, any valid move should change it to a non-zero nimber status.
- If the status has a nonzero nimber, then there must be a valid move that changes it to a zero state.
Thus, for the winner, the strategy is to leave the opponent in a zero state.
Example: start with the sequence 1110011111000111111
, which I denote by 3, 5, 6
for brevity.
If the first player replaces 6
with the sums of 2
and 3
, then the second player will face the status 3, 5, 1, 4
, which has nimber 3 xor 5 xor 2 xor 3 = 7
. To keep the value zero, the second player will replace 5
with 2
, leaving the first player 3, 2, 2, 3
, who again has zero.
The procedure continues until there is a valid movement for the first player.