How to use grundy numbers in this SPOJ problem?

85 Views Asked by At

I read yesterday this question https://www.spoj.com/problems/GAME3/ and I realise that this problem looks like Nim game and by that similar method of solving can be used.
I do this:
Let's take example with $N=3$. I want to use Grundy numbers because if the grundy numbers of some state is zero the position is losing. $G(n)$ is mex of all grundy number of position that I can go to.
Because of that $G(3)=0$.
$G(2)=mex(G(3))=1$
$G(1)=mex(G(2))=0$
But Ivica the first player won that game?

1

There are 1 best solutions below

0
On

With $N=3$, I (move 1) goes to $2$ (both types of moves are equivalent) and move two goes to $3$ ($4$ is disallowed) so the player making that move (Marica) loses and Ivica wins. This is accordance with the output.

$G(3)$ is irrelevant, we cannot reach $3$ as the game is then over, and so $G(2)=0$ by definition: the first mover loses.

So $G(1) = \textrm{mex}(G(2)) = 1$, a first player win. So the base case is at $G(N-1)$, as the only legal move loses, it always has value $0$.