Learn / Library / Brainteasers
Uneven coin stacks
There are stacks of coins of sizes , , and . players alternate; a move takes any positive number of coins from exactly one stack. The player who takes the last coin wins. You move first and both players play optimally. Exactly one first move guarantees you a win. How many coins does that move take?
Checking an answer needs JavaScript. The hint and the solution open without it.
Hint
Compute the XOR of the sizes and ask which stack can be cut down to make it .
Solution
- A position in this game is lost for the player to move exactly when the XOR of the stack sizes is .
- Step by step, XOR is , then XOR is , then XOR is .
- A stack of size can be reduced to XOR only if that is smaller than ; this needs the leading bit of , which is , and only has it.
- So the stack of goes down to XOR , which is , and the XOR of , , and is then .
- The winning move takes coins, so the answer is .
Answer: