A pile of stones and two players take turns to get them to zero

439 Views Asked by At

So we have $n$ stones and a set $S$ that has given positive natural numbers. Two players take stones from the $n$ stone pile and the one who takes the last stones wins the game. We need to find an algorithm that will tell us who wins it.

According to Wikipedia it says that the one who has the turn and $(n \text{ mod } (k+1) ) = 0$ holds has the winning strategy (where $k$ is a number from set $S$ if I understood correctly). However on this site https://open.kattis.com/problems/bachetsgame it is said that for some of the inputs where the above would hold, the contrary is the solution.

Can somebody explain where I am missing my understanding about the nim game.

My idea for this problem was that I would use the statement from Wikipedia to loop through the elements in the set $S$, if it would hold for any of them. If it would hold, then I could instantly stop the loop and say that the first player wins, because he has the winning strategy. If it would not hold for any of the elements in the set $S$, then that would mean that the player 2 has the winning strategy. However my thinking looks like it is not correct... (maybe somebody can correct me, where I did make a mistake)