多项式求逆

我们记\(deg(A)\)为多项式\(A(x)\)的度,即为\(A(x)\)的最高项系数 + 1

对于多项式\(A(x)\),如果存在\(B(x)\)满足\(deg(B) \le deg(A)\),且

\[A(x)B(x) \equiv 1 \pmod {x^{n}} \]

我们称\(B(x)\)\(A(x)\)在模\(x^n\)意义下的逆元,记作\(A^{-1}(x)\)

求解过程##

考虑递归求解
\(n = 1\)时,\(A(x) \equiv c \pmod x\),显然\(A^{-1}(x)\)就是\(c^{-1}\)

倘若我们要计算

\[A(x)B(x) \equiv 1 \pmod {x^n} \]

而已经计算出

\[A(x)B'(x) \equiv 1 \pmod {x^{\lceil \frac{n}{2} \rceil}} \]

我们要求的\(B(x)\)当然也满足

\[A(x)B(x) \equiv 1 \pmod {x^{\lceil \frac{n}{2} \rceil}} \]

两式相减

\[A(x)(B(x) - B'(x)) \equiv 0 \pmod {x^{\lceil \frac{n}{2} \rceil}} \]

\[B(x) - B'(x) \equiv 0 \pmod {x^{\lceil \frac{n}{2} \rceil}} \]

两边平方,由于对于平方后的多项式\(C(x)\),其系数\(c_i = \sum\limits_{j = 0}^{i} b_j*b'_{i - j}\),必有一项小于\(\lceil \frac{n}{2} \rceil\)而使\(c_i = 0\)
所以平方后放到\(\mod x^{n}\)意义下依然成立

\[B^2(x) + B'^2(x) - 2B(x)B'(x) \equiv 0 \pmod {x^{n}} \]

两边乘\(A(x)\)

\[B(x) + A(x)B'^2(x) - 2B'(x) \equiv 0 \pmod {x^{n}} \]

得到

\[B(x) \equiv B'(x)(2 - A(x)B'(x)) \pmod {x^{n}} \]

可以使用\(fft\)优化成\(O(nlogn)\)
总时间复杂度\(T(n) = T(\lceil \frac{n}{2} \rceil) + O(nlogn) = O(nlogn)\)

模板:洛谷P4238

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 400005,maxm = 100005,INF = 1000000000;
inline int read(){
	int out = 0,flag = 1; char c = getchar();
	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
	return out * flag;
}
const int G = 3,P = 998244353;
inline int qpow(int a,int b){
	int re = 1;
	for (; b; b >>= 1,a = 1ll * a * a % P)
		if (b & 1) re = 1ll * re * a % P;
	return re;
}
int a[maxn],b[maxn],c[maxn],R[maxn];
void NTT(int* a,int n,int f){
	for (int i = 0; i < n; i++) if (i < R[i]) swap(a[i],a[R[i]]);
	for (int i = 1; i < n; i <<= 1){
		int gn = qpow(G,(P - 1) / (i << 1));
		for (int j = 0; j < n; j += (i << 1)){
			int g = 1,x,y;
			for (int k = 0; k < i; k++,g = 1ll * g * gn % P){
				x = a[j + k]; y = 1ll * g * a[j + k + i] % P;
				a[j + k] = (x + y) % P; a[j + k + i] = ((x - y) % P + P) % P;
			}
		}
	}
	if (f == 1) return;
	int nv = qpow(n,P - 2); reverse(a + 1,a + n);
	for (int i = 0; i < n; i++) a[i] = 1ll * a[i] * nv % P;
}
void inv(int deg,int* a,int* b){
	if (deg == 1){b[0] = qpow(a[0],P - 2); return;}
	inv((deg + 1) >> 1,a,b);
	int L = 0,n = 1;
	while (n < (deg << 1)) n <<= 1,L++;
	for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
	for (int i = 0; i < deg; i++) c[i] = a[i];
	for (int i = deg; i < n; i++) c[i] = 0;
	NTT(c,n,1); NTT(b,n,1);
	for (int i = 0; i < n; i++)
		b[i] = 1ll * ((2ll - 1ll * c[i] * b[i] % P) + P) % P * b[i] % P;
	NTT(b,n,-1);
	for (int i = deg; i < n; i++) b[i] = 0;
}
int main(){
	int n = read();
	for (int i = 0; i < n; i++) a[i] = read();
	inv(n,a,b);
	for (int i = 0; i < n; i++) printf("%d ",b[i]);
	return 0;
}

posted @ 2018-05-16 11:38  Mychael  阅读(1824)  评论(0编辑  收藏  举报