多项式板子

多项式理论和模板

理论部分

dft(fft/ntt)

对于多项式F(x)F(x)带入单位复根WnkW_n^k可以得到点值,然而求每一个点值消耗的时间是O(d(F))O(d(F))的,通过考虑分治法,把

F(x)=a0+a1x+...anxnF(x)=a_0+a_1\cdot x+...a_n\cdot x^n

进行奇偶分类,得到

G(x)=a0+a2x+...anx(n1)/2,H(x)=a1+a3x+...+anx(n1)/2G(x)=a_0+a_2 \cdot x+...a_{n} \cdot x^{(n-1)/2},H(x)=a_1+a_3 \cdot x+...+a_n \cdot x^{(n-1)/2}

那么可以得到

F(x)=G(x2)+xH(x2)F(x)=G(x^2)+x*H(x^2)

注意到偶数次单位复根的性质:

Wnk=Wnk+n/2W_n^k=-W_n^{k+n/2}

因为G和H都为x2x^2的多项式,所以这两个复根的值相同,那么只要计算出来 G(wn/2k)G(w_{n/2}^{k})H(wn/2k)H(w_{n/2}^{k}) 之后就可以算出来F(wnk)F(w_n^k)F(wnk+n/2)F(w_n^{k+n/2})的值,那么就可以进行递归计算了。ps.在mod n 域上的多项式其单位根可以是膜数的单位根,其与复数单位根性质类似。

位逆序置换

要对dft进行分治法加速,那就要让多项式项数变成2的幂次。

MTT

方法1:进行三次不同模数的NTT,然后就可以算出在其他模数下的NTT结果。

方法2:进行拆系数fft,可以提高fft的精度,以便其在模意义下有足够精度。

多项式求逆

对于多项式 F[x]G[x]=1F[x]*G[x]=1 ,假设 F[x]H[x]=1(modxn)F[x]*H[x]=1(\mod x^n) 那么就有

F[X](G[X]H[X])=0(modxn)F[X](G[X]-H[X])=0( \mod x^n)

考虑两边平方,就有

(G[X]H[X])2=0(modx2n)(G[X]-H[X])^2=0(\mod x^{2n})

左右同时乘以F[X]F[X]得到,

F[X]G[X]G[X]2F[X]G[X]H[X]+F[X]H[X]H[X]=1(modx2n)F[X]*G[X]*G[X]-2*F[X]*G[X]*H[X]+F[X]*H[X]*H[X]=1( \mod x^{2n})

因为G为F逆元,那么G[X]2H[X]+F[X]H[X]H[X]=0(modx2n)G[X]-2H[X]+F[X]*H[X]*H[X]=0(\mod x^{2n})因为H[0]=F[0].inv()H[0]=F[0].inv()那么通过n从1开始倍增,就可以求出G。
也可以通过牛顿迭代得到。

多项式除法

给定F(x)=G(x)Q(x)+R(x)F(x)=G(x)*Q(x)+R(x)

F(x)F(x),G(x)G(x)Q(x),R(x)Q(x),R(x)

其中F(x)F(x)为n阶多项式G(x)G(x)为m阶多项式R(x)R(x )为小于m阶的多项式。

那么对于F(1x)=G(1x)Q(1x)+R(1x)F(\frac {1}{x})=G(\frac{1}{x})*Q(\frac{1}{x})+R(\frac {1}{x})xnF(1x)=xmG(1x)xnmQ(1x)+xnm+1(xm1R(1x))x^nF(\frac{1}{x})=x^{m}G(\frac{1}{x})*x^{n-m}Q(\frac{1}{x})+x^{n-m+1}\cdot (x^{m-1}R(\frac{1}{x}))

因为xnF(1x)=Fr(x)x^nF(\frac{1}{x})=F_r(x)(Fr(x)F_r(x)代表翻转之后的F(x)F(x))

所以上面的式子可以化成Fr(x)=Gr(x)Qr(x)+xnm+1Rr(x)F_r(x)=G_r(x)*Q_r(x)+x^{n-m+1}R_r(x)

对其同时对于xnm+1x^{n-m+1}取模,得到Fr(x)=Gr(x)Qr(x)(modxnm+1)F_r(x)=G_r(x)*Q_r(x)(\mod x^{n-m+1})

因为G(x)G(x)已知,可以通过多项式求逆,得到Q(x)Q(x),然后通过原始公式得到R(x)R(x)。也可以通过牛顿迭代得到。

多项式exp

观察泰勒展开式:ef(x)=i=0nf(x)ii!e^{f(x)}=\sum_{i=0}^n\frac{f(x)^i}{i!}

定义f(i)f(i)为i个小球放入盒子的方案数,如果该盒子无序,那么其要除以i!。有些时候,计算集合中无顺序的方案数非常好算,但是有顺序的方案数非常难算,那么就可以通过exp的逆变换ln来计算。

多项式ln

ln,exp 的求法都可以通过构造函数并让其牛顿迭代得到。

模板部分

FFT模板

含convolution的模板

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

using i64 = long long;
using Z = std::complex<long double>;
const double pi = std::acos(-1);
std::vector<int> rev;
std::vector<Z> roots{(0, 1), (0, 1)};

void dft(std::vector<Z>& a) {
int n = a.size();

if (int(rev.size()) != n) {
rev.resize(n);
for (int i = 0; i < n; ++i) {
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? n >> 1 : 0);
}
}

for (int i = 0; i < n; ++i) {
if (rev[i] < i)std::swap(a[i], a[rev[i]]);
}
if (int(roots.size() < n)) {
int k = __builtin_ctz(roots.size());
roots.resize(n);
while ((1 << k) < n) {
Z e(cos(acos(-1) / (1 << k)), sin(acos(-1) / (1 << k)));
for (int i = 1 << (k - 1); i < (1 << k); i++) {
roots[i << 1] = roots[i];
roots[i << 1 | 1] = roots[i] * e;
}
k++;
}
}

for (int k = 1; k < n; k *= 2) {
for (int i = 0; i < n; i += 2 * k) {
for (int j = 0; j < k; j++) {
Z u = a[i + j];
Z v = a[i + j + k] * roots[k + j];
a[i + j] = u + v;
a[i + j + k] = u - v;
}
}
}
}

void idft(std::vector<Z>& a) {
int n = a.size();
std::reverse(a.begin() + 1, a.end());
dft(a);
}

struct Poly : public std::vector<i64> {
using std::vector<i64>::vector;

Poly() {}

Poly(const std::vector<i64>& a) : std::vector<i64>(a) {}

Poly(const std::initializer_list<i64>& a) : std::vector<i64>(a) {}

i64 operator [](int idx) const {
if (idx > size())return 0;
else return *(begin() + idx);
}

i64& operator [](int idx) { return *(begin() + idx); }

friend Poly operator +(const Poly& a, const Poly& b) {
Poly res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
res[i] = a[i] + b[i];
}
return res;
}

friend Poly operator -(const Poly& a, const Poly& b) {
Poly res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
res[i] = a[i] - b[i];
}
return res;
}

friend Poly operator *(const Poly& a, const Poly& b) {
if (a.size() == 0 || b.size() == 0) {
return Poly();
}
int sz = 1, tot = a.size() + b.size() - 1;
while (sz < tot) {
sz *= 2;
}
std::vector<Z> f(sz);
for (int i = 0; i < a.size(); ++i)f[i].real(a[i]);
for (int i = 0; i < b.size(); ++i)f[i].imag(b[i]);
dft(f);
for (int i = 0; i < sz; ++i) {
f[i] = f[i] * f[i];
f[i] /= sz, f[i] /= 2;
}
idft(f);
Poly ans(tot);
for (int i = 0; i < tot; ++i)ans[i] = f[i].imag() + 0.5;
return ans;
}

friend Poly operator *(i64 a, Poly b) {
for (int i = 0; i < int(b.size()); i++) {
b[i] *= a;
}
return b;
}

friend Poly operator *(Poly a, i64 b) {
for (int i = 0; i < int(a.size()); i++) {
a[i] *= b;
}
return a;
}

Poly operator <<(const int k) const {
auto b = *this;
b.insert(b.begin(), k, 0);
return b;
}

Poly operator >>(const int k) const {
if (size() <= k) {
return {};
}
return {begin() + k, end()};
}

Poly& operator >>=(const int k) { return (*this) = (*this) >> k; }

Poly& operator <<=(const int k) { return (*this) = (*this) << k; }

Poly modxk(int k) const {
k = std::min(k, (int) size());
return {begin(), begin() + k};
}

Poly& operator +=(const Poly& b) { return (*this) = (*this) + b; }

Poly& operator -=(const Poly& b) { return (*this) = (*this) - b; }

Poly& operator *=(const Poly& b) { return (*this) = (*this) * b; }
};

NTT模板

含有ln,exp,inv,integal,derivation,convolution的模板

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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445

template<typename T>
T inverse(T a, T b) {
T u = 0, v = 1;
while (a != 0) {
T t = b / a;
b -= t * a;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
assert(b == 1);
return u;
}

template<typename T>
T power(T a, int b) {
T ans = 1;
for (; b; a *= a, b >>= 1) {
if (b & 1)ans *= a;
}
return ans;
}

template<int Mod>
class Modular {
public:
using Type = int;

template<typename U>
static Type norm(U &x) {
Type v;
if (-Mod <= x && x < Mod) v = static_cast<Type>(x);
else v = static_cast<Type>(x % Mod);
if (v < 0) v += Mod;
return v;
}

constexpr Modular() : value() {}

int val() const { return value; }

Modular inv() const {
return Modular(inverse(value, Mod));
}

template<typename U>
Modular(const U &x) {
value = norm(x);
}

const Type &operator()() const {
return value;
}

template<typename U>
explicit operator U() const {
return static_cast<U>(value);
}

Modular &operator+=(const Modular &other) {
if ((value += other.value) >= Mod) value -= Mod;
return *this;
}

Modular &operator-=(
const Modular &other) {
if ((value -= other.value) < 0) value += Mod;
return *this;
}

template<typename U>
Modular &operator+=(const U &other) { return *this += Modular(other); }

template<typename U>
Modular &operator-=(const U &other) { return *this -= Modular(other); }

Modular &operator++() { return *this += 1; }

Modular &operator--() { return *this -= 1; }

Modular operator++(int) {
Modular result(*this);
*this += 1;
return result;
}

Modular operator--(int) {
Modular result(*this);
*this -= 1;
return result;
}

Modular operator-() const { return Modular(-value); }

template<class ISTREAM_TYPE>
friend ISTREAM_TYPE &operator>>(ISTREAM_TYPE &is, Modular &rhs) {
ll v;
is >> v;
rhs = Modular(v);
return is;
}

template<class OSTREAM_TYPE>
friend OSTREAM_TYPE &operator<<(OSTREAM_TYPE &os, const Modular &rhs) {
return os << rhs.val();
}

Modular &operator*=(const Modular &rhs) {
value = ll(value) * rhs.value % Mod;
return *this;
}

Modular &operator/=(const Modular &other) { return *this *= Modular(inverse(other.value, Mod)); }

friend const Type &abs(const Modular &x) { return x.value; }

friend bool operator==(const Modular &lhs, const Modular &rhs) { return lhs.x == rhs.x; }

friend bool operator<(const Modular &lhs, const Modular &rhs) { return lhs.x < rhs.x; }


bool operator==(const Modular &rhs) { return *this == rhs.value; }

template<typename U>
bool operator==(U rhs) { return *this == Modular(rhs); }

template<typename U>
friend bool operator==(U lhs, const Modular &rhs) { return Modular(lhs) == rhs; }

bool operator!=(const Modular &rhs) { return *this != rhs; }

template<typename U>
bool operator!=(U rhs) { return *this != rhs; }

template<typename U>
friend bool operator!=(U lhs, const Modular &rhs) { return lhs != rhs; }

bool operator<(const Modular &rhs) { return this->value < rhs.value; }

Modular operator+(const Modular &rhs) { return Modular(*this) += rhs; }

template<typename U>
Modular operator+(U rhs) { return Modular(*this) += rhs; }

template<typename U>
friend Modular operator+(U lhs, const Modular &rhs) { return Modular(lhs) += rhs; }

Modular operator-(const Modular &rhs) { return Modular(*this) -= rhs; }

template<typename U>
Modular operator-(U rhs) { return Modular(*this) -= rhs; }

template<typename U>
friend Modular operator-(U lhs, const Modular &rhs) { return Modular(lhs) -= rhs; }

Modular operator*(const Modular &rhs) { return Modular(*this) *= rhs; }

template<typename U>
Modular operator*(U rhs) { return Modular(*this) *= rhs; }

template<typename U>
friend Modular operator*(U lhs, const Modular &rhs) { return Modular(lhs) *= rhs; }

Modular operator/(const Modular &rhs) { return Modular(*this) /= rhs; }

template<typename U>
Modular operator/(U rhs) { return Modular(*this) /= rhs; }

template<typename U>
friend Modular operator/(U lhs, const Modular &rhs) { return Modular(lhs) /= rhs; }

private:
Type value;
};

constexpr int mod = 998244353;
using Z = Modular<mod>;


std::vector<int> rev;
std::vector<Z> roots{0, 1};

void dft(std::vector<Z> &a) {
int n = a.size();

if (int(rev.size()) != n) {
rev.resize(n);
for (int i = 0; i < n; ++i) {
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? n >> 1 : 0);
}
}

for (int i = 0; i < n; ++i) {
if (rev[i] < i)std::swap(a[i], a[rev[i]]);
}
if (int(roots.size() < n)) {
int k = __builtin_ctz(roots.size());
roots.resize(n);
while ((1 << k) < n) {
Z e = power(Z(3), (mod - 1) >> (k + 1));
for (int i = 1 << (k - 1); i < (1 << k); i++) {
roots[i << 1] = roots[i];
roots[i << 1 | 1] = roots[i] * e;
}
k++;
}
}

for (int k = 1; k < n; k *= 2) {
for (int i = 0; i < n; i += 2 * k) {
for (int j = 0; j < k; j++) {
Z u = a[i + j];
Z v = a[i + j + k] * roots[k + j];
a[i + j] = u + v;
a[i + j + k] = u - v;
}
}
}
}

void idft(std::vector<Z> &a) {
int n = a.size();
std::reverse(a.begin() + 1, a.end());
dft(a);
}

struct Poly : public std::vector<Z> {
using std::vector<Z>::vector;

Poly() = default;

explicit Poly(const std::vector<Z> &a) : vector(a) {}

friend Poly operator+(const Poly &a, const Poly &b) {
Poly res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
if (i < a.size())res[i] = a[i];
if (i < b.size())res[i] += b[i];
}
return res;
}

friend Poly operator-(const Poly &a, const Poly &b) {
Poly res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
if (i < a.size()) res[i] = a[i];
if (i < b.size()) res[i] -= b[i];
}

return res;
}

friend Poly operator*(const Poly &a, const Poly &b) {
if (a.empty() || b.empty()) {
return {};
}
int sz = 1, tot = a.size() + b.size() - 1;
while (sz < tot) {
sz *= 2;
}
std::vector<Z> f(a), g(b);
f.resize(sz), g.resize(sz);
dft(f), dft(g);
for (int i = 0; i < sz; ++i) {
f[i] = f[i] * g[i];
f[i] /= sz;
}
idft(f);
return Poly(f).modxk(tot);
}

friend Poly operator*(int a, Poly b) {
for (int i = 0; i < int(b.size()); i++) {
b[i] *= a;
}
return b;
}

friend Poly operator*(Poly a, int b) {
for (int i = 0; i < int(a.size()); i++) {
a[i] *= b;
}
return a;
}

Poly operator<<(const int k) const {
auto b = *this;
b.insert(b.begin(), k, 0);
return b;
}

Poly operator>>(const int k) const {
if (size() <= k) {
return {};
}
return Poly(begin() + k, end());
}

Poly &operator>>=(const int k) {
return (*this) = (*this) >> k;
}

Poly &operator<<=(const int k) {
return (*this) = (*this) << k;
}

Poly mulxk(int k) const {
return *this << k;
}

Poly modxk(int k) const {
k = std::min(k, (int) size());
return Poly(begin(), begin() + k);
}

Poly divxk(int k) const {
if (size() <= k) {
return {};
}
return Poly(begin() + k, end());
}

Poly &operator+=(const Poly &b) {
return (*this) = (*this) + b;
}

Poly &operator-=(const Poly &b) {
return (*this) = (*this) - b;
}

Poly &operator*=(const Poly &b) {
return (*this) = (*this) * b;
}


Poly deriv() const {
if (empty()) {
return {};
}
Poly res(size() - 1);
for (int i = 0; i < size() - 1; ++i) {
res[i] = (i + 1) * (*this)[i + 1];
}
return res;
}

Poly integr() const {
Poly res(size() + 1);
for (int i = 0; i < size(); ++i) {
res[i + 1] = (*this)[i] / (i + 1);
}
return res;
}

Poly inv(int m) const {
Poly x{(*this)[0].inv()};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{2} - modxk(k) * x)).modxk(k);
}
return x.modxk(m);
}

Poly log(int m) const {
return (deriv() * inv(m)).integr().modxk(m);
}

Poly exp(int m) const {
Poly x{Z(1)};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{Z(1)} - x.log(k) + modxk(k))).modxk(k);
}
return x.modxk(m);
}

Poly pow(int k, int m) const {
int i = 0;
while (i < size() && (*this)[i].val() == 0) {
i++;
}
if (i == size() || 1LL * i * k >= m) {
return Poly(m);
}
Z v = (*this)[i];
Poly f = divxk(i) * (v.inv().val());
return (f.log(m - i * k) * k).exp(m - i * k).mulxk(i * k) * power(v, k).val();
}

Poly sqrt(int m) const {
Poly x{Z(1)};
int k = 1;
while (k < m) {
k *= 2;
x = (x + (modxk(k) * x.inv(k)).modxk(k)) * ((mod + 1) / 2);
}
return x.modxk(m);
}

Poly mulT(Poly b) const {
if (b.empty()) {
return {};
}
int n = b.size();
std::reverse(b.begin(), b.end());
return ((*this) * b).divxk(n - 1);
}

std::vector<Z> eval(std::vector<Z> x) const {
if (empty()) {
return Poly(x.size());
}
const int n = std::max(int(x.size()), (int) size());
std::vector<Poly> q(4 * n);
std::vector<Z> ans(x.size());
x.resize(n);
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
q[p] = Poly{1, -x[l]};
} else {
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
q[p] = q[2 * p] * q[2 * p + 1];
}
};
build(1, 0, n);
std::function<void(int, int, int, const Poly &)> work = [&](int p, int l, int r, const Poly &num) {
if (r - l == 1) {
if (l < int(ans.size())) {
ans[l] = num[0];
}
} else {
int m = (l + r) / 2;
work(2 * p, l, m, num.mulT(q[2 * p + 1]).modxk(m - l));
work(2 * p + 1, m, r, num.mulT(q[2 * p]).modxk(r - m));
}
};
work(1, 0, n, mulT(q[1].inv(n)));
return ans;
}
};

好抄一点的板子:

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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using i64 = long long;
constexpr int P = 998244353;


template<class T>
T inverse(T a, T b) {
T u = 0, v = 1;
while (a != 0) {
T t = b / a;
b -= t * a;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
assert(b == 1);
return u;
}

template<class T>
T power(T a, i64 b) {
T ret = 1;
for (; b; b >>= 1, a = a * a)if (b & 1)ret = ret * a;
return ret;
}

int norm(int x) {
return x < 0 ? x += P : x >= P ? x -= P : x;
}

struct ModInt {
int a;

ModInt(int a = 0) : a(norm(a)) {} // assume a <= 2P
ModInt(i64 a) : a(norm(a % P)) {}

int val() const { return a; }

ModInt inv() const { return power(*this, P - 2); }

ModInt operator -() const { return ModInt(norm(P - a)); }

friend ModInt operator *(const ModInt& lhs, const ModInt& rhs) { return {i64(lhs.a) * rhs.a}; }

friend ModInt operator +(const ModInt& lhs, const ModInt& rhs) { return {lhs.a + rhs.a}; }

friend ModInt operator -(const ModInt& lhs, const ModInt& rhs) { return {lhs.a - rhs.a}; }

friend ModInt operator /(const ModInt& lhs, const ModInt& rhs) { return {i64(lhs.a) * rhs.inv()}; }

friend std::istream& operator >>(std::istream& is, ModInt& rhs) {
i64 x;
is >> x;
rhs = ModInt(x);
return is;
}

friend std::ostream& operator <<(std::ostream& os, const ModInt& rhs) { return os << rhs.val(); }
};

using Z = ModInt;
std::vector<int> rev;
std::vector<Z> roots{0, 1};

void dft(std::vector<Z>& a) {
int n = a.size();

if (int(rev.size()) != n) {
rev.resize(n);
for (int i = 0; i < n; ++i) {
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? n >> 1 : 0);
}
}

for (int i = 0; i < n; ++i) {
if (rev[i] < i)std::swap(a[i], a[rev[i]]);
}
if (int(roots.size() < n)) {
int k = __builtin_ctz(roots.size());
roots.resize(n);
while ((1 << k) < n) {
Z e = power(Z(3), (P - 1) >> (k + 1));
for (int i = 1 << (k - 1); i < (1 << k); i++) {
roots[i << 1] = roots[i];
roots[i << 1 | 1] = roots[i] * e;
}
k++;
}
}

for (int k = 1; k < n; k *= 2) {
for (int i = 0; i < n; i += 2 * k) {
for (int j = 0; j < k; j++) {
Z u = a[i + j];
Z v = a[i + j + k] * roots[k + j];
a[i + j] = u + v;
a[i + j + k] = u - v;
}
}
}
}

void idft(std::vector<Z>& a) {
int n = a.size();
std::reverse(a.begin() + 1, a.end());
dft(a);
}

struct Poly : public std::vector<Z> {
using std::vector<Z>::vector;

Poly() = default;

explicit Poly(const std::vector<Z>& a) : vector(a) {}

Z operator [](int idx) const {
if (idx < size()) {
return *(this->begin() + idx);
} else {
return 0;
}
}

Z& operator [](int idx) {
return *(this->begin() + idx);
}

friend Poly operator +(const Poly& a, const Poly& b) {
Poly res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
res[i] = a[i] + b[i];
}
return res;
}

friend Poly operator -(const Poly& a, const Poly& b) {
Poly res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
res[i] = a[i] - b[i];
}

return res;
}

friend Poly operator *(Poly a, Poly b) {
if (a.empty() || b.empty()) {
return {};
}
int sz = 1, tot = a.size() + b.size() - 1;
while (sz < tot) {
sz *= 2;
}
a.resize(sz), b.resize(sz);
dft(a), dft(b);
Z inv = (1 - P) / sz;
for (int i = 0; i < sz; ++i) {
a[i] = a[i] * b[i];
a[i] = a[i] * inv;
}
idft(a);
return Poly(a).modxk(tot);
}

friend Poly operator *(int a, Poly b) {
for (int i = 0; i < int(b.size()); i++) {
b[i] = b[i] * a;
}
return b;
}

friend Poly operator *(Poly a, int b) {
for (int i = 0; i < int(a.size()); i++) {
a[i] = a[i] * b;
}
return a;
}

Poly operator <<(const int k) const {
auto b = *this;
b.insert(b.begin(), k, 0);
return b;
}

Poly operator >>(const int k) const {
if (size() <= k) {
return {};
}
return Poly(begin() + k, end());
}

Poly mulxk(int k) const {
return *this << k;
}

Poly modxk(int k) const {
k = std::min(k, (int) size());
return Poly(begin(), begin() + k);
}

Poly divxk(int k) const {
if (size() <= k) {
return {};
}
return Poly(begin() + k, end());
}


Poly deriv() const {
if (empty()) {
return {};
}
Poly res(size() - 1);
for (int i = 0; i < size() - 1; ++i) {
res[i] = (i + 1) * (*this)[i + 1];
}
return res;
}

Poly integr() const {
Poly res(size() + 1);
for (int i = 0; i < size(); ++i) {
res[i + 1] = (*this)[i] / (i + 1);
}
return res;
}

Poly inv(int m) const {
Poly x{(*this)[0].inv()};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{2} - modxk(k) * x)).modxk(k);
}
return x.modxk(m);
}

Poly log(int m) const {
return (deriv() * inv(m)).integr().modxk(m);
}

Poly exp(int m) const {
Poly x{Z(1)};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{Z(1)} - x.log(k) + modxk(k))).modxk(k);
}
return x.modxk(m);
}

Poly pow(int k, int m) const {
int i = 0;
while (i < size() && (*this)[i].val() == 0) {
i++;
}
if (i == size() || 1LL * i * k >= m) {
return Poly(m);
}
Z v = (*this)[i];
Poly f = divxk(i) * (v.inv().val());
return (f.log(m - i * k) * k).exp(m - i * k).mulxk(i * k) * power(v, k).val();
}

Poly sqrt(int m) const {
Poly x{Z(1)};
int k = 1;
while (k < m) {
k *= 2;
x = (x + (modxk(k) * x.inv(k)).modxk(k)) * ((P + 1) / 2);
}
return x.modxk(m);
}

Poly mulT(Poly b) const {
if (b.empty()) {
return {};
}
int n = b.size();
std::reverse(b.begin(), b.end());
return ((*this) * b).divxk(n - 1);
}

std::vector<Z> eval(std::vector<Z> x) const {
if (empty()) {
return Poly(x.size());
}
const int n = std::max(int(x.size()), (int) size());
std::vector<Poly> q(4 * n);
std::vector<Z> ans(x.size());
x.resize(n);
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
q[p] = Poly{1, -x[l]};
} else {
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
q[p] = q[2 * p] * q[2 * p + 1];
}
};
build(1, 0, n);
std::function<void(int, int, int, const Poly&)> work = [&](int p, int l, int r, const Poly& num) {
if (r - l == 1) {
if (l < int(ans.size())) {
ans[l] = num[0];
}
} else {
int m = (l + r) / 2;
work(2 * p, l, m, num.mulT(q[2 * p + 1]).modxk(m - l));
work(2 * p + 1, m, r, num.mulT(q[2 * p]).modxk(r - m));
}
};
work(1, 0, n, mulT(q[1].inv(n)));
return ans;
}
};

MTT模板

含 convolution,inv 拆系数fft实现的板子

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

template<typename T>
T inverse(T a, T b) {
T u = 0, v = 1;
while (a != 0) {
T t = b / a;
b -= t * a;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
assert(b == 1);
return u;
}

template<typename T>
T power(T a, long long b, int mod) {
T ans = 1;
for (; b; a = 1ll * a * a % mod, b >>= 1) {
if (b & 1)ans = 1ll * ans * a % mod;
}
return ans;
}

int mod;
using Z = std::complex<long double>;
using i64 = long long;
const double pi = std::acos(-1);
std::vector<int> rev;
std::vector<Z> roots{(0, 1), (0, 1)};

int getsz(int x) {
int sz = 1;
while (sz < x)sz *= 2;
return sz;
}

void dft(std::vector<Z>& a) {
int n = a.size();

if (int(rev.size()) != n) {
rev.resize(n);
for (int i = 0; i < n; ++i) {
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? n >> 1 : 0);
}
}

for (int i = 0; i < n; ++i) {
if (rev[i] < i)std::swap(a[i], a[rev[i]]);
}
if (int(roots.size() < n)) {
int k = __builtin_ctz(roots.size());
roots.resize(n);
while ((1 << k) < n) {
Z e(cos(acos(-1) / (1 << k)), sin(acos(-1) / (1 << k)));
for (int i = 1 << (k - 1); i < (1 << k); i++) {
roots[i << 1] = roots[i];
roots[i << 1 | 1] = roots[i] * e;
}
k++;
}
}

for (int k = 1; k < n; k *= 2) {
for (int i = 0; i < n; i += 2 * k) {
for (int j = 0; j < k; j++) {
Z u = a[i + j];
Z v = a[i + j + k] * roots[k + j];
a[i + j] = u + v;
a[i + j + k] = u - v;
}
}
}
}

void idft(std::vector<Z>& a) {
int n = a.size();
std::reverse(a.begin() + 1, a.end());
dft(a);
}

template<typename U>
static U norm(U& x, int Mod) {
if (-Mod <= x && x < Mod) x = static_cast<U>(x);
else x = static_cast<U>(x % Mod);
if (x < 0) x += Mod;
return x;
}

struct Poly : public std::vector<i64> {
using std::vector<i64>::vector;

i64 operator [](int idx) const {
if (idx > size())return 0;
else return *(begin() + idx);
}

i64& operator [](int idx) { return *(begin() + idx); }

friend Poly operator +(const Poly& a, const Poly& b) {
Poly res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
res[i] = a[i] + b[i];
norm(res[i], mod);
}
return res;
}

friend Poly operator -(const Poly& a, const Poly& b) {
Poly res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
res[i] = a[i] - b[i];
norm(res[i], mod);
}
return res;
}

friend Poly operator *(int a, Poly b) {
for (int i = 0; i < int(b.size()); i++) {
b[i] *= a;
}
return b;
}

friend Poly operator *(Poly a, int b) {
for (int i = 0; i < int(a.size()); i++) {
a[i] *= b;
}
return a;
}

Poly operator <<(const int k) const {
auto b = *this;
b.insert(b.begin(), k, 0);
return b;
}

Poly operator >>(const int k) const {
if (size() <= k) {
return {};
}
return Poly(begin() + k, end());
}

Poly& operator >>=(const int k) {
return (*this) = (*this) >> k;
}

Poly& operator <<=(const int k) {
return (*this) = (*this) << k;
}

Poly mulxk(int k) const {
return *this << k;
}

Poly modxk(int k) const {
k = std::min(k, (int) size());
return Poly(begin(), begin() + k);
}

Poly divxk(int k) const {
if (size() <= k) {
return {};
}
return Poly(begin() + k, end());
}

Poly& operator +=(const Poly& b) {
return (*this) = (*this) + b;
}

Poly& operator -=(const Poly& b) {
return (*this) = (*this) - b;
}

Poly& operator *=(const Poly& b) {
return (*this) = (*this) * b;
}


Poly deriv() const {
if (empty()) {
return {};
}
Poly res(size() - 1);
for (int i = 0; i < size() - 1; ++i) {
res[i] = (i + 1) * (*this)[i + 1] % mod;
}
return res;
}

Poly integr() const {
Poly res(size() + 1);
for (int i = 0; i < size(); ++i) {
res[i + 1] = (*this)[i] * power(i + 1, mod - 2, mod);
}
return res;
}

friend Poly operator *(const Poly& x, const Poly& y) {
if (x.empty() || y.empty())return {};
Poly a(x), b(y);
int len = a.size() + b.size() - 1, n = getsz(len);
vector<Z> f(n), g(n), p(n), q(n);
for (int i = 0; i < a.size(); i++)
f[i] = Z(a[i] >> 15, a[i] & 0x7fff);
for (int i = 0; i < b.size(); i++)
g[i] = Z(b[i] >> 15, b[i] & 0x7fff);
dft(f), dft(g);
for (int i = 0; i < n; ++i) {
int r = (n - i) & (n - 1);
p[i] = Z(0.5 * (f[i].real() + f[r].real()), 0.5 * (f[i].imag() - f[r].imag())) * g[i];
q[i] = Z(0.5 * (f[i].imag() + f[r].imag()), 0.5 * (f[r].real() - f[i].real())) * g[i];
}
idft(p), idft(q);
for (int i = 0; i < n; ++i)p[i] /= n, q[i] /= n;
a.resize(len);
for (int i = 0; i < len; i++) {
long long X, Y, Z, W;
X = p[i].real() + 0.5, Y = p[i].imag() + 0.5;
Z = q[i].real() + 0.5, W = q[i].imag() + 0.5;
a[i] = ((X % mod << 30) + ((Y + Z) % mod << 15) + W) % mod;
}
return a;
}

Poly inv(int m) const {
Poly x{(power((*this)[0], mod - 2, mod))};
int k = 1;
while (k < m) {
k <<= 1;
x = (x * (Poly{2} - modxk(k) * x)).modxk(k);
}
return x;
}
};


多项式板子
https://mrxyan6.github.io/2022/09/04/poly-templates/
作者
mrx
发布于
2022年9月4日
许可协议