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

Polynomial Inverse DSL

Back to problem

Editorial

Initially, g=f0−1g=f_0^{-1}g=f0−1​ satisfies fg≡1(modx)fg\equiv 1\pmod{x}fg≡1(modx). Given an inverse of length mmm, the Newton update

g′=2g−fg2(modx2m)g' = 2g-fg^2\pmod{x^{2m}}g′=2g−fg2(mod

doubles the precision. A direct implementation with two full convolutions is correct, but its normalized cost is about 242424 for N=220N=2^{20}N=220, so it does not receive full score.

The full-score construction recovers the required high half of q=fg2q=fg^2q=fg2 from two cyclic views. Treat ggg as a length-mmm polynomial and truncate fff to length .

First compute

u=q mod (x2m−1)u=q\bmod (x^{2m}-1)u=qmod(x2m−1)

with a cyclic convolution of length 2m2m2m. This uses two length-2m2m2m DFTs and one IDFT.

Next, let rrr be a primitive 4m4m4m-th root and let j=rmj=r^mj=rm. Multiply coefficient iii by rir^i, fold modulo , and perform a cyclic convolution of length . After the inverse transform, multiply coefficient by . The result is

vk=qk+jqk+m−qk+2m−jqk+3m.v_k=q_k+jq_{k+m}-q_{k+2m}-jq_{k+3m}.vk​=qk​+j

For 0≤k<m0\le k<m0≤k<m, write

A=qk,B=qk+m,C=qk+2m,D=qk+3m.A=q_k,\quad B=q_{k+m},\quad C=q_{k+2m},\quad D=q_{k+3m}.A=qk​,B=q

Then

ulow=A+C,uhigh=B+D,u_{\mathrm{low}}=A+C,\qquad u_{\mathrm{high}}=B+D,ulow​=A+C,u v=A+jB−C−jD.v=A+jB-C-jD.v=A+jB−C−jD.

Because fg≡1(modxm)fg\equiv 1\pmod{x^m}fg≡1(modxm), we have q=fg2≡g(modxm)q=fg^2\equiv g\pmod{x^m}q, so . Therefore,

−B=j(ulow+v−2g)−uhigh2.-B=\frac{j(u_{\mathrm{low}}+v-2g)-u_{\mathrm{high}}}{2}.−B=2j(ulow​

The new high half in the Newton update is exactly −B-B−B, so compute this expression and append it to ggg.

Each doubling step uses three transforms of length 2m2m2m and three transforms of length mmm. For N=2KN=2^KN=2K, the total cost is

3∑k=0K−1(D(2k+1)+D(2k))=(9K−12)N+12.3\sum_{k=0}^{K-1}\left(D(2^{k+1})+D(2^k)\right) =(9K-12)N+12.3k=0∑K−1​(D(2

Thus, for N=220N=2^{20}N=220,

C=8.400000572…<9,C=8.400000572\ldots<9,C=8.400000572…<9,

which receives full score. When NNN is not a power of two, zero-pad at each step and truncate to length NNN at the end.

The frequency order of DFT output is irrelevant. The construction only multiplies DFT outputs produced at the same length, so the opaque-order guarantees in the specification are sufficient.

Solution written by GPT5.6

x2m
)
2m2m
2m
ri
fff
xm−1x^m-1xm−1
mmm
iii
r−ir^{-i}r−i
q
k+m​
−
qk+2m​−
jqk+3m​.
k+m
​
,
C
=
qk+2m​,D=
qk+3m​.
high
​
=
B+
D,
=
fg2≡
g
(modxm)
A=gkA=g_kA=gk​
+
v
−
2
g
)
−
uhigh​
​
.
k+1
)
+
D
(
2k
)
)
=
(9K−
12)N+
12.