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 account linking.

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

Difference Pyramid

Back to problem

Editorial

After every operation, every value is still one of 000, 111, and 222. Therefore, the final value is also one of these three values.

First, determine the parity of the final value. For integers x,yx,yx,y,

∣x−y∣≡x+y(mod2).|x-y|\equiv x+y\pmod 2.∣x−y∣≡x+y(mod2).

Thus, modulo 222, one operation is equivalent to replacing adjacent values by their sum.

After repeating this N−1N-1N−1 times, Pascal-triangle coefficients appear. If PPP is the parity of the final value, then

P≡∑i=1N(N−1i−1)Ai(mod2).P\equiv\sum_{i=1}^{N}\binom{N-1}{i-1}A_i\pmod 2.P≡i=1∑N​(

By Lucas's theorem,

(nk)\binom{n}{k}(kn​)

is odd if and only if every set bit of kkk is also set in nnn. Equivalently,

(k&n)=k.(k\mathbin{\&}n)=k.(k&n)=k.

Therefore, with n=N−1n=N-1n=N−1, XOR Ai mod 2A_i\bmod2Ai​mod2 over all positions satisfying . This computes in time.

If P=1P=1P=1, the final value is odd. Since it is one of 000, 111, and 222, the answer must be 111.

Now suppose P=0P=0P=0. The final value is either 000 or 222.

If the final value is 222, the original array cannot contain a 111. This can be proved by induction on the array length. The claim is trivial for length 111. For a longer array whose final value is 222, apply the induction hypothesis to the array after the first operation. Every value after the first operation must be even. Hence every adjacent pair in the original array has the same parity, so all original values have the same parity. If they were all odd, then they would all be 111, the first operation would produce only 00s, and the final value would be , a contradiction. Thus all original values are or .

Consequently, if P=0P=0P=0 and at least one AiA_iAi​ equals 111, the answer is 000.

It remains to handle the case where every AiA_iAi​ is 000 or 222. Let Ci=Ai/2C_i=A_i/2C, so is binary. Since

∣2x−2y∣=2∣x−y∣,|2x-2y|=2|x-y|,∣2x−2y∣=2∣x−y∣,

every value in the entire Difference Pyramid for AAA is exactly twice the corresponding value for CCC. Thus, the final value for AAA is twice the final value for CCC.

Because CCC is binary, its final value is exactly its parity, which is

Q≡∑i=1N(N−1i−1)Ai2(mod2).Q\equiv\sum_{i=1}^{N}\binom{N-1}{i-1}\frac{A_i}{2}\pmod2.Q≡i=1∑N​(

Compute QQQ with the same Lucas-theorem test and print 2Q2Q2Q.

We can compute PPP, QQQ, and whether a 111 exists in a single pass. The time complexity is O(N)O(N)O(N) and the memory complexity is O(1)O(1)O(1).

Solution written by GPT5.6

i−1
N−1
​
)
Ai​
(mod2).
(i−1)&n=i−1(i-1)\mathbin{\&}n=i-1
(i−1)&n=i−1
PPP
O(N)O(N)O(N)
0
000
000
222
i
​
=
Ai​/2
CiC_iCi​
i−1N−1
​
)
2Ai​​
(mod2).