# Stone games again

Two players are playing a stone-picking game.

There are some piles of stones. The number of stones in each pile is given.

Every player takes action in turns as following rules:

• The one in his turn chooses a pile of stones. Let's denote the number as A.
• Then the same player chooses a number B with 0<2*B<=A.
• 2B stones are moved from the original pile,
• and a new pile with A-B stones is added.

Then one who can not take a action as above rules describe will lose the game.

Suppose everyone is clever enough. Determine whether the first one or the second one will win.

This problem is a very common stone game. The common solution is to calculate the Sprague-Grundy function value as the definition and then find some rules. After this try to prove the rules holds for all situations.

My problem is that the SG function values in small cases seem very irregular. Can anyone help me? Or if there is other solution?

sg = 0, sg = 0, sg = 1, sg = 0

sg = 0, sg = 1, sg = 2, sg = 2

sg = 1, sg = 0, sg = 0, sg = 1

sg = 0, sg = 0, sg = 1, sg = 2

sg = 2, sg = 1, sg = 4, sg = 4

sg = 1, sg = 4, sg = 4, sg = 1

sg = 2, sg = 2, sg = 1, sg = 0

sg = 0, sg = 1, sg = 0, sg = 0

sg = 1, sg = 2, sg = 2, sg = 1

sg = 0, sg = 0, sg = 1, sg = 0

sg = 0, sg = 1, sg = 2, sg = 2

sg = 1, sg = 4, sg = 4, sg = 1

sg = 4, sg = 4, sg = 1, sg = 2

sg = 2, sg = 1, sg = 7, sg = 7

sg = 1, sg = 7, sg = 7, sg = 1

sg = 2, sg = 2, sg = 1, sg = 7

sg = 7, sg = 1, sg = 7, sg = 7

sg = 1, sg = 2, sg = 2, sg = 1

sg = 4, sg = 4, sg = 1, sg = 4

sg = 4, sg = 1, sg = 2, sg = 2

sg = 1, sg = 0, sg = 0, sg = 1

sg = 0, sg = 0, sg = 1, sg = 2

sg = 2, sg = 1, sg = 0, sg = 0

sg = 1, sg = 0, sg = 0, sg = 1

sg = 2, sg = 2, sg = 1, sg = 4

• @WillieWong sorry, I forget it. The criterion is added. – konjac May 14 '12 at 10:56
• @WillieWong zero is not allowed, otherwise the game maybe never ends. – konjac May 14 '12 at 11:04

The SG-function is defined by $SG(n) := mex\{SG(n-2m) \stackrel{*}{+} SG(n-m)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These are the first $486$ values. The next number is, unfortunately, $11$. Anyway, we can observe the following pattern:

• Every row seems to have the form $x x 1 x x 1 2 2 1$.
• Every $3k$th has $x=4$.
• The other adjacent rows coincide.

If this is true, the whole function is determined by the values $a(k) := SG(9(3k+1)+1)$, which also follow a nice pattern when we fix $k$ mod $3$:

0  0  7
0  0  7
8  8  7
0  0  7
0  0  7
8  8  7
11 11 7
11 11 7
8  8  7
0  0  7
0  0  7
8  8  7
0  0  7
0  0  7
8  8  7
11 11 7
11 11 7
8  8  7
13 13 7
13 13 7
8  8  7
13 13 7
13 13 7
8  8  7
11 11 7
11 11 7
8  8  7
0  0  7
0  0  7
8  8  7
0  0  7
0  0  7
8  8  7
11 11 7
11 11 7
8  8  7
0  0  7
0  0  7
8  8  7
0  0  7
0  0  7
8  8  7
11 11 7
11 11 7
8  8  7
13 13 7
13 13 7
8  8  7
13 13 7
13 13 7
8  8  7
11 11 7
11 11 7
8  8  7
14 14 7
14 14 7
8  8  7
14 14 7
14 14 7
8  8  7
11 11 7


The pattern is similar to the one above: Every row has the form $x x 7$, where every third $x$ is $8$, and the other adjacent ones coincide. Now if this is also true, $a$ only depends on the values $a(3(3j+1)+1)$, which again satisfy a similar pattern (the rows now have the form $x x 11$, and every third $x$ is $13$). I don't know if this kind of analysis will solve the game, but it already shows that the SG-function contains a lot of "nested" periodicity. And perhaps this already suffices to determine when $SG(n)=0$, i.e. when the second player has a winning strategy.