DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Chopsticks Game

Back to problem

Editorial

States and termination

Let the current player have aaa one-finger hands and ccc two-finger hands, and let the opponent have bbb and ddd, respectively. Set n=a+cn=a+cn=a+c and m=b+dm=b+dm=b+d.

Assign weight 222 to a one-finger hand and weight 111 to a two-finger hand. Every attack decreases the total weight of both players by at least 111, 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

  • If n>mn>mn>m, the current player wins.
  • If m≥n+2m\geq n+2m≥n+2, the current player loses.
  • If n=mn=mn=m, the current player loses exactly when b=mb=m and is odd.

Unequal hand counts

If n>mn>mn>m 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 m≥n+2m\geq n+2m≥n+2, every move leaves the opponent with strictly more hands. The opponent therefore wins.

If m=n+1m=n+1m=n+1, 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 a=na=na=n, b<mb<mb<m, 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.

  • If the opponent has at least two two-finger hands, remove one of them. Another remains.
  • If the opponent has exactly one two-finger hand, the current player has a two-finger hand, and the opponent has a one-finger hand, use the current player's two-finger hand to remove that one-finger hand. The opponent's two-finger hand remains.
  • If all current hands have one finger, remove an opposing two-finger hand. The next player's opponent then has only one-finger hands, which rules out the exceptional win.
  • The remaining case has a single opposing hand. Remove it to win immediately.

Now suppose all opposing hands have one finger. Removing an opposing hand using a two-finger hand wins exactly when aaa is even: the next player is one hand behind with only one-finger hands, and the exception depends on the parity of aaa.

Converting an opposing one-finger hand into a two-finger hand wins exactly when all current hands also have one finger and nnn is even. The next state has equal counts, and the next player's one-finger count is n−1n-1n−1. Together these observations show that the losing cases are exactly those with odd aaa.

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 (a,c,b,d)(a,c,b,d)(a,c,b,d). 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 KKK the larger initial hand count, there are O(K4)O(K^4)O(K4) states. The provided implementation uses an ordered map, taking time and space. Here .

For subtask 2, the first player wins exactly when N>MN>MN>M, or when N=MN=MN=M and NNN is even. Time and space are O(1)O(1)O(1).

For the full problem, substitute n=Nn=Nn=N, m=Mm=Mm=M, a=a1a=a_1a=a1​, 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 O(T)O(T)O(T) total time and O(1)O(1)O(1) extra space. The naive implementation shares its memoization across cases, taking O(T+K4log⁡(K+1))O(T+K^4\log(K+1))O(T+K 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

b=m
aaa
  • If m=n+1m=n+1m=n+1, the current player wins exactly when a=na=na=n, b<mb<mb<m, and bbb is odd.
  • bb
    b
    O(K4log⁡(K+1))O(K^4\log(K+1))O(K4log(K+1))
    O(K4)O(K^4)O(K4)
    K≤10K\leq10K≤10
    b=b1b=b_1
    b=b1​
    O(1)O(1)O(1)
    N+1N+1N+1
    4
    log
    (
    K
    +
    1))
    O(K4)O(K^4)O(K4)