Understanding Nim

394 Views Asked by At

I was going through a problem where we have given $k$ piles from $1$ to $k$ and each pile contains some stones. Now, there is a game in which there are $2$ players, say $A$ and $B$ who removes stones from piles one by one and who removes stone(s) at the end, wins the game and here, restriction is : for each pile, maximum number of stones he/she can remove is given and minimum number of stones he/she can remove is also given which is $1$. For example, there are $2$ piles, $1$ and $2$, from pile $1$, a person can remove maximum $1$ stone and from pile $2$, a person can remove maximum $2$ stones. Pile $1$ contains total $3$ stones and pile $2$ contains $2$ stones. Now pair (pile number, number of stones removed) of moves between players can be (1, 1) (2, 2) (1, 1) (1, 1). So, here, player $1$ lost the game. I was applying the approach which is given here means player $2$ will try to make $(a+b)$ as $1+2 = 3$ and at the end player $1$ will be remaining, So, he will lost the game. Is my approach Correct ?
I have tried few examples with the approach given here but I didn't get the logic of $(a+b)$. Why he has chosen $(a+b)$ i.e. min+max, not any other value like $(a+1),(a+3)$ etc. Could anyone please help me.
Thank you.

1

There are 1 best solutions below

18
On BEST ANSWER

First, consider a game with only one pile.

If you are allowed to take from $1$ through $s$ stones from the pile, and you leave $s+1$ stones, you have won, for your opponent can't pick up all the stones, and he must reduce the number of stones to $s$ or less, and then you pick up all remaining stones. Similarly, if you leave $2(s+1)$ stones, then whatever your opponent does, you can get to $s+1$ stones, and win. The winning positions are of the form $c(s+1),\ c=0,1,2,\dots$.

It's not hard to show that the Grundy number of a pile with $k$ stones is the remainder of $k$ on division by $s+1$.

To evaluate the Grundy number of a position with multiple piles, take the nim sum of the Grundy numbers of the piles, as in nim.