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

Floccinaucinihilipilification

Back to problem

Editorial

Which pairs contribute to the answer?

Call the initially present viruses original viruses and those inserted during changes new viruses. Pairs of original viruses contribute equally to both extinction risks. The answer is therefore the sum of similarities over unordered pairs containing at least one new virus. All terms are nonnegative, so subtracting the original-only risk from the full risk gives the required absolute difference.

Store the strings in a trie. Each non-root node represents a nonempty prefix. The number of nodes on the common prefix of two strings equals their similarity. If a node's subtree contains ooo original viruses and aaa new viruses, its contribution is

F(o,a)=oa+(a2)=oa+a(a−1)2.F(o,a)=oa+\binom{a}{2}=oa+\cfrac{a(a-1)}{2}.F(o,a)=oa+(2a​)=oa+2

Summing FFF over all non-root nodes gives the answer. A pair of new viruses must be counted only once. Distinct virus types with identical structures still count separately.

Subtask 1: direct simulation

After each change, explicitly modify every string and append the new viruses. Compute the longest common prefix for every pair containing a new virus. With B=∑xiB=\sum x_iB=∑xi​ and H=max⁡∣Vi∣+MH=\max |V_i|+MH=max∣V, the time complexity is and the memory complexity is . This subtask has and .

Subtask 2: one change

Let the only change be cxcxcx. If kkk original structures start with ccc after the mutation, the answer is kx+(x2)kx+\binom{x}{2}kx+(2. An original string qualifies exactly when it did not initially start with , or when its first two characters were both . A one-character string becomes empty. Writing , the solution takes time.

Subtask 3: only the letter A

Every new virus always has an empty or one-character structure. If there are ooo nonempty original structures and aaa nonempty new structures, the answer is F(o,a)F(o,a)F(o,a).

An original string of length ℓ\ellℓ first becomes empty after ℓ\ellℓ changes. It then alternates between empty and length one. Count initial lengths and maintain, separately for both parities, the number of originals that have already reached zero. At time iii, an original structure is empty exactly when its initial length is at most iii and has the same parity as iii.

A virus inserted at time ttt has length one at time iii exactly when i−ti-ti−t is even. Maintain insertion totals by time parity as well. The total time complexity is O(L+M)O(L+M)O(L+M), with O memory.

Moving the root of the trie

Let rrr be the current root and ccc the mutation letter. Let sss be the ccc child of rrr, creating an empty node if that child does not exist.

Moving the root from rrr to sss removes the leading ccc from strings that previously started with ccc. Every other string gains a leading ccc, represented by an edge from the new root to the old root. The old root's former ccc child link must be removed.

Inserting xxx new viruses after the mutation is equivalent to inserting xxx empty strings at the old root before moving it. These strings become exactly the one-character string ccc after the move. Inserting empty strings at the root does not immediately change the answer because no nonempty prefix is added.

After adding these xxx new viruses to the old root's count, let (os,as)(o_s,a_s)(os​,as​) be the counts at sss before the move. Subtract because becomes the root. Subtract the counts of from those of , and add the contribution of the resulting to the answer. Set the counts of to the total counts. The move itself leaves all other subtree counts unchanged.

Merging subtrees with equal edge labels

If sss already had a ccc child uuu, it conflicts with rrr, which is also becoming a ccc child. The node uuu represents strings that originally started with cc. The two subtrees now represent the same prefix and must be merged.

When merging nodes v,wv,wv,w, subtract their previous contributions, combine their counts, and add the contribution of the combined node. A child present on only one side can be transferred unchanged. Children present on both sides under the same letter must also be merged.

The reference solution uses an explicit work stack, avoiding recursive calls even for very long strings. Each merge makes one node unreachable. Thus, the total number of node merges is bounded by the number of nodes ever created.

The initial trie has at most L+1L+1L+1 nodes, and each change creates at most one node. For alphabet size KKK, the total time and memory complexities are both O(K(L+M))O(K(L+M))O(K(L+M)). The full task has K=26K=26, giving linear complexity in .

Subtasks 4, 5, and 6

For subtasks 4 and 5, specialize the same algorithm to child arrays of sizes 222 and 666. Their time and memory complexities are O(2(L+M))O(2(L+M))O(2(L+M)) and O(6(L+M))O(6(L+M))O(6(L, respectively.

In subtask 6, exactly one new virus is inserted throughout the entire process. Thus a≤1a \leq 1a≤1 always holds and (a2)=0\binom{a}{2}=0(2a​)=0. A trie implementation using only the contribution oaoa solves this subtask in time and memory. The full solution must retain the term for pairs of new viruses.

Integer bounds

At most B=9M≤2 700 000B=9M \leq 2\,700\,000B=9M≤2700000 new viruses are inserted. A virus inserted at step iii has final length at most M−i+1M-i+1M−i+. Original-new pairs contribute at most . New-new pairs are maximized by inserting viruses at every step and increasing the length of every existing new structure at each later step. Their maximum contribution is .

The exact maximum answer is therefore 9M(M+1)(N+3M+1)/29M(M+1)(N+3M+1)/29M(M+1)(N+3M+1)/2, which is 486 002 025 001 350 000486\,002\,025\,001\,350\,000486002025 at the full bounds. This is attained by using identical initial strings of one letter and alternating mutations of two other letters while inserting viruses each time. The intermediate product before division is also at most , within signed -bit range.

There are at most 3 000 0003\,000\,0003000000 viruses, and a single-node contribution is at most 4 454 998 650 0004\,454\,998\,650\,0004454998650000. The original-only risk is at most ((N−1)L+MN(N−1))/2((N-1)L+MN(N-1))/2((N−1). Even if both risks are computed separately, the full risk is at most , safely within signed -bit range. The reference solution directly accumulates prefix contributions and does not evaluate these large polynomial formulas.

At most 2 300 0022\,300\,0022300002 nodes are allocated, including the dummy node. The reference solution maintains a bitmask of existing child letters and visits only those children while merging. Each full-task node uses 116116116 bytes, so the node array occupies about 255255255 MiB. Reserving the maximum capacity in advance avoids holding both old and new arrays during reallocation. The merge work stack and input buffers also fit within the 1 0241\,0241024 MiB limit.

The validator checks bounds before accumulating an integer. Intermediate seed-hash values are below 2.82⋅10112.82\cdot 10^{11}2.82⋅1011, random-state multiplication is below 1.04⋅10141.04\cdot 10^{14}1.04⋅1014, and the generator's product N(N+1)N(N+1) is at most . No judging component requires an integer type wider than bits.

Solution written by GPT6

a(a−1)
​
.
i
​
∣
+
M
O(M(N+B)2H)O(M(N+B)^2H)O(M(N+B)2H)
O((N+B)H)O((N+B)H)O((N+B)H)
N+B≤100N+B \leq 100N+B≤100
H≤20H \leq 20H≤20
x
​
)
ccc
ccc
ccc
L=∑∣Vi∣L=\sum |V_i|L=∑∣Vi​∣
O(L)O(L)O(L)
(L)O(L)
O(L)
F(os,as)F(o_s,a_s)
F(os​,as​)
sss
sss
rrr
rrr
sss
cc
cc
K
=
26
L+ML+ML+M
+
M))
oa
O(26(L+M))O(26(L+M))O(26(L+M))
1
9NM(M+1)/29NM(M+1)/29NM(M+1)/2
999
∑t=1M(9t2)=9M(M+1)(3M+1)/2\sum_{t=1}^{M}\binom{9t}{2}=9M(M+1)(3M+1)/2∑t=1M​(29t​)=9M(M+1)(3M+1)/2
001
350
000
999
972 004 050 002 700 000972\,004\,050\,002\,700\,000972004050002700000
646464
L
+
MN(N−
1))/2
499 502 280 000 350 000499\,502\,280\,000\,350\,000499502280000350000
646464
N(N+1)
90 000 300 00090\,000\,300\,00090000300000
646464