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

Shirogane Wants to Sing

Back to problem

Editorial

Since the total error is at most EEE, each term satisfies ∣Ai−Bi∣≤E|A_i-B_i|\le E∣Ai​−Bi​∣≤E. Because 0≤Ai≤500\le A_i\le500≤Ai​≤50 and Bi≥0B_i\ge0Bi​≥0, all possible pitches lie between L=0L=0L=0 and U=50+EU=50+EU=50+E. This range contains W=51+E≤101W=51+E\le101W=51+E≤101 integers.

Let Di(e,x)D_i(e,x)Di​(e,x) be the number of sequences consisting of the first iii terms whose total error is exactly eee and whose last pitch BiB_iB is . Store states with and .

The first pitch is fixed at B1=0B_1=0B1​=0, so its error is A1A_1A1​. If A1≤EA_1\le EA, initialize

D1(A1,0)=1.D_1(A_1,0)=1.D1​(A1​,0)=1.

All other states are 000. If A1>EA_1>EA1​>E, no valid sequence exists, so we can immediately print 000.

Now suppose i≥2i\ge2i≥2 and we choose xxx as the last pitch. The error on this term is d=∣Ai−x∣d=|A_i-x|d=∣Ai​−x, so the total error on the first terms must be . The previous pitch must satisfy . Thus, for ,

Di(e,x)=∑y=max⁡(L,x−K)min⁡(U,x+K)Di−1(e−d,y).D_i(e,x)=\sum_{y=\max(L,x-K)}^{\min(U,x+K)}D_{i-1}(e-d,y).Di​(e,x)=

States with e<de<de<d have value 000.

Process i=2,3,…,Ni=2,3,\ldots,Ni=2,3,…,N in order. For each error eee and last pitch xxx, compute the sum by directly iterating over the values of yyy in the interval above. Perform all additions modulo .

The final answer is

∑e=0E∑x=LUDN(e,x).\sum_{e=0}^{E}\sum_{x=L}^{U}D_N(e,x).e=0∑E​x=L∑U

We must sum all states whose error is at most EEE, rather than counting only those whose error is exactly EEE.

There are N(E+1)WN(E+1)WN(E+1)W states, and each state checks at most 2K+12K+12K+1 previous pitches. Therefore, the time complexity is

O(N⋅W⋅(E+1)⋅(2K+1)).O\bigl(N\cdot W\cdot(E+1)\cdot(2K+1)\bigr).O(N⋅W⋅(E+1)⋅(2K+1)).

These factors correspond to number of terms × pitch range × total error range × range of differences between adjacent pitches. Under the given constraints, we can compute these transitions directly.

Since each step uses only the immediately preceding step, we only need to store two layers of the array. The space complexity is O(W(E+1))O(W(E+1))O(W(E+1)). Map pitch xxx to array index x−Lx-Lx−L.

Proof

The first term is uniquely fixed at 000, and its error is A1A_1A1​. Thus, setting D1(A1,0)=1D_1(A_1,0)=1D1​ when counts the valid sequence of length exactly once. If , later terms cannot decrease the total error, so the answer is .

Solution written by GPT6

i​
xxx
0≤e≤E0\le e\le E0≤e≤E
L≤x≤UL\le x\le UL≤x≤U
1​
≤
E
∣
i−1i-1i−1
e−de-de−d
yyy
∣x−y∣≤K|x-y|\le K∣x−y∣≤K
e≥de\ge de≥d
y=max(L,x−K)
∑
min(U,x+K)
​
Di−1​
(
e
−
d,y).
1 000 000 0071\,000\,000\,007
1000000007
​
DN​
(
e
,
x
)
.
(
A1​
,
0
)
=
1
A1≤EA_1\le EA1​≤E
111
A1>EA_1>EA1​>E
000

Assume that Di−1D_{i-1}Di−1​ is correct. Removing the last term from a valid sequence whose last pitch is xxx and whose total error is eee leaves a previous sequence with error e−∣Ai−x∣e-|A_i-x|e−∣Ai​−x∣ and last pitch yyy satisfying ∣x−y∣≤K|x-y|\le K∣x−y∣≤K.

Conversely, appending xxx to any such previous sequence satisfies the restriction on adjacent pitches and makes the total error exactly eee. These two operations are inverses, so the transition counts every valid sequence exactly once. Summing all final states with e≤Ee\le Ee≤E gives the required answer.