-多项式-NTT/FFT- 多项式板子合集

下面是多项式的一些操作,包含了NTT,求逆,求 $\ln$,求导,求积分,求 $e^x$,开根,快速幂,三角函数和反三角函数。

多项式除法先咕着

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 400010
#define MOD 998244353
#define G 3
#define I 86583718
void read(int &x) {
int f = 1;
x = 0;
char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
x *= f;
}
void write(int x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
int qpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = (1LL * res * a) % MOD;
b >>= 1;
a = (1LL * a * a) % MOD;
}
return res % MOD;
}
int n, m;
int a[MAXN], b[MAXN], f[MAXN], rev[MAXN];
void ntt(int *a, int n, int f) {
for (int i = 0; i < n; i++) {
if (1LL * i < rev[i]) {
std::swap(a[i], a[rev[i]]);
}
}
for (int i = 1; i < n; i <<= 1) {
int wn = qpow(G, (MOD - 1) / ((long long)i << 1));
for (int j = 0; j < n; j += ((long long)i << 1)) {
int w = 1LL;
for (int k = 0; k < i; ++k, w = 1LL * w * wn % MOD) {
int x = a[j + k], y = 1LL * w * a[j + k + i] % MOD;
a[j + k] = (x + y) % MOD, a[j + k + i] = (x - y + MOD) % MOD;
}
}
}
if (f == 1) return;
int tmp = qpow(n, MOD - 2);
std::reverse(a + 1, a + n);
for (int i = 0; i < n; i++) {
a[i] = 1LL * a[i] * tmp % MOD;
}
}
int c[MAXN];
void inv(int d, int *a, int *b) {
if (d == 1) {
b[0] = qpow(a[0], MOD - 2);
return;
}
inv((d + 1) >> 1, a, b);
int l = 0, _n = 1;
while (_n < (d << 1)) _n <<= 1, l++;
for (int i = 1; i < _n; i++) {
rev[i] = (rev[i >> 1] >> 1) | (((long long)i & 1) << (l - 1));
}
for (int i = 0; i < d; i++) c[i] = a[i];
for (int i = d; i < _n; i++) c[i] = 0;
ntt(c, _n, 1);
ntt(b, _n, 1);
for (int i = 0; i < _n; i++) {
b[i] = (2 - 1LL * b[i] * c[i] % MOD + MOD) % MOD * b[i] % MOD;
}
ntt(b, _n, -1);
for (int i = d; i < _n; i++) {
b[i] = 0;
}
return;
}
int d[MAXN], e[MAXN];
void Deriv(int *a, int *b, int l) {
for (int i = 1; i < l; i++) {
b[i - 1] = 1LL * a[i] * i % MOD;
}
b[l - 1] = 0;
}
void Integ(int *a, int *b, int l) {
for (int i = 1; i < l; i++) {
b[i] = 1LL * a[i - 1] * qpow(i, MOD - 2) % MOD;
}
b[0] = 0;
}
void Ln(int *a, int *b, int k) {
for (int i = 0; i < 2 * k; i++) d[i] = 0;
inv(k, a, d);
Deriv(a, e, k);
int l = 0, _n = 1;
while (_n < (k << 1)) _n <<= 1, l++;
for (int i = 1; i < _n; i++) {
rev[i] = (rev[i >> 1] >> 1) | (((long long)i & 1) << (l - 1));
}
ntt(e, _n, 1);
ntt(d, _n, 1);
for (int i = 0; i < _n; i++) {
e[i] = 1LL * e[i] * d[i] % MOD;
}
ntt(e, _n, -1);
Integ(e, b, k);
}
int g[MAXN], h[MAXN];
void Exp(int *a, int *b, int k) {
if (k == 1) {
b[0] = 1;
return;
}
Exp(a, b, (k + 1) >> 1);
int l = 0, _n = 1;
while (_n < (k << 1)) _n <<= 1, l++;
for (int i = 1; i < _n; i++) {
rev[i] = (rev[i >> 1] >> 1) | (((long long)i & 1) << (l - 1));
}
for (int i = 0; i < (k << 1); i++) g[i] = h[i] = 0;
Ln(b, g, k);
for (int i = 0; i < k; i++) h[i] = a[i];
ntt(b, _n, 1);
ntt(h, _n, 1);
ntt(g, _n, 1);
for (int i = 0; i < _n; i++) {
b[i] = 1LL * (1LL - g[i] + h[i] + MOD) * b[i] % MOD;
}
ntt(b, _n, -1);
for (int i = k; i < _n; i++) {
b[i] = 0;
}
return;
}
void mult(int *a, int *b, int *c, int n) {
int _n = 1, l = 0;
while (_n < (n << 1)) _n <<= 1, l++;
for (int i = 1; i < _n; i++) {
rev[i] = (rev[i >> 1] >> 1) | (((1LL * i) & 1) << (l - 1));
}
ntt(a, _n, 1);
ntt(b, _n, 1);
for (int i = 0; i < _n; i++) {
c[i] = 1LL * a[i] * b[i] % MOD;
}
ntt(c, _n, -1);
}
int fl, tt[MAXN], rr[MAXN], gg[MAXN], pp[MAXN], kk[MAXN], ss[MAXN], yy[MAXN], hh[MAXN], ww[MAXN];
void clear() {
for (int i = 1; i < (n << 2); i++) {
b[i] = c[i] = h[i] = g[i] = d[i] = e[i] = 0;
tt[i] = rr[i] = kk[i] = ss[i] = yy[i] = hh[i] = 0;
}
}
void _clr() {
for (int i = 1; i < (n << 2); i++) {
ww[i] = pp[i] = gg[i] = tt[i] = rr[i] = kk[i] = ss[i] = yy[i] = hh[i] = 0;
}
}
void sqrt(int *a, int *b, int d) {
clear();
Ln(a, kk, d);
int inv2 = qpow(2, MOD - 2);
for (int i = 0; i < d; i++) {
kk[i] = (1LL * kk[i] * inv2) % MOD;
}
clear();
Exp(kk, b, d);
}
void pow(int *a, int *b, int k, int d) {
clear();
_clr();
Ln(a, kk, n);
clear();
for (int i = 0; i < d; i++) {
kk[i] = 1LL * kk[i] * k % MOD;
}
Exp(kk, b, d);
return;
}
void sin(int *a, int *b, int d) {
clear();
_clr();
for (int i = 0; i < d; i++) {
tt[i] = 1LL * a[i] * I % MOD;
}
Exp(tt, rr, d);
clear();
inv(d, rr, gg);
for (int i = 0; i < d; i++) {
b[i] = 1LL * (rr[i] - gg[i] + MOD) * qpow(I * 2, MOD - 2) % MOD;
}
}
void cos(int *a, int *b, int d) {
clear();
_clr();
for (int i = 0; i < d; i++) {
tt[i] = 1LL * a[i] * I % MOD;
}
Exp(tt, rr, d);
clear();
inv(d, rr, gg);
for (int i = 0; i < d; i++) {
b[i] = 1LL * (rr[i] + gg[i]) * qpow(2, MOD - 2) % MOD;
}
}
void arcsin(int *a, int *b, int d) {
clear();
_clr();
for (int i = 0; i < d; i++) {
ww[i] = gg[i] = a[i];
}
Deriv(a, tt, d);
mult(gg, ww, pp, d);
for (int i = 0; i < d; i++) {
pp[i] = (MOD - pp[i]) % MOD;
}
pp[0] = (pp[0] + 1) % MOD;
sqrt(pp, ss, d);
clear();
inv(d, ss, yy);
clear();
mult(tt, yy, hh, d);
clear();
Integ(hh, b, d);
}
void arctan(int *a, int *b, int d) {
clear();
_clr();
for (int i = 0; i < d; i++) {
ww[i] = gg[i] = a[i];
}
Deriv(a, tt, d);
mult(gg, ww, pp, d);
pp[0] = (1 + pp[0]) % MOD;
clear();
inv(d, pp, yy);
clear();
mult(tt, yy, hh, d);
clear();
Integ(hh, b, d);
}
int main() {
read(n), read(fl);
for (int i = 0; i < n; i++) {
read(a[i]);
}
/* Operator */
for (int i = 0; i < n; i++) {
write(f[i]), putchar(' ');
}
return 0;
}
文章作者: RiverFun
文章链接: https://stevebraveman.github.io/blog/2019/07/10/92/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 RiverFun

评论
目录