\[ \let\oldlor\lor \renewcommand{\lor}{\; \oldlor \;} \let\oldland\land \renewcommand{\land}{\; \oldland \;} \renewcommand{\set}[1]{\{\, #1 \,\}} \renewcommand{\given}{\,\mid\,} \renewcommand{\abs}[1]{\lvert #1 \rvert} \renewcommand{\divs}{\!\;\mid\;\!} \renewcommand{\ndivs}{\!\;\nmid\;\!} \renewcommand{\betw}[3][1]{#1 \leq #2 \leq #3} \renewcommand{\mod}[1]{\ (\mathrm{mod}\ #1)} \renewcommand{\floor}[1]{\left \lfloor #1 \right \rfloor} \renewcommand{\ceil}[1]{\left \lceil #1 \right \rceil} \renewcommand{\t}[1]{\texttt{#1}} \renewcommand{\fori}[2][i]{\text{for } #1 = 0, 1, \dots, #2} \renewcommand{\x}[1]{\text{#1}} \renewcommand\concat{\mathbin{+\mkern-10mu+}} \DeclareMathOperator*{\CONCAT}{\concat} \DeclareMathOperator*{\SCC}{\|} \]
UVa 10165 - Stone Game
Time limit: 3.0 seconds
UVa Difficulty rating: 3
View on UVa

This is a classic Game Theory problem. We are told there are two players, Jack and Jim, in a game with N distinct pile(s) of stones, where each pile has Pi stones (i.e., i = 1..N, 1 <= Pi <= 2 * 109). Jack and Jim take turns removing some stones until all have been removed. At that point, the player who removed the last stone is declared the winner. On each turn, the active player chooses one of the remaining piles and removes a number of stones (greater than zero, but not more than the number of stones remaining in the selected pile) from the selected pile only. For each test case, represented by the initial game state, we want to know if Jack will win or lose, assuming both players perform optimally. Jack will always go first. Let's start by noticing an important property that holds true when there are two piles (i.e., N = 2, P1 = P2). Under these conditions, the player who takes his turn 2nd will always win. Consider the following sample input containing three test cases that demonstrate this condition:


2
1 1
2
2 2
2
3 3
0

    

In the first case (i.e., N = 2, P1 = P2 = 1), the game will play out as follows: (1) Jack takes 1 from one of two piles, (2) Jim takes 1 from the last pile, (3) Jim wins. In the second case (i.e., N = 2, P1 = P2 = 2), there are a few more (equally optimal) ways the game can play out, however, the result is the same as the first case. On the first turn, Jack may take one or two stones from one of the piles. If he takes one stone, Jim's optimal move is to take one stone from the opposite pile and thereby creating a game state that is effectively identical to the first case, in which Jim always wins. If, instead, Jack chooses to take two stones from one of the piles, Jim will take two stones from the opposite pile and win the game.

Similarly for the third case. Whenever Jack takes his turn and there are only two piles, each with an equal number of stones, Jim is certain to win. We can extend this idea to game states with more than two piles. If, at the beginning of a player's turn, there are M equal piles remaining, the active player is guaranteed to win when M is odd. If M is even, the passive player will win. This observation alone should help steer you towards a solution for this problem. Try drawing decision trees for small cases and see if you can't spot a pattern. This is the essence of competitive programming! Perhaps you will come to understand the underlying theory of what Charles L. Bouton coined as the "Nim" game. Our observations above are related to what Bouton referred to as safe combinations[1]. It turns out there is a very elegant solution to determining a winner.

For a game with N distinct piles, in which the number of stones in the ith pile is equal to Pi, for i = 1, 2, ..., N, Jack is guaranteed to win the game if the value of P1 ⊕ ... ⊕ PN is non-zero (⊕ is bitwise XOR). Otherwise, Jim (i.e., 2nd player) will win. That's it! I encourage you to check out Bouton's proof, it will really help cement this problem type into your mind and promise you easy points when you see it in competition.

References

  1. http://www.jstor.org/stable/1967631 "Bouton, C. L. (1901). Nim, A Game with a Complete Mathematical Theory. The Annals of Mathematics, 3(1/4), 35. doi:10.2307/1967631"

Links

C++ References

UVa

230 Borrowers
543 Goldbach's Conjecture
900 Brick Wall Patterns
10047 The Monocycle
10140 Prime Distance
10165 Stone Game
10338 Mischievous Children
10394 Twin Primes
10892 LCM Cardinality

CodeForces

1A Theatre Square
4A Watermelon
25A IQ test
50A Domino piling
58A Chat room
59A Word
69A Young Physicist
71A Way Too Long Words
96A Football
112A Petya and Strings
118A String Task
131A cAPS lOCK
158A Next Round
189A Cut Ribbon
230B T-primes
231A Team
263A Beautiful Matrix
281A Word Capitalization
282A Bit++
318A Even Odds
339A Helpful Maths
339B Xenia and Ringroad
455A Boredom
459B Pashmak and Flowers
474B Worms
479A Expression
486A Calculating Function
489B BerSU Ball
489C Given Length and Sum of Digits...
492B Vanya and Lanterns
500A New Year Transportation
507B Amr and Pins
513A Game
520B Two Buttons
550A Two Substrings
580A Kefa and First Steps
742A Arpa's hard exam and Mehrdad's naive cheat
766B Mahmoud and a Triangle
935A Fafa and his Company
977B Two-gram
977D Divide by three, multiply by two
996A Hit the Lottery
1097A Gennady and a Card Game
1108A Two distinct points
1154A Restoring Three Numbers