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

Catwalk BrainF**k

Back to problem

Editorial

Suppose exactly 2k2k2k bracket characters appear. Their positions can be chosen in (N2k)\binom{N}{2k}(2kN​) ways, and each remaining position can contain any of the six non-bracket characters. The number of correct bracket strings on the chosen 2k2k2k positions is the Catalan number

Cat(k)=1k+1(2kk).\mathrm{Cat}(k)=\frac{1}{k+1}\binom{2k}{k}.Cat(k)=k+11​(k

Therefore, if ANA_NAN​ is the number of compilable strings of length NNN,

AN=∑k=0⌊N/2⌋(N2k)Cat(k)6N−2k.A_N=\sum_{k=0}^{\lfloor N/2\rfloor}\binom{N}{2k}\mathrm{Cat}(k)6^{N-2k}.AN​=k=0∑

For a single test case, evaluating this sum directly gives an O(N)O(N)O(N) solution.

To handle all test cases efficiently, precompute every ANA_NAN​ in linear time. Interpret [ as a step that increases the height by 111, ] as a step that decreases the height by 111, and each of the other six characters as a distinct step that keeps the height unchanged. A compilable string is exactly a weighted Motzkin path that never goes below height 000 and ends at height 00.

Let

F(x)=∑n≥0Anxn.F(x)=\sum_{n\ge 0}A_nx^n.F(x)=n≥0∑​An​x

A first-step decomposition gives

F(x)=1+6xF(x)+x2F(x)2,F(x)=1+6xF(x)+x^2F(x)^2,F(x)=1+6xF(x)+x2F(x)

so

F(x)=1−6x−1−12x+32x22x2.F(x)=\frac{1-6x-\sqrt{1-12x+32x^2}}{2x^2}.F(x)=2x21−6x−

Differentiating and rearranging gives

x(1−12x+32x2)F′(x)+(2−18x+32x2)F(x)−2=0.x(1-12x+32x^2)F'(x)+(2-18x+32x^2)F(x)-2=0.x(1−12x+32x2)F′

Comparing coefficients of xnx^nxn yields, for n≥2n\ge 2n≥2,

(n+2)An=6(2n+1)An−1−32(n−1)An−2.(n+2)A_n=6(2n+1)A_{n-1}-32(n-1)A_{n-2}.(n+2)An​=6(2n+1)A

The required value is the probability

Rn=An8n.R_n=\frac{A_n}{8^n}.Rn​=8nA

Dividing the recurrence by 8n8^n8n gives

(n+2)Rn=3(2n+1)4Rn−1−n−12Rn−2,(n+2)R_n=\frac{3(2n+1)}{4}R_{n-1}-\frac{n-1}{2}R_{n-2},(n+2)Rn​=4

with R0=1R_0=1R0​=1 and R1=34R_1=\cfrac34R1​=.

Modulo 998 244 353998\,244\,353998244353, the numbers 222, 444, and every n+2n+2n+2 with 2≤n+2≤100 0022\le n+2\le 100\,0022 are invertible. Hence the recurrence can be evaluated directly modulo the prime. If is the maximum input value, precompute once and answer each test case with .

The time complexity is O(M+T)O(M+T)O(M+T) and the space complexity is O(M+T)O(M+T)O(M+T).

Before reduction, the probability has denominator 8N8^N8N. Therefore its reduced denominator is also coprime to 998 244 353998\,244\,353998244353.

Solution written by GPT5.6

2k
​
)
.
⌊N/2⌋
​
(2kN​)
Cat
(
k
)
6N−2k
.
0
n
.
2
,
1−12x+32x2​
​
.
(
x
)
+
(2−
18x+
32x2)F(x)−
2=
0.
n−1​
−
32(n−
1)An−2​.
n
​
​
.
3(2n+1)
​
Rn−1​
−
2n−1​Rn−2​,
43
​
≤
n+
2≤
100002
MMM
R0,R1,…,RMR_0,R_1,\ldots,R_MR0​,R1​,…,RM​
RNR_NRN​