「多项式乘法逆」

前置芝士

多项式乘法

基本问题

给定一个 \(n\) 次多项式 \(F(x)\),求 \(G(x)\) 满足:

\[F(x)\times G(x)\equiv 1\mod x^n \]

假设有一个 \(0\) 次多项式 \(F(x)\),易得 \(F(x)\)\(G(x)\) 的逆元, 这给我们提供了一个分治的思路。

假设已有:

\[G'(x)\equiv F(x)\mod x^{\left \lceil \frac {n}{2} \right \rceil} \]

  • 考虑为什么要向上取整

我们分治的思路是得到两个区间来合并到一个区间,我们要保证合并后的区间长度要大于 \(n\)

则有:

\[G(x)-G'(x)\equiv 0\mod x^{\left \lceil \frac {n}{2} \right \rceil} \]

两边同时平方:

\[(G(x)-G'(x))^2\equiv 0\mod x^n \]

\[G^2(x)-2G(x)G'(x)+G'^2(x)\equiv 0\mod x^n \]

两边同乘 \(F(x)\)

\[F(x)G^2(x)-2F(x)G(x)G'(x)+F(x)G'^2(x)\equiv 0\mod x^n \]

\[\because F(x)G(x)\equiv 1\mod x^n \]

\[G(x)-2G'(x)+F(x)G'^2(x)\equiv 0\mod x^n \]

\[G(x)\equiv 2G'(x)-F(x)G'^2(x)\mod x^n \]

最后递归求解即可

代码
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

const int maxn = 3e5 + 50, INF = 0x3f3f3f3f, mod = 998244353, inv3 = 332748118;

inline int read () {
	register int x = 0, w = 1;
	register char ch = getchar ();
	for (; ch < '0' || ch > '9'; ch = getchar ()) if (ch == '-') w = -1;
	for (; ch >= '0' && ch <= '9'; ch = getchar ()) x = x * 10 + ch - '0';
	return x * w;
}

inline void write (register int x) {
	if (x / 10) write (x / 10);
	putchar (x % 10 + '0');
}

int n, f[maxn], g[maxn], rev[maxn], res[maxn], a[maxn], b[maxn];

inline int qpow (register int a, register int b, register int ans = 1) {
	for (; b; b >>= 1, a = 1ll * a * a % mod) 
		if (b & 1) ans = 1ll * ans * a % mod;
	return ans;
}

inline void NTT (register int len, register int * a, register int opt) {
	for (register int i = 1; i < len; i ++) if (i < rev[i]) swap (a[i], a[rev[i]]);
	for (register int d = 1; d < len; d <<= 1) {
		register int w1 = qpow (opt, (mod - 1) / (d << 1));
		for (register int i = 0; i < len; i += d << 1) {
			register int w = 1;
			for (register int j = 0; j < d; j ++, w = 1ll * w * w1 % mod) {
				register int x = a[i + j], y = 1ll * w * a[i + j + d] % mod;
				a[i + j] = (x + y) % mod, a[i + j + d] = (x - y + mod) % mod;
			}
		}
	}
}

inline void Poly_Inv (register int d, register int * a, register int * b) {
	if (d == 1) return b[0] = qpow (a[0], mod - 2), void (); // 长度为1,只有常数项
	Poly_Inv ((d + 1) >> 1, a, b); // 向下递归
	register int len = 1, bit = 0;
	while (len <= d << 1) len <<= 1, bit ++;
	for (register int i = 0; i < len; i ++) res[i] = 0, rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << bit - 1);
	for (register int i = 0; i < d; i ++) res[i] = a[i];
	NTT (len, res, 3), NTT (len, b, 3);
	for (register int i = 0; i < len; i ++) b[i] = ((2ll * b[i] % mod - 1ll * res[i] * b[i] % mod * b[i] % mod) % mod + mod) % mod; // 套公式
	NTT (len, b, inv3);
	register int inv = qpow (len, mod - 2);
	for (register int i = 0; i < d; i ++) b[i] = 1ll * b[i] * inv % mod;
	for (register int i = d; i < len; i ++) b[i] = 0; // 记得清零,下次NTT要用
}

inline void P4238 () {
	n = read() - 1;
	for (register int i = 0; i <= n; i ++) f[i] = read(); Poly_Inv (n + 1, f, g);
	for (register int i = 0; i <= n; i ++) printf ("%d ", g[i]); putchar ('\n');
}

int main () {
	return P4238 (), 0;
}
posted @ 2020-12-29 19:41  Rubyonlу  阅读(124)  评论(0编辑  收藏  举报