Shuriken Game
Time limit1sMemory limit128 MB
Two players remove 1 to N shurikens from a pile, but a player cannot repeat the opponent's previous move; find the smallest winning first move.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Game theory, Implementation
- Solved
- No attempts yet
Problem
Being a ninja means spending a lot of time training. To pass the time between training sessions, ninjas like to play games with their shurikens (a shuriken is a metal star with sharp corners that ninjas throw at enemies).
This is a game for two players featuring a single stack of shurikens. The players take turns. On a turn, a player removes some shurikens from the stack: at least and at most . The player who takes the last shuriken(s) wins.
The basic game was quickly solved, so an extra rule was added to make it interesting again: a player may not take the same number of shurikens the opponent just took (you cannot copy your opponent's last move). If the stack holds exactly shuriken and the opponent has just taken , the player to move has no legal move and loses.
For a given situation, determine how the player to move can win.
Input
The first line contains the number of test cases . Each test case has the following format:
- One line with three integers , , and (, , ): the number of shurikens in the stack, the maximum number of shurikens a player may take, and the number the opponent took on the last move, respectively.
Output
For each test case, print one integer on its own line: the smallest number of shurikens the player to move can take to secure the win. If there is no winning move, print .