[模板] 多项式操作模板

NOI 前复习。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>

using namespace std;

const int maxn = 100010; //最大多项式长度,倍数问题已经考虑
const int mod = 998244353;
const int g = 3;

int wa[8*maxn], wb[8*maxn], wc[8*maxn], rev[8*maxn];

int qpow(int x, int y) {
int ret = 1;
while (y) {
if (y & 1) ret = 1LL*ret*x%mod;
x = 1LL*x*x%mod;
y >>= 1;
}
return ret;
}

struct poly {
int *a, len;
poly(int l_ = 0) {
len = l_;
a = new int[l_];
for (int i = 0; i < l_; i++) a[i] = 0;
}
};

void ntt(int *a, int l, int ty) {
rev[0] = 0;
for (int i = 1; i < (1<<l); i++) rev[i] = ((rev[i >> 1] >> 1) | ((i&1) << (l-1)));
for (int i = 0; i < (1<<l); i++) if (rev[i] > i) swap(a[i], a[rev[i]]);
for (int len = 2; len <= (1<<l); len <<= 1) {
int wn = qpow(g, (mod-1)/len);
for (int i = 0; i < (1<<l); i += len) {
int w = 1;
for (int j = 0; j < (len>>1); j++) {
int v1 = a[i+j], v2 = 1LL*w*a[i+j+(len>>1)]%mod;
a[i+j] = (v1 + v2) % mod;
a[i+j+(len >> 1)] = (v1 + mod - v2) % mod;
w = 1LL*w*wn%mod;
}
}
}
if (ty == -1) {
int inv = qpow(1<<l, mod-2);
for (int i = 0; i < (1<<l); i++) a[i] = 1LL*a[i]*inv%mod;
for (int i = 1; i < (1<<(l-1)); i++) swap(a[i], a[(1<<l)-i]);
}
}

poly operator*(const poly &p1, const poly &p2) {
poly ret(p1.len + p2.len - 1);
int l = 0; while ((1<<l) < ret.len) ++ l;
for (int i = 0; i < (1<<l); i++) wa[i] = wb[i] = 0;
for (int i = 0; i < p1.len; i++) wa[i] = p1.a[i];
for (int i = 0; i < p2.len; i++) wb[i] = p2.a[i];
ntt(wa, l, 1); ntt(wb, l, 1);
for (int i = 0; i < (1<<l); i++) wc[i] = 1LL*wa[i]*wb[i]%mod;
ntt(wc, l, -1);
for (int i = 0; i < ret.len; i++) ret.a[i] = wc[i];
return ret;
}

poly polyInv(const poly &p) {
if (p.len == 1) {
poly ret(1);
ret.a[0] = qpow(p.a[0], mod-2);
return ret;
}
int nlen = (p.len+1)/2;
poly tmp(nlen); for (int i = 0; i < nlen; i++) tmp.a[i] = p.a[i];
poly G = polyInv(tmp);
poly ret(p.len);
for (int i = 0; i < G.len; i++) ret.a[i] = 1LL*2*G.a[i]%mod;
poly t = p*G*G;
for (int i = 0; i < p.len; i++) ret.a[i] = (ret.a[i] + mod - t.a[i]) % mod;
return ret;
}

poly polyLn(const poly &p) {
if (p.len == 1) return poly(1);
poly inv = polyInv(p);
poly d(p.len);
for (int i = 0; i+1 < p.len; i++) {
d.a[i] = 1LL*p.a[i+1]*(i+1)%mod;
}
inv = inv * d;
poly ret(p.len);
for (int i = 1; i < ret.len; i++) {
ret.a[i] = 1LL*qpow(i, mod-2)*inv.a[i-1]%mod;
}
return ret;
}

poly polyExp(const poly &p) {
if (p.len == 1) {
poly ret(1);
ret.a[0] = 1;
return ret;
}
int nlen = (p.len+1)/2;
poly tmp(nlen); for (int i = 0; i < nlen; i++) tmp.a[i] = p.a[i];
poly G = polyExp(tmp);
poly t(p.len), v(p.len);
for (int i = 0; i < nlen; i++) v.a[i] = G.a[i];
v = polyLn(v);
for (int i = 0; i < p.len; i++) {
t.a[i] = (mod - v.a[i] + p.a[i]) % mod;
}
t.a[0] = (t.a[0] + 1) % mod;
t = t*G;
poly ret(p.len);
for (int i = 0; i < ret.len; i++) ret.a[i] = t.a[i];
return ret;
}

void polyDiv(const poly &F, const poly &Q, poly &P, poly &R) {
poly rF(F.len-Q.len+1), rQ(F.len-Q.len+1);
for (int i = 0; i < rF.len; i++) rF.a[i] = F.a[F.len-1-i];
for (int i = 0; i < rQ.len; i++) if (Q.len-1-i >= 0) rQ.a[i] = Q.a[Q.len-1-i];
poly v = rF*polyInv(rQ);
for (int i = 0; i < P.len; i++) P.a[i] = v.a[F.len-Q.len-i];
poly t = P*Q;
for (int i = 0; i < R.len; i++) R.a[i] = (F.a[i] + mod - t.a[i]) % mod;
}

poly polySqrt(const poly &p) {
if (p.len == 1) {
poly ret(1);
ret.a[0] = 1;
return ret;
}
int nlen = (p.len+1)/2;
poly np(nlen); for (int i = 0; i < nlen; i++) np.a[i] = p.a[i];
poly F0 = polySqrt(np);
poly exF0(p.len);
for (int i = 0; i < F0.len; i++) exF0.a[i] = F0.a[i];
poly tmp = F0*F0;
poly extmp(p.len);
for (int i = 0; i < p.len; i++) {
if (i < tmp.len) extmp.a[i] = tmp.a[i];
extmp.a[i] = (extmp.a[i] + mod - p.a[i]) % mod;
}
poly Q(p.len); for (int i = 0; i < p.len; i++) Q.a[i] = 1LL*exF0.a[i]*2%mod;
poly tt = extmp*polyInv(Q);
poly res(p.len);
for (int i = 0; i < res.len; i++) {
res.a[i] = (exF0.a[i] + mod - tt.a[i]) % mod;
}
return res;
}

int main() {
int n;
scanf("%d", &n);
poly P(n);
for (int i = 0; i < n; i++) scanf("%d", &P.a[i]);
poly res = polySqrt(P);
for (int i = 0; i < res.len; i++) printf("%d ", res.a[i]);
printf("\n");
return 0;
}