多项式求逆学习笔记

其实这玩意儿之前学过,不过感觉挺有趣的就再拿出来写篇笔记。

$$
\begin{align}
A B_n &\equiv 1 \pmod {x^n} \\
A B_{\frac n2} &\equiv 1 \pmod {x^{\frac n2}} \\
\\
B_n - B_{\frac n2} &\equiv 0 \pmod {x^{\frac n2}} \\
B_n^2 - B_n B_{\frac n2} + B_{\frac n2}^2 &\equiv 0 \pmod {x^n} \\
A(B_n^2 - 2B_n B_{\frac n2} + B_{\frac n2}^2) &\equiv 0 \pmod {x^n} \\
B_n &\equiv 2B_{\frac n2} - AB_{\frac n2}^2 \pmod {x^n}
\end{align}
$$

代码:

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
// =================================
// author: memset0
// date: 2019.01.26 14:26:03
// 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, k, lim, inv_lim;
int a[N], b[N], c[N], f[N], g[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 main() {
read(n);
for (int i = 0; i < n; i++) read(f[i]);
g[0] = inv(f[0]);
for (int len = 2, k = 2; (len >> 1) <= n; len <<= 1, ++k) {
lim = len << 1, inv_lim = inv(lim);
for (int i = 0; i < len; i++) a[i] = f[i], b[i] = g[i];
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;
}
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 < lim; i++) a[i] = (ll)a[i] * inv_lim % mod;
for (int i = 0; i < len; i++) g[i] = ((ll)(g[i] << 1) - a[i] + mod) % mod;
}
for (int i = 0; i < n; i++) print(g[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

×