The Stones Game
Time limit1sMemory limit128 MB
Given N stones and M players in cyclic order with forced removals, decide if player X has a strategy that always takes the last stone.
- Level
Medium7 of 10
- Topics
- Game theory, Math
- Solved
- No attempts yet
Problem
The stones game has simple rules and it is very old.
The game starts with stones and players. The players are numbered from to . Player takes the first turn, then player , and so on up to player . After player finishes a turn, player goes again, and the game repeats this order until it ends.
Each turn consists of the following two steps.
- The player whose turn it is gets a chance to remove one stone. If the player decides to remove a stone, exactly one stone leaves the pile in this step.
- This step happens regardless of the decision made in step 1. If this is not the first turn and the player of the previous turn decided not to remove a stone in step 1, then the current player must remove one stone in this step. If the player of the previous turn did remove a stone in step 1, then the current player must not remove a stone in this step.
So a player removes 0, 1 or 2 stones during a single turn, depending on the rules above. The player who removes the last stone wins the game.
You are given the number of stones, the number of players, and one player number. Decide whether that player has a strategy that always wins, whatever the other players do on their turns.
Input
Your program is tested on one or more test cases. The first line of the input has a single integer , the number of test cases (). Each of the next lines describes one test case and holds three integers separated by a single space, , , (, ), the number of stones, the number of players, and the player number.
Output
For each test case, print a single word on its own line. Print YES if player has a strategy that always wins regardless of what the other players do, and NO otherwise.