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

Palindrome Addition

Back to problem

Editorial

Let F(x)=∑i=0NcixiF(x)=\sum_{i=0}^{N}c_ix^iF(x)=∑i=0N​ci​xi. If N=0N=0N=0, then FFF itself is palindromic, so we can output it directly. Assume N≥1N\ge1N≥1 from now on.

Construct coefficient arrays a0,⋯ ,aNa_0,\cdots,a_Na0​,⋯,aN​ and b0,⋯ ,bN−1b_0,\cdots,b_{N-1}b, and define

g(x)=∑i=0Naixi,h(x)=∑i=0N−1bixi.g(x)=\sum_{i=0}^{N}a_ix^i,\qquad h(x)=\sum_{i=0}^{N-1}b_ix^i.g(x)=i=0∑N​a

Start with

a0=aN=cN.a_0=a_N=c_N.a0​=aN​=cN​

For i=0,1,⋯ ,⌊N−12⌋i=0,1,\cdots,\left\lfloor\frac{N-1}{2}\right\rfloori=0,1,⋯,⌊2N−1​⌋, perform the following assignments in order, all modulo :

bi=ci−ai,bN−1−i=bi,b_i=c_i-a_i,\qquad b_{N-1-i}=b_i,bi​=ci​−a ai+1=cN−1−i−bi,aN−1−i=ai+1.a_{i+1}=c_{N-1-i}-b_i,\qquad a_{N-1-i}=a_{i+1}.ai+1​=cN−1−i​

Each iteration fixes the two outer coefficients of bbb and then the next two outer coefficients of aaa. After the loop, ai=aN−ia_i=a_{N-i}ai​=aN−i for all , so is palindromic, and also holds. Substituting the assignments coefficient by coefficient gives

F=g+h.F=g+h.F=g+h.

If h=0h=0h=0, output only ggg. If h≠0h\ne0h=0 and b0≠0b_0\ne0b, then , so has degree and is itself palindromic. In this case output .

The remaining case is h≠0h\ne0h=0 and b0=0b_0=0b0​=0. Let kkk be the smallest index with . By symmetry, the first and last coefficients of are all zero and . Define

d=N−1−2k,d=N-1-2k,d=N−1−2k, r(x)=∑j=0dbj+kxj.r(x)=\sum_{j=0}^{d}b_{j+k}x^j.r(x)=j=0∑d​bj+

Then rrr is a nonzero palindromic polynomial and

h=xkr.h=x^kr.h=xkr.

Now define

s=r+xkr.s=r+x^kr.s=r+xkr.

Since rrr is palindromic of degree ddd, the coefficients of sss are symmetric around degree d+kd+kd+k, so sss is palindromic as well. The polynomial −r-r is also palindromic, and

s+(−r)=xkr=h.s+(-r)=x^kr=h.s+(−r)=xkr=h.

Therefore

F=g+s+(−r),F=g+s+(-r),F=g+s+(−r),

which uses at most three palindromic polynomials.

The leading coefficient of ggg is cN≠0c_N\ne0cN​=0. In the two-polynomial case, the leading coefficient of hhh is . In the last case, the construction also gives nonzero leading coefficients for , , and . Hence every printed polynomial satisfies the definition in the statement.

Every coefficient is processed only a constant number of times, so the time complexity for one test case is O(N)O(N)O(N) and the extra memory usage is O(N)O(N)O(N). Over the whole input, the time complexity is O ⁣(∑(N+1))O\!\left(\sum(N+1)\right)O(∑(N+1)). Only addition and subtraction are used, so does not need to be prime.

Solution written by GPT5.6

0​
,
⋯
,
bN−1​
i​
xi
,
h
(
x
)
=
i=0∑N−1​bi​xi.
.
MM
M
i
​
,
bN−1−i​
=
bi​,
−
bi​,aN−1−i​=
ai+1​.
​
iii
ggg
bi=bN−1−ib_i=b_{N-1-i}bi​=bN−1−i​
0​
=
0
bN−1=b0≠0b_{N-1}=b_0\ne0bN−1​=b0​=0
hhh
N−1N-1N−1
g,hg,hg,h
bk≠0b_k\ne0bk​=0
kkk
bbb
bN−1−k=bk≠0b_{N-1-k}=b_k\ne0bN−1−k​=bk​=0
k
​
xj
.
−r
b0≠0b_0\ne0
b0​=0
rrr
sss
−r-r−r
MMM