多项式求逆
首先定义多项式的度数 \(degA\) 为多项式 \(A(x)\) 的最高次数
那么多项式 \(A(x)\) 的逆即为存在多项式 \(B(x)\) 使得条件满足:
求解过程
假设存在多项式 \(A(x)\) ,以及其逆 \(B(x)\) 满足条件 ,那么必定有\(A(x)B(x) \equiv 1 \pmod{x^{\left\lceil\frac{n}{2}\right\rceil}} \ ... \ (1)\) ,因为 \(x^n\) 是 \(x^{\left\lceil\frac{n}{2}\right\rceil}\) 的倍数
并且存在 \(A(x)B'(x) \equiv 1 \pmod{x^{\left\lceil\frac{n}{2}\right\rceil}} \ ... \ (2)\)
\((1) - (2)\) ,得
上一步解释一下,是两边平方
至于为什么模数也需要平方,是因为 \((3)\) 式满足左边的多项式在其模意义下为 \(0\) ,且式子恒成立,故其除了第 \(x^{\left\lceil\frac{n}{2}\right\rceil}\) 项其余系数皆为 \(0\) ,那么现在考虑 \(0 \le x \le n - 1\) ,又因为 \(a_i = \sum\limits_{j = 1}^i a_j b_{i - j}\) ,所以必定存在 \(i\) 或者 \(i - j\) 小于 \(x^{\left\lceil\frac{n}{2}\right\rceil}\) ,故 \(a_i = 0\) ,所以平方无妨
同乘 \(A(x)\) ,移项,得
在递归过程中最后会递归到 \(n = 1\) ,那么此时 \(A(x)B''(x) \equiv c \pmod{x}\) ,取 \(c^{- 1}\) 就好了
那么再用 \(FFT\) 优化就可以做到 \(O (n \log n)\) 求解
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#define MOD 998244353
#define g 3
using namespace std;
typedef long long LL;
const int MAXN = 4e05 + 10;
LL power (LL x, int p) {
LL cnt = 1;
while (p) {
if (p & 1)
cnt = cnt * x % MOD;
x = x * x % MOD;
p >>= 1;
}
return cnt;
}
const LL invg = power (g, MOD - 2);
int N;
LL f[MAXN], invf[MAXN]= {0};
LL A[MAXN]= {0}, B[MAXN]= {0};
int oppo[MAXN]= {0};
int limit;
void NTT (LL* a, int inv) {
for (int i = 0; i < limit; i ++)
if (i < oppo[i])
swap (a[i], a[oppo[i]]);
for (int mid = 1; mid < limit; mid <<= 1) {
LL omega = power (inv == 1 ? g : invg, (MOD - 1) / (mid << 1));
for (int n = mid << 1, j = 0; j < limit; j += n) {
LL x = 1;
for (int k = 0; k < mid; k ++, x = x * omega % MOD) {
LL a1 = a[j + k], xa2 = x * a[j + k + mid] % MOD;
a[j + k] = (a1 + xa2) % MOD;
a[j + k + mid] = (a1 - xa2 + MOD) % MOD;
}
}
}
}
void Inverse (int deg, LL* ps) {
if (deg == 1) {
ps[0] = power (f[0], MOD - 2);
return ;
}
Inverse ((deg + 1) >> 1, ps);
int n, lim;
for (n = 1, lim = 0; n < (deg << 1); n <<= 1, lim ++);
limit = n;
for (int i = 0; i < limit; i ++)
oppo[i] = (oppo[i >> 1] >> 1) | ((i & 1) << (lim - 1));
for (int i = 0; i < limit; i ++)
A[i] = B[i] = 0;
for (int i = 0; i < deg; i ++)
A[i] = f[i];
for (int i = 0; i < deg << 1; i ++)
B[i] = ps[i];
NTT (A, 1), NTT (B, 1);
for (int i = 0; i < limit; i ++)
B[i] = B[i] * ((2ll - A[i] * B[i] % MOD + MOD) % MOD) % MOD;
NTT (B, - 1);
LL invn = power (n, MOD - 2);
for (int i = 0; i < deg; i ++)
ps[i] = B[i] * invn % MOD;
}
int getnum () {
int num = 0;
char ch = getchar ();
while (! isdigit (ch))
ch = getchar ();
while (isdigit (ch))
num = (num << 3) + (num << 1) + ch - '0', ch = getchar ();
return num;
}
int main () {
N = getnum ();
for (int i = 0; i < N; i ++)
f[i] = getnum ();
Inverse (N, invf);
for (int i = 0; i < N; i ++) {
if (i > 0)
putchar (' ');
printf ("%lld", invf[i]);
}
puts ("");
return 0;
}
/*
5
1 6 3 4 9
*/
/*
10
2 3 3 3 1233 211 23 3 3 322
*/