DOJ logoDOJ
Problems
Recently added problemsSaved problems
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

Weird Decimal XOR

Back to problem

Editorial

Number the colors Red, Orange, Yellow, Blue, Purple as 0,1,2,3,40,1,2,3,40,1,2,3,4, and represent the state by 000 or 111. Use the following mapping fff:

(f(0),f(1),⋯ ,f(9))=(0,5,6,1,2,7,8,3,4,9).(f(0),f(1),\cdots,f(9))=(0,5,6,1,2,7,8,3,4,9).(f(0),f(1),⋯,f(9))=(0,5

For a digit ddd, f(d)f(d)f(d) is the unique integer from 000 to 999 satisfying both conditions below.

  • f(d) mod 5f(d)\bmod 5f(d)mod5 is the color index of ddd.
  • f(d) mod 2f(d)\bmod 2f(d)mod is the state of .

Color composition is addition of color indices modulo 555, while XOR of two states is addition modulo 222. Therefore, by the Chinese remainder theorem,

f(x⋄y)≡f(x)+f(y)(mod10).f(x\diamond y)\equiv f(x)+f(y)\pmod{10}.f(x⋄y)≡f(x)+f(y)(mod10).

Let dj(Ai)d_j(A_i)dj​(Ai​) be the digit at the 10j10^j10j position of . Define a prefix sum for every decimal position by

Sij=∑k=1if(dj(Ak))(mod10),S0j=0.S_i^j=\sum_{k=1}^{i} f(d_j(A_k))\pmod{10},\qquad S_0^j=0.Sij​=k

The result at decimal position jjj for a subarray [l,r][l,r][l,r] is 000 if and only if Sl−1j=SrjS_{l-1}^j=S_r^jSl.

The whole weird decimal XOR is 000 exactly when this condition holds at every decimal position. Since 0≤Ai≤10180\le A_i\le10^{18}0≤Ai​≤1018, only the 1919 positions matter. Define

Si=(Si0,Si1,⋯ ,Si18).S_i=(S_i^0,S_i^1,\cdots,S_i^{18}).Si​=(Si0​,S

Then the weird decimal XOR of Al,Al+1,⋯ ,ArA_l,A_{l+1},\cdots,A_rAl​,Al+1​,⋯,A is if and only if .

Subtask 1

If Ai≤9A_i\le9Ai​≤9, only the ones digit can be nonzero, so each SiS_iSi​ is effectively a single residue modulo 101010. Store prefix occurrence counts for each residue . In a query , obtain the frequency of every residue among prefix positions . If a residue appears times, it contributes equal-position pairs. Thus one query takes time.

The time complexity is O(N+10Q)O(N+10Q)O(N+10Q) and the space complexity is O(10N)O(10N)O(10N).

Full constraints

Now SiS_iSi​ is a vector of length 191919, so the entire state space is far too large to enumerate. However, only S0,S1,⋯ ,SNS_0,S_1,\cdots,S_NS0​, can actually occur. Sort these vectors lexicographically and assign the same integer ID to equal vectors.

Each query [L,R][L,R][L,R] is now the problem of counting pairs of distinct positions with equal IDs inside the prefix-position interval [L−1,R][L-1,R][L−1,R].

Process these queries offline with Mo's algorithm. If the current interval contains ccc occurrences of ID vvv, adding one more vvv increases the answer by ccc. When removing vvv, first decrease its frequency by one and then subtract the remaining frequency from the answer.

Building the vectors costs O(19N)O(19N)O(19N), coordinate compression costs O(19Nlog⁡N)O(19N\log N)O(19NlogN), and Mo's algorithm costs O((N+Q)N)O((N+Q)\sqrt N)O((N+Q. Therefore the total time complexity is

O(19Nlog⁡N+(N+Q)N),O(19N\log N+(N+Q)\sqrt N),O(19NlogN+(N+Q)N

and the space complexity is O(19N+Q)O(19N+Q)O(19N+Q).

Solution written by GPT5.6

,
6
,
1
,
2
,
7
,
8
,
3
,
4
,
9
)
.
2
ddd
AiA_i
Ai​
=
1
∑
i
​
f
(
dj​
(
Ak​
))
(mod10),S0j​=
0.
−
1
j
​
=
Srj​
19
j=0,1,⋯ ,18j=0,1,\cdots,18j=0,1,⋯,18
i1​
,
⋯
,
Si18​
)
.
r
​
000
Sl−1=SrS_{l-1}=S_rSl−1​=Sr​
0,1,⋯ ,90,1,\cdots,90,1,⋯,9
[L,R][L,R][L,R]
L−1,L,⋯ ,RL-1,L,\cdots,RL−1,L,⋯,R
ccc
(c2)\binom{c}{2}(2c​)
O(10)O(10)O(10)
S1​
,
⋯
,
SN​
)
N​
)
​
)
,