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

Yet Another XOR Problem

Back to problem

Editorial

The values A1A_1A1​ and ANA_NAN​ are never changed by any operation. Hence A1=B1A_1=B_1A1​=B1​ and AN=BNA_N=B_NAN​=BN​ are necessary.

Define the adjacent XOR array DDD by Di=Ai⊕Ai+1D_i=A_i \oplus A_{i+1}Di​=Ai​⊕A for . If an operation is performed at position , only changes, so only and are affected. A direct calculation shows that these two values are swapped, and all other values remain unchanged.

Therefore one operation is exactly an adjacent swap in the adjacent XOR array. Since arbitrary permutations can be obtained by adjacent swaps, the adjacent XOR array can be reordered arbitrarily.

Conversely, once A1A_1A1​ is fixed, the adjacent XOR array uniquely determines the whole array. Thus AAA can be made equal to BBB if and only if the following conditions hold.

  • A1=B1A_1=B_1A1​=B1​.
  • AN=BNA_N=B_NAN.

For each test case, build the two adjacent XOR arrays, sort them, and compare them. The time complexity is O(Nlog⁡N)O(N \log N)O(NlogN) per test case, and the memory complexity is O(N)O(N)O(N).

Solution written by GPT5.5

i+1​
1≤i≤N−11 \leq i \leq N-11≤i≤N−1
iii
AiA_iAi​
Di−1D_{i-1}Di−1​
DiD_iDi​
​
=
BN​
  • The multiset of Ai⊕Ai+1A_i \oplus A_{i+1}Ai​⊕Ai+1​ for 1≤i≤N−11 \leq i \leq N-11≤i≤N−1 equals the multiset of Bi⊕Bi+1B_i \oplus B_{i+1}Bi​⊕Bi+1​ for 1≤i≤N−11 \leq i \leq N-11≤i≤N−1.