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

Pebble Game

Back to problem

Editorial

Define a game value g(x)g(x)g(x) for a pile of size xxx as follows.

  • If x mod 6x\bmod6xmod6 is 111 or 222, then g(x)=0g(x)=0g(x)=0.
  • Otherwise, let t=⌊(x−3)/6⌋t=\lfloor(x-3)/6\rfloort=⌊(x−3)/6⌋. Count the consecutive nonzero digits at the end of the base-three representation of ttt, stopping at the first zero digit. If the count is ccc, then g(x)=c+1g(x)=c+1. In particular, gives .

The answer is Second if g(A1)⊕⋯⊕g(AN)=0g(A_1)\oplus\cdots\oplus g(A_N)=0g(A1​)⊕⋯⊕g(AN​)=, and otherwise.

The current player chooses a partition into three positive parts, while the opponent chooses which part survives. The current player can force a pile's game value to become hhh exactly when a partition exists in which all three parts have value hhh. Hence define

S(x)={h∣a+b+c=x, a,b,c≥1, g(a)=g(b)=g(c)=h}.S(x)=\{h\mid a+b+c=x,\ a,b,c\ge1,\ g(a)=g(b)=g(c)=h\}.S(x)={h∣a+b+c=

The stated formula satisfies g(x)=mex⁡S(x)g(x)=\operatorname{mex}S(x)g(x)=mexS(x): if g(x)=kg(x)=kg(x)=k, then every value h<kh<kh can be forced, but itself cannot. To see this, write with . When all three parts have positive game value , write them as with . Their indices must satisfy or . If the last ternary digits of are nonzero and the next digit is zero, neither target can be represented by three indices of the same type. For any smaller , a representation can be constructed digit by digit. The case follows by using three parts congruent to or modulo . If , no three value-zero parts can sum to .

Now let XXX be the XOR of the values of all piles. If X≠0X\ne0X=0, choose a pile whose value contains the highest set bit of XXX. The other piles' XOR is smaller than this pile's value and can be forced using a suitable three-way split. Thus the opponent must move to a position with XOR zero. If X=0X=0X=, no split can make all three choices retain the old value, so the opponent can always choose a pile that leaves a nonzero XOR. Therefore, the XOR rule determines the winner.

The time complexity is O(∑Nlog⁡3max⁡Ai)O(\sum N\log_3\max A_i)O(∑Nlog3​maxAi​), and the auxiliary space complexity is O(1)O(1).

g
(
x
)
=
c+
1
t=0t=0t=0
c=0c=0c=0
0
First
x, a,b,c≥
1, g(a)=
g(b)=
g(c)=
h}.
<
k
kkk
x=6t+rx=6t+rx=6t+r
3≤r≤63\le r\le63≤r≤6
hhh
6ti+ri6t_i+r_i6ti​+ri​
3≤ri≤63\le r_i\le63≤ri​≤6
t1+t2+t3=t−1t_1+t_2+t_3=t-1t1​+t2​+t3​=t−1
t−2t-2t−2
k−1k-1k−1
ttt
hhh
h=0h=0h=0
111
222
666
g(x)=0g(x)=0g(x)=0
xxx
0
O
(
1
)