跳到正文

算法·理论

【我的模板】多项式全家桶

Timmylyx约 16 分钟
原文
文章信息与版本

文章信息

发布
2026年4月25日
收录
2026年7月21日
更新
2026年11月8日
篇幅
约 16 分钟

版本

  1. v1当前

(未完待续)

本代码实现了多项式加、减、乘、除、整除、模六种基本运算。

实现了求导、积分、指数、对数、开根、次幂、三角函数和常系数齐次线性递推,附赠 cipolla 算法。

本代码还实现了 与、或、异或和子集卷积。

主要算法:快速幂、NTT、FWT、FMT、cipolla、牛顿迭代、与 ee 有关的公式。

代码
C++
//Timmy's poly 
//writen by @Timmylyx
//update to 7-3
//last update:2026/4/24
//too interesting
//I love poly!
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define poly vector<int> 
const int M=998244353,ima=86583718,G1=3,G2=(M+1)/3,inv2=(M+1)/2;
vector <int> inv;
//it is time to prepare
inline int Pow(int a,int n){
	//faster pow
	int ans=1;
	while (n){
		if (n&1) ans=ans*a%M;
		a=a*a%M; n>>=1;
	}
	return ans;
}
void init(int len){
	inv.resize(len+1);
	for (int i=0; i<=len; i++){
		inv[i]=Pow(i,M-2);
	}
}
//these are ins ans outs
void readin(poly &p,int n){
	while (n--){
		int x; cin>>x;
		p.push_back(x);
	}
}
void writeout(poly p){
	for (auto x:p) cout<<x<<" ";
}
//start from easy-FFT
void FFT(poly &x,int type){
	int n=x.size();
	poly r; r.resize(n);
	//resize done
	for (int i=0; i<n; i++){
		r[i]=((r[i>>1]>>1)+((i&1)*(n>>1)));
	}
	for (int i=0; i<n; i++){
		if (i<r[i]) swap(x[i],x[r[i]]);
	}
	//exchange done
	for (int now=1; now<n; now<<=1){
		int wn=Pow((type==1?G1:G2),(M-1)/(now<<1));
		for (int tmp=(now<<1),j=0; j<n; j+=tmp){
			for (int k=j,w=1; k<j+now; k++,w=w*wn%M){
				int X=x[k],Y=w*x[k+now]%M;
				x[k]=(X+Y)%M;
				x[k+now]=(X-Y+M)%M;
			}
		}
	}
}
//four operators
poly operator +(poly a,poly b){
	int len=max(a.size(),b.size());
	a.resize(len); b.resize(len);
	for (int i=0; i<len; i++) a[i]=(a[i]+b[i])%M;
	return a;
}
poly operator -(poly a,poly b){
	int len=max(a.size(),b.size());
	a.resize(len); b.resize(len);
	for (int i=0; i<len; i++) a[i]=(a[i]-b[i]+M)%M;
	return a;
}
poly operator *(poly a,poly b){
	int n=a.size(),m=b.size();
	int len=1; while (len<n+m) len*=2;
	a.resize(len); b.resize(len);
	FFT(a,1); FFT(b,1); 
	a.resize(max(a.size(),b.size()));
	b.resize(max(a.size(),b.size()));
	for (int i=0; i<a.size(); i++) a[i]=a[i]*b[i]%M;
	FFT(a,-1);  
	for (int i=0; i<a.size(); i++) a[i]=a[i]*inv[a.size()]%M;
	a.resize(n+m-1);
	return a;
}
poly get_inv(poly x){
	int m=1; poly ans; 
	ans.push_back(Pow(x[0],M-2));
	while (m<x.size()){
		m*=2;
		auto tmp=x; 
		tmp.resize(m);
		ans=ans*((poly){2}-tmp*ans);
		ans.resize(m);
	}
	ans.resize(x.size());
	return ans;
}
poly operator /(poly a,poly b){
	return a*get_inv(b);
}
//some calculus
poly get_der(poly p){
	int n=p.size();
	poly ans;
	for (int i=1; i<n; i++) ans.push_back(i*p[i]%M);
	return ans;
}
poly get_int(poly p){
	int n=p.size();
	poly ans={0};
	for (int i=0; i<n; i++){
		ans.push_back(p[i]*inv[i+1]%M);
	}
	return ans;
}
poly get_ln(poly p){
	poly ans=get_int(get_der(p)/p);
	ans.resize(p.size());
	return ans;
}
poly get_exp(poly p){
	poly ans={1};
	int m=1;
	while (m<p.size()){
		m*=2;
		ans.resize(m); //remember to resize
		poly tmp=p; tmp.resize(m);
		ans=ans*((poly){1}-get_ln(ans)+tmp);
		ans.resize(m);
	} 
	ans.resize(p.size());
	return ans;
} 
poly Pow(poly a,int n,int N,int N2){
	if (a[0]==1){
		return get_exp(get_ln(a)*((poly){n}));
	}
	if (a[0]==0){
		//delete some zeros
		int id=-1;
		for (int i=1; i<a.size(); i++){
			if (a[i]!=0){
				id=i; break;
			}
		}
		if (id==-1){
			poly ans;
			ans.resize(a.size());
			return ans;
		}
		else{
			poly b;
			for (int i=id; i<a.size(); i++) b.push_back(a[i]);
			poly val=Pow(b,n,N,N2);
			int tmp=min((int)a.size(),id*N2);
			poly ans;
			ans.resize(tmp);
			for (int i=0; i<a.size()-tmp; i++) ans.push_back(val[i]);
			return ans;
		}
	}
	int bas=a[0],bas2=Pow(bas,N);
	for (int i=0; i<a.size(); i++) a[i]=a[i]*Pow(bas,M-2)%M;
	poly ans=Pow(a,n,N,N2);
	for (int i=0; i<a.size(); i++) ans[i]=ans[i]*bas2%M;
	return ans;
}
//quadratic residue
int a,omega;
int brute_force(int n){
	for (int i=0; i<M; i++){
		if (i*i%M==n) return i;
	}
}
//complex for cipolla 
struct cipolla{
	int rel,ima;
};
cipolla operator+(cipolla a,cipolla b){
	return cipolla{(a.rel+b.rel)%M,(a.ima+b.ima)%M};
}
cipolla operator-(cipolla a,cipolla b){
	return cipolla{(a.rel-b.rel+M)%M,(a.ima-b.ima+M)%M};
}
cipolla operator*(cipolla a,cipolla b){
	return cipolla{(a.rel*b.rel+a.ima*b.ima%M*omega)%M,(a.ima*b.rel+b.ima*a.rel)%M};
}
cipolla Pow(cipolla a,int n){
	cipolla ans=cipolla{1,0};
	while (n){
		if (n&1) ans=ans*a;
		a=a*a; n>>=1;
	}
	return ans;
}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
void cipolla_init(int n){
	while (true){
		a=rnd()%M;
		if (Pow((a*a-n+M)%M,(M-1)/2)==M-1){
			omega=(a*a-n+M)%M;
			return ;
		}
	}
}
int cipolla_work(int n){
	cipolla_init(n);
	cipolla tmp=Pow(cipolla{a,1},(M+1)/2);
	return min(tmp.rel,M-tmp.rel);
}
poly sqrt(poly p){
	poly ans={cipolla_work(p[0])};
	int m=1;
	while (m<p.size()){
		m*=2;
		auto tmp=p; tmp.resize(m);
		ans=ans-(ans*ans-tmp)/((poly){2}*ans);
		ans.resize(m);
	}
	ans.resize(p.size());
	return ans;
}
//the last operator
poly zhengchu(poly a,poly b){
	int n=a.size(),m=b.size();
	if (n<m) a.resize(m),n=m;
	int len=n-m+1;
	reverse(a.begin(),a.end()); a.resize(len);
	reverse(b.begin(),b.end()); b.resize(len);
	poly ans=a/b; ans.resize(len); reverse(ans.begin(),ans.end());
	return ans; 
}
poly operator%(poly a,poly b){
	poly ans=a-zhengchu(a,b)*b;
	ans.resize(b.size()-1);
	return ans;
}
//trigonometric function
poly get_sin(poly p){
	poly I=(poly){ima};
	poly ans=get_exp(I*p)-get_exp((poly){0}-I*p);
	ans=ans/((poly){2}*I);
	return ans;
}
poly get_cos(poly p){
	poly I=(poly){ima};
	poly ans=get_exp(I*p)+get_exp((poly){0}-I*p);
	ans=ans/((poly){2});
	return ans;
}
//linear recursion 
poly linear_recursion_pow(poly f,int n){
	poly ans=(poly){0,1},a=(poly){0,1};
	while (n){
		if (n&1) ans=ans*a%f;
		a=a*a%f; n>>=1;
	}
	return ans;
}
int linear_recursion_easy(poly f,poly a,int n){
	reverse(f.begin(),f.end());
	for (auto&x:f) x=-x;
	f.push_back(1);
	poly p=linear_recursion_pow(f,n-1);
	int ans=0;
	for (int i=0; i<a.size(); i++){
		ans=(ans+p[i]*a[i])%M;
	}
	return (ans%M+M)%M;
}
//FMT
void fill(poly &p){
	int len=1;
	while (len<p.size()) len<<=1;
	p.resize(len); 
}
void FMT_or(poly &p,int op){
	for (int w=1,len=p.size(); w<len; w<<=1){
		for (int i=1; i<len; i++){
			if (i&w) p[i]=(p[i]+op*p[i^w]+M)%M;
		}
	}
} 
void FMT_and(poly &p,int op){
	for (int w=1,len=p.size(); w<len; w<<=1){
		for (int i=0; i<len; i++){
			if ((i&w)==0) p[i]=(p[i]+op*p[i^w]+M)%M;
		}
	}
} 
poly operator|(poly a,poly b){
	FMT_or(a,1); FMT_or(b,1);
	for (int i=0; i<a.size(); i++) a[i]=a[i]*b[i]%M;
	FMT_or(a,-1); return a; 
}
poly operator&(poly a,poly b){
	FMT_and(a,1); FMT_and(b,1);
	for (int i=0; i<a.size(); i++) a[i]=a[i]*b[i]%M;
	FMT_and(a,-1); return a; 
}
//FWT
void FWT_xor(poly &a,int op){
	for (int w=1,len=a.size(); w<len; w<<=1){
		for (int l=0,r=w-1; r<len; l+=(w<<1),r+=(w<<1)){
			for (int k=l; k<=r; k++){
				a[k]=(a[k]+a[k+w])%M;
				a[k+w]=(a[k]+2*M-2*a[k+w])%M;
				a[k]=a[k]*(op==-1?inv2:1)%M;
				a[k+w]=a[k+w]*(op==-1?inv2:1)%M; 
			}
		}
	}
}
poly operator^(poly a,poly b){
	FWT_xor(a,1); FWT_xor(b,1);
	for (int i=0; i<a.size(); i++) a[i]=a[i]*b[i]%M;
	FWT_xor(a,-1); return a; 
}
//subset convolution
int pop_count(int x){
	int ans=0;
	while (x){
		ans++; x&=x-1;
	}
	return ans;
}
poly subset(poly a,poly b){
	int len=a.size(),p=log2(len);
	poly x[30],y[30],z[30],ans; ans.resize(len);
	for (int i=0; i<=p; i++) x[i].resize(len),y[i].resize(len),z[i].resize(len);
	for (int i=0; i<=p; i++){
		for (int j=0; j<len; j++){
			if (pop_count(j)==i) x[i][j]=a[j],y[i][j]=b[j];
			else x[i][j]=0,y[i][j]=0;
		}
		FMT_or(x[i],1); FMT_or(y[i],1);
	}
	for (int i=0; i<=p; i++){
		for (int j=0; j<=i; j++){
			for (int k=0; k<len; k++) z[i][k]=(z[i][k]+x[j][k]*y[i-j][k])%M;
		}
		FMT_or(z[i],-1);
		for (int k=0; k<len; k++) if (pop_count(k)==i) ans[k]=z[i][k];
	}
	return ans;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	init(1e5);
	int n; cin>>n; n=(1<<n);
	poly p,q; readin(p,n); readin(q,n);
	writeout(subset(p,q));
	
}
原文