「多项式除法」

前置知识

多项式乘法

多项式乘法逆

基本问题

给定一个 \(n\) 次多项式 \(F(x)\) 和一个 \(m\) 次多项式 \(G(x)\),求 \(A(x)\)\(B(x)\) 满足

\[F(x)=A(x)G(x)+B(x) \]

显然 \(A(x)\) 的次数为 \(n - m\)\(B(x)\) 的次数 \(\leq m - 1\)

新定义

\[A_R(x)=x^nA(\frac{1}{x}) \]

容易发现 \(A_R(x)\) 的系数是 \(A(x)\) 的系数 \(reverse\) 得到的。

简单证明

\[A(x)=\sum^{n}_{i=0}a_ix^i \]

\[A(\frac{1}{x})=\sum^{n}_{i=0}a_ix^{-i} \]

\[A_R(x)=x^nA(\frac{1}{x})=\sum^{n}_{i=0}a_ix^{n-i}=\sum^{n}_{i=0}a_{n-i}x^i \]

\[A_R(x)=\sum^{n}_{i=0}a_{n-i}x^i \]

证毕

\[\because F(x)=A(x)G(x)+B(x) \]

\[F(\frac{1}{x})=A(\frac{1}{x})G(\frac{1}{x})+B(\frac{1}{x}) \]

两边同乘 \(x^n\)

\[x^nF(\frac{1}{x})=x^{n-m}A(\frac{1}{x})x^mG(\frac{1}{x})+x^{n-m+1}x^{m-1}B(\frac{1}{x}) \]

\[F_R(x)=A_R(x)G_R(x)+x^{n-m+1}B_R(x) \]

\[F_R(x)\equiv A_R(x)G_R(x)(mod\;x^{n-m+1}) \]

\[A_R(x)\equiv F_R(x)G_R^{-1}(x)(mod\;x^{n-m+1}) \]

然后通过 \(reverse\) 操作得到 \(A(x)\)

\[B(x)=F(x)-A(x)G(x) \]

代码
#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, m;
int f[maxn], g[maxn];
int fr[maxn], gr[maxn], ngr[maxn];
int rev[maxn], res[maxn];
int 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 ();
	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;
}

inline void Division (register int * f, register int * g) { // 除法
	Poly_Inv (n - m + 1, gr, ngr);
	register int len = 1, bit = 0;
	while (len <= 2 * n - m + 1) len <<= 1, bit ++;
	for (register int i = 0; i < len; i ++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << bit - 1);
	NTT (len, fr, 3), NTT (len, ngr, 3);
	for (register int i = 0; i < len; i ++) A[i] = 1ll * fr[i] * ngr[i] % mod;
	NTT (len, A, inv3);
	register int inv = qpow (len, mod - 2);
	for (register int i = n - m + 1; i < len; i ++) A[i] = 0;
	for (register int i = n - m; i >= 0; i --) printf ("%d ", A[i] = 1ll * A[i] * inv % mod); putchar ('\n');
	reverse (A, A + n - m + 1);
}

inline void Remainder (register int * f, register int * g) { // 取膜
	register int len = 1, bit = 0;
	while (len <= n) len <<= 1, bit ++;
	for (register int i = 0; i < len; i ++) res[i] = 0, rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << bit - 1);
	NTT (len, A, 3), NTT (len, g, 3);
	for (register int i = 0; i < len; i ++) res[i] = 1ll * A[i] * g[i] % mod;
	NTT (len, res, inv3);
	register int inv = qpow (len, mod - 2);
	for (register int i = 0; i <= m - 1; i ++) printf ("%lld ", ((f[i] - 1ll * res[i] * inv % mod) % mod + mod) % mod); putchar ('\n');
}

int main () {
	n = read(), m = read();
	for (register int i = 0; i <= n; i ++) f[i] = fr[n - i] = read();
	for (register int i = 0; i <= m; i ++) g[i] = gr[m - i] = read();
	Division (f, g), Remainder(f, g);
	return 0;
}
posted @ 2020-12-30 12:17  Rubyonlу  阅读(105)  评论(0编辑  收藏  举报