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

bbbbbbbbinary sequence

Back to problem

Editorial

Interpret each string as a polynomial over F2\mathbb{F}_2F2​, with characters written from the lowest-degree coefficient to the highest-degree coefficient. Since every string starts with 111, every polynomial has constant coefficient 111. Shifting by aaa is equivalent to multiplying the polynomial by xax^axa.

Assume that several normalized polynomials are placed at some starting positions and that their total XOR is zero. At least two terms must have the minimum starting position. Otherwise, the bit at that position would be contributed by exactly one term and could not be canceled.

We can therefore backtrack by guessing two terms with the same starting position. Align the starts of two normalized polynomials P,QP,QP,Q and XOR them.

  • If P⊕Q=0P\oplus Q=0P⊕Q=0, the two terms cancel completely, so remove both terms.
  • If P⊕Q≠0P\oplus Q\ne 0P⊕Q=0, let dd be the number of leading zero bits of . Remove those bits to normalize the result so that its first bit is , and replace the two terms with this new term. The new term starts positions after the common start of the old terms.

Try every pair and recurse. If a solution exists, one branch chooses two terms having the minimum starting position in that solution. The transformation preserves the condition that the XOR of all remaining terms is zero, so repeatedly applying it eventually finds a complete solution. Conversely, if the recursion cancels every term, the shifts of the original strings can be reconstructed from the relative positions recorded during the merges.

For reconstruction, store the original string indices contained in every current term and their shifts relative to the normalized start of that term. If a merge removes ddd leading bits, subtract ddd from every stored relative shift in the merged term. When two terms cancel completely, subtract the minimum stored shift from all shifts in that component so that its minimum becomes zero. A completely canceled component has XOR zero by itself, so different components may be normalized independently.

Every current polynomial has length at most LLL. Before its final cancellation, one original string participates in at most N−2N-2N−2 nonzero normalizations, and each normalization removes at most L−1L-1L−1 bits. Therefore the reconstructed shifts can always be made to lie between 000 and (N−2)(L−.

Store each polynomial as an array of 64-bit words. XOR and normalization of one pair then take O(⌈L/64⌉)O(\lceil L/64\rceil)O(⌈L/64⌉) time. The size of the recursion tree depends only on NNN, and N≤8N\le 8N≤8. Pairs with the same two polynomial values can be tried only once at the same recursion state to remove symmetric branches.

Solution written by GPT5.6

d
P⊕QP\oplus QP⊕Q
ddd
111
ddd
1)(N-2)(L-1)
(N−2)(L−1)