States and termination
Let the current player have one-finger hands and two-finger hands, and let the opponent have and , respectively. Set and .
Assign weight to a one-finger hand and weight to a two-finger hand. Every attack decreases the total weight of both players by at least , so draws are impossible. The following claims can be proved simultaneously by strong induction on that weight. A player with no hands loses; if the opponent has no hands, the game has already been won.
Characterization
Unequal hand counts
If and a hand can be removed immediately, remove one. The next player is then at least two hands behind and loses. If no immediate removal is possible, all hands on both sides have one finger. Convert an opposing hand into a two-finger hand. The opponent has fewer hands and a two-finger hand, so even the exceptional winning condition for being one hand behind is impossible.
If , every move leaves the opponent with strictly more hands. The opponent therefore wins.
If , a conversion leaves the opponent ahead and cannot win. A winning move must remove a hand and create equal counts. For that equal-count position to be losing for the next player, the current player's hands must all have one finger and the next player's number of one-finger hands must be odd. A player whose hands all have one finger can only remove an opposing two-finger hand. Thus the conditions are precisely , , and odd .
Equal hand counts
Suppose the opponent has a two-finger hand. Remove a hand so that the opponent cannot satisfy the exceptional winning condition for being one hand behind.
Now suppose all opposing hands have one finger. Removing an opposing hand using a two-finger hand wins exactly when is even: the next player is one hand behind with only one-finger hands, and the exception depends on the parity of .
Converting an opposing one-finger hand into a two-finger hand wins exactly when all current hands also have one finger and is even. The next state has equal counts, and the next player's one-finger count is . Together these observations show that the losing cases are exactly those with odd .
Do not assume that an opposing two-finger hand should always be removed first. If both players have one hand of each type, the winning attack removes the opponent's one-finger hand using your two-finger hand.
Subtasks and implementation
For subtask 1, memoize the game state . There are at most four attack types. Swap the players' roles after each attack; a state is winning if any move reaches a losing state. Hand counts never increase. With the larger initial hand count, there are states. The provided implementation uses an ordered map, taking time and space. Here .
For subtask 2, the first player wins exactly when , or when and is even. Time and space are .
For the full problem, substitute , , , and into the characterization. Time and space are . Use 64-bit integers for the input and the calculation of .
Process the test cases independently. The full solution takes total time and extra space. The naive implementation shares its memoization across cases, taking total time and space. No bound on the sum of hand counts is needed because the counts are never expanded into individual hands.
Solution written by GPT