分治 NTT 学习笔记

memset0 又来水博客了…

第一种做法是正常的分治 NTT ,想必大家都会,就不废话了;
第二种做法是多项式求逆,想必大家也会,就摆几个式子了。

令 $F = \sum\limits_{x=0}^{\infty} x^i f_i$ , $G = \sum\limits_{x=0}^{\infty} x^i g_i$ ,则易得 $F * G = F - 1$ ,故 $F = (1 - G)^{-1}$ ,多项式求逆即可。

代码1:

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
// =================================
// author: memset0
// date: 2019.01.19 09:43:04
// website: https://memset0.cn/
// =================================
#include <bits/stdc++.h>
#define ll long long
namespace ringo {
template <class T> inline void read(T &x) {
x = 0; register char c = getchar(); register bool f = 0;
while (!isdigit(c)) f ^= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (f) x = -x;
}
template <class T> inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar('0' + x % 10);
}
template <class T> inline void print(T x, char c) { print(x), putchar(c); }

const int N = 4e5 + 10, mod = 998244353;
int n, m;
int f[N], g[N], a[N], b[N], c[N], w[N], rev[N];

int inv(int x) { return !x || x == 1 ? 1 : (ll)(mod - mod / x) * inv(mod % x) % mod; }
inline int fpow(int a, int b) {
int s = 1;
for (; b; b >>= 1, a = (ll)a * a % mod)
if (b & 1) s = (ll)s * a % mod;
return s;
}

void ntt(int *a, int lim) {
for (int i = 0; i < lim; i++) if (i < rev[i]) std::swap(a[i], a[rev[i]]);
for (int len = 1; len < lim; len <<= 1)
for (int i = 0; i < lim; i += (len << 1))
for (int j = 0; j < len; j++) {
int x = a[i + j], y = (ll)w[len + j] * a[i + j + len] % mod;
a[i + j] = (x + y) % mod, a[i + j + len] = (x - y + mod) % mod;
}
}

void mul(int *a, int *b, int *c, int n, int m) {
int lim = 1, k = 0, inv_lim;
while (lim <= (n + m)) lim <<= 1, ++k;
for (int i = 0; i < lim; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (k - 1));
for (int i = n; i < lim; i++) a[i] = 0;
for (int i = m; i < lim; i++) b[i] = 0;
ntt(a, lim), ntt(b, lim);
for (int i = 0; i < lim; i++) a[i] = (ll)a[i] * b[i] % mod;
std::reverse(a + 1, a + lim), ntt(a, lim), inv_lim = inv(lim);
for (int i = 0; i < n + m - 1; i++) c[i] = (ll)a[i] * inv_lim % mod;
}

void solve(int l, int r) {
if (l == r) return (void)(f[l] += l == 0);
int mid = (l + r) >> 1;
solve(l, mid);
for (int i = 0; i <= mid - l; i++) a[i] = f[i + l];
for (int i = 0; i < r - l + 1; i++) b[i] = g[i];
mul(a, b, c, mid - l + 1, r - l + 1);
solve(mid + 1, r);
}

void main() {
read(n);
for (int len = 1, wn; (len << 1) < N; len <<= 1) {
wn = fpow(3, (mod - 1) / (len << 1)), w[len] = 1;
for (int i = 1; i < len; i++) w[len + i] = (ll)wn * w[len + i - 1] % mod;
}
for (int i = 1; i < n; i++) read(g[i]);
solve(0, n - 1);
for (int i = 0; i < n; i++) print(f[i], " \n"[i == n - 1]);
}

} signed main() { return ringo::main(), 0; }

代码2:

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
// =================================
// author: memset0
// date: 2019.01.27 17:03:22
// website: https://memset0.cn/
// =================================
#include <bits/stdc++.h>
#define ll long long
namespace ringo {
template <class T> inline void read(T &x) {
x = 0; register char c = getchar(); register bool f = 0;
while (!isdigit(c)) f ^= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (f) x = -x;
}
template <class T> inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar('0' + x % 10);
}
template <class T> inline void print(T x, char c) { print(x), putchar(c); }

const int N = 4e5 + 10, mod = 998244353;
int n, lim;
int f[N], g[N], a[N], b[N], w[N], rev[N];

int inv(int x) { return !x || x == 1 ? 1 : (ll)(mod - mod / x) * inv(mod % x) % mod; }
inline int fpow(int a, int b) { int s = 1; for (; b; b >>= 1, a = (ll)a * a % mod) if (b & 1) s = (ll)s * a % mod; return s; }

void ntt(int *a) {
for (int i = 0; i < lim; i++) if (i < rev[i]) std::swap(a[i], a[rev[i]]);
for (int len = 1; len < lim; len <<= 1)
for (int i = 0; i < lim; i += (len << 1))
for (int j = 0; j < len; j++) {
int x = a[i + j], y = (ll)w[j + len] * a[i + j + len] % mod;
a[i + j] = (x + y) % mod, a[i + j + len] = (x - y + mod) % mod;
}
}

void polyInv(int *f, int *g) {
g[0] = inv(f[0]);
for (int len = 2, k = 2, inv_lim; (len >> 1) < n; len <<= 1, ++k) {
lim = len << 1, inv_lim = inv(lim);
for (int i = 0; i < lim; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (k - 1));
for (int len = 1, wn; len < lim; len <<= 1) {
wn = fpow(3, (mod - 1) / (len << 1)), w[len] = 1;
for (int i = 1; i < len; i++) w[i + len] = (ll)w[i + len - 1] * wn % mod;
}
for (int i = 0; i < len; i++) a[i] = f[i], b[i] = g[i];
ntt(a), ntt(b);
for (int i = 0; i < lim; i++) a[i] = (ll)a[i] * b[i] % mod * b[i] % mod;
std::reverse(a + 1, a + lim), ntt(a);
for (int i = 0; i < len; i++) g[i] = ((g[i] << 1) - (ll)inv_lim * a[i] % mod + mod) % mod;
}
}

void main() {
read(n), g[0] = 1;
for (int i = 1, x; i < n; i++) read(x), g[i] = x ? mod - x : 0;
polyInv(g, f);
for (int i = 0; i < n; i++) print(f[i], " \n"[i == n - 1]);
}

} signed main() { return ringo::main(), 0; }

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×