跳到正文

个人记录

多项式模板(持续更新,自用)

Purumi约 21 分钟
原文
文章信息与版本

文章信息

发布
2025年9月10日
收录
2026年7月13日
更新
2026年11月15日
篇幅
约 21 分钟

版本

  1. v1当前
C++
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"

#include <vector>
#include <algorithm>
#include <cstdio>
#include <random>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
#define ll long long

static int NTTdeg(int d) {
	if (d <= 1) return 1;
	int n = 1;
	while (n < d)
		n <<= 1;
	return n;
}

class poly {
public:
	static const int mod = 998244353;
	static const int g = 3;
	static const int I = 86583718;
	
	static inline int qpow(int base, int exp) {
		long long res = 1;
		while (exp) {
			if (exp & 1) res = res * base % mod;
			base = (long long)base * base % mod;
			exp >>= 1;
		}
		return res;
	}
private:
	inline void bit_reverse(vector<int>& a) {
		int n = a.size();
		for(int i = 0, j = 0; i < n; i++) {
			if (i < j) swap(a[i], a[j]);
			for(int k = n >> 1; (j ^= k) < k; k >>= 1);
		}
	}
	poly _inv(int n) {
		// 牛顿迭代公式:B(x) = B0(x) · (2 - A(x) · B0(x)) mod (x^n)
		if (n == 1)	{
			auto c = f[0];
			return poly(vector<int>({qpow(c, mod - 2)}));
		}
		int m = (n+1)/2;
		poly B0 = trunc(m)._inv(m);
		poly A = trunc(n);
		poly AB0 = A * B0;
		AB0 = AB0.trunc(n);
		vector<int> two_vec(1,2);
		poly two_poly(two_vec);
		poly temp = two_poly - AB0;
		poly B = B0 * temp;
		B = B.trunc(n);
		return B;
	}
	
	poly reverse() const {
		vector<int> rev_f(f.rbegin(), f.rend());
		return poly(rev_f);
	}
	
	static int cipolla_sqrt(int a) {
		if (a == 1)	return 1;
		int p = mod;
		
		mt19937 rng(time(0));
		uniform_int_distribution<int> dis(1, p - 1);
		int n;
		do {
			n = dis(rng);
		}while (qpow((1LL * n * n - a + p) % p, (p-1)/2) == 1);
		
		int w2 = (1LL*n*n - a) % p;
		if (w2 < 0)	w2 += p;
		int re = 1, im = 0;
		int x = n, y = 1;
		int exp = (p + 1) / 2;
		while (exp) {
			if (exp & 1) {
				int r = (1LL*re*x + 1LL*im*y%p*w2) % p;
				int i = (1LL*re*y + 1LL*im*x) % p;
				re = r;
				im = i;
			}
			int nx = (1LL*x*x + 1LL*y*y%p*w2) % p, ny = (2LL*x*y) % p;
			x = nx, y = ny;
			exp >>= 1;
		}
		if (re > p / 2)	re = p - re;
		return re;
	}
	
	
public:
	vector<int> f;
	int deg;
	
	poly(const vector<int>& coeffs = {}) : f(coeffs), deg(coeffs.size() - 1) {}
	
	poly operator+(const poly& other) const {
		int n = max(f.size(), other.f.size());
		vector<int> res(n, 0);
		for (int i = 0; i < f.size(); i++) res[i] = f[i];
		for (int i = 0; i < other.f.size(); i++) {
			res[i] = (res[i] + other.f[i]) % mod;
		}
		return poly(res);
	}
	
	poly operator-(const poly& other) const {
		int n = max(f.size(), other.f.size());
		vector<int> res(n, 0);
		for (int i = 0; i < f.size(); i++) res[i] = f[i];
		for (int i = 0; i < other.f.size(); i++) {
			res[i] = (res[i] - other.f[i] + mod) % mod;
		}
		return poly(res);
	}
	
	poly operator*(int c) const {
		c = (c % mod + mod) % mod;
		vector<int> res = f;
		for (int i = 0; i < res.size(); i++) {
			res[i] = (1LL * res[i] * c) % mod;
		}
		return poly(res);
	}
	
	friend poly operator*(int c, const poly& p) {
		return p * c;
	}
	
	poly operator/(int c) const {
		c = (c % mod + mod) % mod;
		// of course c != 0. do not need to consider this case
		return *this * qpow(c, mod - 2);
	}
	
	poly operator*(const poly& other) const {
		if (f.empty() || other.f.empty()) 
			return poly(vector<int>());
		
		int n1 = f.size(), n2 = other.f.size();
		int len = NTTdeg(n1 + n2 - 1);
		
		poly a = *this;
		poly b = other;
		
		a.f.resize(len, 0);
		b.f.resize(len, 0);
		
		a.NTT(1);
		b.NTT(1);
		
		poly res;
		res.f.resize(len);
		for(int i = 0; i < len; i++)
			res.f[i] = (long long)a.f[i] * b.f[i] % mod;
		
		res.NTT(-1);
		res.f.resize(n1 + n2 - 1);
		res.deg = res.f.size();
		return res;
	}
	
	inline void NTT(int type) {
		int n = f.size();
		bit_reverse(f);
		for(int len = 2; len <= n; len <<= 1) {
			int wn = qpow(g, (mod - 1) / len);
			if (type == -1) wn = qpow(wn, mod - 2);
			
			for(int i = 0; i < n; i += len) {
				int w = 1;
				for(int j = 0; j < (len >> 1); j++) {
					long long u = f[i + j];
					long long v = (long long)w * f[i + j + len / 2] % mod;
					f[i + j] = (u + v) % mod;
					f[i + j + len / 2] = (u - v + mod) % mod;
					w = (long long)w * wn % mod;
				}
			}
		}
		
		if (type == -1) {
			int inv_n = qpow(n, mod - 2);
			for(int i = 0; i < n; i++)
				f[i] = (long long)f[i] * inv_n % mod;
		}
	}
	
	poly deri() {
		if (f.empty()) return poly();
		poly res = *this;
		for(int i = 0; i < deg; i++)
			res.f[i] = (1LL * res.f[i + 1] * (i + 1)) % mod;
		res.f[deg] = 0;
		res.deg--;
		res.f.resize(res.deg+1);
		return res;
	}
	poly integral() {
		vector<int> res(f.size() + 1, 0); // 积分后次数+1,常数项为0
		for (int i = 1; i <= f.size(); i++) {
			res[i] = (long long)f[i-1] * qpow(i, mod-2) % mod;
		}
		return poly(res);
	}
	
	poly trunc(int n) const {
		if (n <= 0) return poly(vector<int>());
		if (n > f.size()) {
			vector<int> res = f;
			res.resize(n, 0);
			return poly(res);
		}
		vector<int> res(f.begin(), f.begin() + n);
		poly p(res);
		p.deg = p.f.size() - 1;
		return p;
	}
	poly inv() {
		return _inv(f.size());
	}
	pair<poly, poly> div(const poly& g) const {
		poly f_clean = this->clean();
		poly g_clean = g.clean();
		
		int n = f_clean.f.size() - 1;
		int m = g_clean.f.size() - 1;
		
		if (n < m)
			return make_pair(poly(), *this);
		
		poly f_rev = f_clean.reverse();
		poly g_rev = g_clean.reverse();
		poly g_rev_inv = g_rev.inv().trunc(n - m + 1);
		
		poly Q_rev = (f_rev * g_rev_inv).trunc(n - m + 1);
		poly Q = Q_rev.reverse();
		
		// 计算余式:R = f - Q*g
		poly R = (f_clean - Q * g_clean).clean();
		
		return make_pair(Q, R);
	}
	
private:
	poly clean() const {
		if (f.empty()) return poly();
		int last_nonzero = f.size() - 1;
		while (last_nonzero >= 0 && f[last_nonzero] == 0) {
			last_nonzero--;
		}
		
		if (last_nonzero < 0) return poly();
		return poly(vector<int>(f.begin(), f.begin() + last_nonzero + 1));
	}
	poly build_poly(const vector<int>& x, int l, int r) {
		if (l == r) return poly({(mod - x[l]) % mod, 1});
		int mid = (l + r) >> 1;
		return build_poly(x, l, mid) * build_poly(x, mid + 1, r);
	}
	
	vector<int> _eval(const vector<int>& x, int l, int r, int L, int R) {
		int u = 1000;
		if (u < r - l + 1) {
			int mid = (l + r) >> 1;
			poly g0 = build_poly(x, l, mid);
			poly g1 = build_poly(x, mid + 1, r);
			
			// 关键修正:使用余数多项式递归
			poly f0 = div(g0).second;
			poly f1 = div(g1).second;
			
			vector<int> res0 = f0._eval(x, l, mid, L, R);
			vector<int> res1 = f1._eval(x, mid + 1, r, L, R);
			
			res0.insert(res0.end(), res1.begin(), res1.end());
			return res0;
		}
		else {
			vector<int> res0(r - l + 1);
			for(int i = l; i <= r; i++)
				res0[i - l] = eval(x[i]);
			return res0;
		}
		
	}
public:
	// 对数函数
	poly ln() {
		// For debug
		if(f[0] != 1) {
			fprintf(stderr, "Error: Constant term must be 1 for polynomial logarithm\n");
			return poly();
		}
		
		poly der = deri();
		poly inv_f = inv();
		poly product = der * inv_f;
		product = product.trunc(f.size()-1);
		return product.integral();
	}
	// 指数函数
	poly exp() {
		int n = f.size();
		if(n == 0) return poly().trunc(n);
		
		// For debug
		if(f[0] != 0) {
			fprintf(stderr, "Error: Constant term must be 0 for polynomial exponential\n");
			return poly();
		}
		poly B(vector<int>({1}));
		int len = 1;
		poly A = *this;
		
		while(len < n) {
			len *= 2;
			int trunc_len = min(len, n);
			poly B_ext = B;
			B_ext.f.resize(trunc_len, 0);
			poly lnB = B_ext.ln();
			poly temp = A.trunc(trunc_len) - lnB;
			temp.f[0] = (temp.f[0] + 1) % mod;
			B = B * temp.trunc(trunc_len);
			B = B.trunc(trunc_len);
		}
		return B.trunc(n);
	}
	
	// 正弦 & 余弦
	// sin x = (exp(ix) - exp(-ix)) / 2i
	// cos x = (exp(ix) + exp(-ix)) / 2
	poly sin() {
		poly ix = *this * I;
		poly neg_ix = *this * (mod - I);
		return (ix.exp() - neg_ix.exp()) * qpow(2 * I, mod - 2);
	}
	poly cos() {
		poly ix = *this * I;
		poly neg_ix = *this * (mod - I);
		return (ix.exp() + neg_ix.exp()) * qpow(2, mod - 2);
	}
	poly tan() {
		return sin() * cos().inv();
	}
	// 平方根
	// 牛顿迭代: B -> 1/2 *(B + A / B)
	poly sqrt() {
		int n = f.size();
		// 常数项为 0?不管了!
		int b0 = cipolla_sqrt(f[0]);
		poly B(vector<int>({b0}));
		int len = 1;
		while (len < n) {
			len <<= 1;
			int trunc_len = min(len, n);
			B.f.resize(trunc_len, 0);
			B = (B + (trunc(trunc_len) * B.inv())) * qpow(2, mod - 2);
			B = B.trunc(trunc_len);
		}
		return B.trunc(n);
	}
	
	poly asin() {
		int n = f.size();
		poly x = *this;
		poly x2 = x * x;
		x2.f.resize(n);
		x2.f[0] = (1 - x2.f[0] + mod) % mod;
		for (int i = 1; i < n; i++) x2.f[i] = (mod - x2.f[i]) % mod;
		
		poly sqrt_x2 = x2.sqrt();
		sqrt_x2.f.resize(n);
		poly ix = x * I;
		ix.f.resize(n);
		poly inner = ix + sqrt_x2;
		inner.f.resize(n);
		poly ln_inner = inner.ln();
		ln_inner.f.resize(n);
		poly res = ln_inner * (-I);
		res.f.resize(n);
		return res;
	}
	// acos 略. 使用积分推导.
	poly atan() {
		poly ix = *this * I;
		poly one_minus_ix = poly({1}) - ix;
		poly one_plus_ix = poly({1}) + ix;
		poly diff = one_minus_ix.ln() - one_plus_ix.ln();
		return diff * I * qpow(2, mod - 2);
	}
	
	// 多项式单点求值:霍纳法则
	int eval(int x) {
		long long res = 0;
		for(int i = deg; i >= 0; i--)
			res = (res * x + f[i]) % mod;
		return res;
	}
	
	// 多项式多点求值:CRT
	vector<int> eval(const vector<int>& x) {
		return _eval(x, 0, x.size() - 1, 0, x.size() - 1);
	}
	
	poly polypow(int n) {
		if (n == 0)	return poly(vector<int>{1});
		else if (n == 1)	return *this;
		poly res = polypow(n / 2);
		if (n & 1)	return (res * res) * (*this);
		else	return res * res;
	}
	
	poly trunc_polypow(int n, int trunc_num) {
		if (n == 0) return poly(vector<int>{1}).trunc(trunc_num);
		
		poly base = *this;
		poly result(vector<int>{1});
		
		while (n > 0) {
			if (n & 1) {
				result = (result * base).trunc(trunc_num);
			}
			base = (base * base).trunc(trunc_num);
			n /= 2;
		}
		return result.trunc(trunc_num);
	}
	
	void print() {
		for(auto i: f)	printf("%d ", i);
		printf("\n");
	}
	
};

#pragma GCC diagnostic pop
#endif

使用例

P1919 高精度乘法

P3803 多项式乘法

P4238 多项式求逆

P4831 Scarlet loves WenHuaKe

P4512 多项式除法/取模

P4721 分治 FFT

P4725 多项式对数

P4726 多项式指数

P5050 多项式多点求值

P5264 多项式三角函数

P5265 多项式反三角函数

P5205 & P5277 多项式开根

原文