跳到正文

休闲·娱乐

多项式全家桶

KingPowers约 10 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月28日
篇幅
约 10 分钟
获赞
1

版本

  1. v1当前

注意文章分类。

你只需要会对多项式求导!

对于多项式 A(x)A(x),记 AiA_i 表示 [xi]A(x)[x^i]A(x)。

多项式乘法

呃这个肯定就不用讲了。

多项式求逆

假设现在有多项式 A(x)A(x),设 BB 是 AA 的逆,那么有 AB=1AB=1,所以:

∑i=0nAiBn−i=[n=0]\sum_{i=0}^n A_{i}B_{n-i}=[n=0]

当 n≥1n\ge 1 时,提取出来左边 i=0i=0 的项,也就是 A0BnA_0B_n:

A0Bn=−∑i=1nAiBn−iBn=−1A0∑i=1nAiBn−i\begin{aligned} A_0B_n&=-\sum_{i=1}^nA_iB_{n-i}\\ B_n&=-\frac{1}{A_0}\sum_{i=1}^nA_iB_{n-i} \end{aligned}

对于 n=0n=0 的情况,显然 B0=1A0B_0=\frac{1}{A_0},以此为边界递推即可求出 BB。

多项式 ln

假设现在有多项式 A(x)A(x),设 B(x)=ln⁡A(x)B(x)=\ln A(x)。

先对两边求导得到 B′(x)=A′(x)A(x)B'(x)=\frac{A'(x)}{A(x)},然后对 A(x)A(x) 做多项式求逆再积分即可求出 B(x)B(x),也就是 A′(x)=B′(x)A(x)A'(x)=B'(x)A(x),系数对应一下:

(n+1)An+1=∑i=0n(i+1)Bi+1An−i\begin{aligned} (n+1)A_{n+1}=\sum_{i=0}^n(i+1)B_{i+1}A_{n-i} \end{aligned}

拉出来右边 i=ni=n 时的项:

(n+1)Bn+1A0=(n+1)An+1−∑i=0n−1(i+1)Bi+1An−iBn+1=(n+1)An+1−∑i=0n−1(i+1)Bi+1An−i(n+1)A0\begin{aligned} (n+1)B_{n+1}A_{0}&=(n+1)A_{n+1}-\sum_{i=0}^{n-1}(i+1)B_{i+1}A_{n-i}\\ B_{n+1}&=\frac{(n+1)A_{n+1}-\sum_{i=0}^{n-1}(i+1)B_{i+1}A_{n-i}}{(n+1)A_0} \end{aligned}

直接递推即可。

多项式 exp

假设现在有多项式 A(x)A(x),设 B(x)=exp⁡A(x)B(x)=\exp A(x),那么就有 A(x)=ln⁡B(x)A(x)=\ln B(x),再两边求导得到 A′(x)=B′(x)B(x)A'(x)=\frac{B'(x)}{B(x)},也就是 B′(x)=B(x)A′(x)B'(x)=B(x)A'(x),同样对应下系数:

(n+1)Bn+1=∑i=0n(i+1)Ai+1Bn−iBn+1=1n+1∑i=0n(i+1)Ai+1Bn−i\begin{aligned} (n+1)B_{n+1}&=\sum_{i=0}^n(i+1)A_{i+1}B_{n-i}\\ B_{n+1}&=\frac{1}{n+1}\sum_{i=0}^n(i+1)A_{i+1}B_{n-i} \end{aligned}

同样直接递推即可。

代码

对上面的操作进行了整理,可以通过洛谷模板所有该通过的数据(迫真)。

C++
#include<bits/stdc++.h>
#define For(i, a, b) for(int i = (a); i <= (b); i++)
#define Rof(i, a, b) for(int i = (a); i >= (b); i--)
#define int long long
#define SZ(x) ((int)x.size())
using namespace std;
using poly = vector<int>;
const int mod = 998244353;
inline void add(int &x, int y){x += y; if(x >= mod) x -= mod;}
inline void sub(int &x, int y){x += mod - y; if(x >= mod) x -= mod;}
int qpow(int x, int y){
	int res = 1;
	while(y){
		if(y & 1) res = res * x % mod;
		x = x * x % mod; y /= 2;
	}
	return res;
}
poly mul(const poly &a, const poly &b){
	int n = SZ(a) - 1, m = SZ(b) - 1;
	poly res(n + m + 1);
	For(i, 0, n + m) For(j, 0, min(i, n))
		if(i - j <= m) add(res[i], a[j] * b[i - j] % mod);
	return res;
}
poly inv(const poly &a){
	int n = SZ(a) - 1;
	poly res(n + 1); int inv = qpow(a[0], mod - 2);
	res[0] = inv;
	For(i, 1, n) For(j, 1, i) sub(res[i], inv * a[j] % mod * res[i - j] % mod);
	return res;
}
poly ln(const poly &a){
	int n = SZ(a) - 1;
	poly res(n + 1); 
	For(i, 0, n - 1){
		int inv = qpow((i + 1) * a[0] % mod, mod - 2);
		int sum = (i + 1) * a[i + 1] % mod;
		For(j, 0, i - 1) sub(sum, (j + 1) * res[j + 1] % mod * a[i - j] % mod);
		res[i + 1] = sum * inv % mod;
	}
	return res;
}
poly exp(const poly &a){
	int n = SZ(a) - 1;
	poly res(n + 1);
	res[0] = 1;
	For(i, 0, n - 1){
		int inv = qpow(i + 1, mod - 2), sum = 0;
		For(j, 0, i) add(sum, (j + 1) * a[j + 1] % mod * res[i - j] % mod);
		res[i + 1] = sum * inv % mod;
	}
	return res;
}

咳咳,这里有份正经的,依据洛谷模板题的表现常数好像还挺小的。

C++
const int mod = 998244353, G = 3, Gi = 332748118;
using poly = vector<int>;
int tot, bit, rev[4000050];
inline void init(int n){
	tot = 1, bit = 0;
	while(tot <= n) tot <<= 1, bit++;
	For(i, 0, tot - 1) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << bit - 1);
}
void ntt(poly &a, int n, int op){
	a.resize(n);
	For(i, 0, n - 1) if(i < rev[i]) swap(a[i], a[rev[i]]);
	int base = op == 1 ? G : Gi;
	for(int mid = 1; mid < n; mid <<= 1){
		int w1 = qpow(base, (mod - 1) / (mid << 1));
		for(int i = 0; i < n; i += mid << 1)
			for(int j = 0, wk = 1; j < mid; j++, wk = wk * w1 % mod){
				int u = a[i + j], v = wk * a[i + j + mid] % mod;
				a[i + j] = (u + v) % mod; a[i + j + mid] = (u + mod - v) % mod;
			}
	}
	if(op == -1){
		int inv = qpow(n, mod - 2);
		For(i, 0, n - 1) a[i] = a[i] * inv % mod;
	}
}
poly mul(poly a, poly b){
	int n = SZ(a) - 1, m = SZ(b) - 1;
	init(n + m); ntt(a, tot, 1); ntt(b, tot, 1);
	For(i, 0, tot - 1) a[i] = a[i] * b[i] % mod;
	ntt(a, tot, -1); a.resize(n + m + 1);
	return a;
}
poly diff(const poly &a){
	int n = SZ(a) - 1; poly b(n);
	For(i, 1, n) b[i - 1] = a[i] * i % mod;
	return b;
}
poly integ(const poly &a){
	int n = SZ(a); poly b(n + 1);
	For(i, 1, n) b[i] = a[i - 1] * qpow(i, mod - 2) % mod;
	return b;
}
void getinv(int deg, const poly &a, poly &b){
	if(deg == 1) return b[0] = qpow(a[0], mod - 2), void();
	getinv((deg + 1) >> 1, a, b); init(deg * 2 + 1);
	poly c(tot); For(i, 0, deg - 1) c[i] = a[i];
	ntt(b, tot, 1); ntt(c, tot, 1);
	For(i, 0, tot - 1) b[i] = (2 - b[i] * c[i] % mod + mod) % mod * b[i] % mod;
	ntt(b, tot, -1); b.resize(deg);
}
poly inv(const poly &a){
	int n = SZ(a) - 1;
	poly b(n); getinv(n, a, b);
	return b;
}
poly ln(const poly &a){
	int n = SZ(a) - 1;
	poly res = integ(mul(diff(a), inv(a)));
	return res.resize(n + 1), res;
}
void getexp(int l, int r, const poly &a, poly &b){
	if(l == r){
		b[l] = !l ? 1 : b[l] * qpow(l, mod - 2) % mod;
		return;
	}
	int mid = (l + r) >> 1;
	getexp(l, mid, a, b);
	poly A(r - l + 1), B(mid - l + 1);
	For(i, 0, r - l) A[i] = a[i];
	For(i, l, mid) B[i - l] = b[i];
	poly res = mul(A, B);
	For(i, mid + 1, r) add(b[i], res[i - l - 1]);
	getexp(mid + 1, r, a, b);
}
poly exp(const poly &a){
	int n = SZ(a) - 1;
	poly b(n + 1); getexp(0, n, diff(a), b);
	return b;
}
原文