跳到正文

个人记录

多项式

ysy11约 17 分钟
原文

这是 2026年4月29日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年4月27日
收录
2026年4月29日
更新
2026年11月13日
篇幅
约 17 分钟

版本

  1. v2当前
  2. v1

NTT

C++
ll power(ll x,ll y)
{
	ll ans=1;
	while(y)
	{
		if(y&1)
		{
			ans*=x;
			ans%=MOD;
		}
		x*=x;
		x%=MOD;
		y>>=1;
	}
	return ans;
}
struct poly
{
	vector<ll>a;
};
poly get(ll x)
{
	poly ans;
	for(ll i=0;i<=x;i++)
	{
		ll q;
		cin>>q;
		ans.a.push_back(q);
	}
	while(!ans.a.empty()&&!ans.a.back())ans.a.pop_back();
	return ans;
}
void ask(poly x)
{
	if(x.a.empty())
	{
		return ;
	}
	for(ll i=0;i<x.a.size();i++)
	{
		cout<<x.a[i]<<" ";
	}
}
ll rev[MAXN];
ll wj[MAXN];
void NTT(ll *x,ll y)
{
	for(register int i=0;i<y;i++)
	{
		if(i<rev[i])swap(x[i],x[rev[i]]);
	}
	ll e=y>>1;
	for(register int i=1;i<=e;i<<=1)
	{
		for(register int j=0;j<y;j+=(i<<1))
		{
			for(register int k=0;k<i;k++)
			{
				ll q=x[j+k],w=x[j+i+k]*wj[e/i*k]%MOD;
				x[j+k]=q+w;
				x[j+k]%=MOD;
				x[j+i+k]=q-w+MOD;
				x[j+i+k]%=MOD;
			}
		}
	}
}
ll nta[MAXN],ntb[MAXN];
poly operator *(poly x,poly y)
{
	poly ans;
	if(x.a.empty())
	{
		return x;
	}
	if(y.a.empty())
	{
		return y;
	}
	ll xl=x.a.size()-1,yl=y.a.size()-1;
	for(register int i=0;i<x.a.size();i++)
	{
		nta[i]=x.a[i];
	}
	for(register int i=0;i<y.a.size();i++)
	{
		ntb[i]=y.a[i];
	}
	ll t=1;
	while(t<=xl+yl)t<<=1;
	for(register int i=xl+1;i<=t;i++)
	{
		nta[i]=0;
	}
	for(register int i=yl+1;i<=t;i++)
	{
		ntb[i]=0;
	}
	for(register int i=1;i<t;i++)
	{
		rev[i]=(rev[i>>1]>>1)|((i&1)<<__lg(t)-1);
	}
	ll tttt=t>>1;
	wj[0]=1;
	ll tt=power(3,(MOD-1)/t);
	for(int i=1;i<tttt;i++)
	{
		wj[i]=wj[i-1]*tt%MOD;
	}
	NTT(nta,t);
	NTT(ntb,t);
	tt=power(t,MOD-2);
	for(register int i=0;i<t;i++)nta[i]=nta[i]*ntb[i]%MOD*tt%MOD;
	tt=power(332748118,(MOD-1)/t);
	for(int i=1;i<tttt;i++)
	{
		wj[i]=wj[i-1]*tt%MOD;
	}
	NTT(nta,t);
	for(register int i=0;i<=xl+yl;i++)
	{
		ans.a.push_back(nta[i]);
	}
	return ans;
}
void operator *=(poly &x,poly y){x=x*y;}
void swap(poly &x,poly &y)
{
	poly ans=x;
	x=y;
	y=ans;
}
poly operator +(poly x,poly y)
{
	ll xl=x.a.size();
	ll yl=y.a.size();
	if(xl<yl)swap(x,y),swap(xl,yl);
	for(ll i=0;i<yl;i++)
	{
		x.a[i]+=y.a[i];
	}
	while(!x.a.empty()&&!x.a.back())x.a.pop_back();
	return x;
}
void operator +=(poly &x,poly y){x=x+y;}
poly operator -(poly x,poly y)
{
	ll xl=x.a.size();
	ll yl=y.a.size();
	while(xl<yl)x.a.push_back(0),xl++;
	for(ll i=0;i<xl;i++)
	{
		x.a[i]-=y.a[i];
	}
	while(!x.a.empty()&&!x.a.back())x.a.pop_back();
	return x;
}
void operator -=(poly &x,poly y){x=x-y;}
poly operator *(poly x,ll y)
{
	for(register int i=0;i<x.a.size();i++)
	{
		x.a[i]*=y;
	}
	while(!x.a.empty()&&!x.a.back())x.a.pop_back();
	return x;
}
void operator *=(poly &x,ll y){x=x*y;}
poly operator /(poly x,ll y)
{
	for(register int i=0;i<x.a.size();i++)
	{
		x.a[i]/=y;
	}
	while(!x.a.empty()&&!x.a.back())x.a.pop_back();
	return x;
}
void operator /=(poly &x,ll y){x=x/y;}
poly operator %(poly x,ll y)
{
	for(register int i=0;i<x.a.size();i++)
	{
		x.a[i]%=y;
		x.a[i]+=y;
		x.a[i]%=y;
	}
	while(!x.a.empty()&&!x.a.back())x.a.pop_back();
	return x;
}
void operator %=(poly &x,ll y){x=x%y;}
poly mod(poly x,ll y)
{
	while(x.a.size()>y)x.a.pop_back();
	return x;
}
poly operator <<(poly x,ll y)
{
	ll t=x.a.size()-1;
	for(int i=1;i<=y;i++)
	{
		x.a.push_back(0);
	}
	for(int i=t+y;i>=y;i--)
	{
		x.a[i]=x.a[i-y];
	}
	for(int i=y-1;i>=0;i--)
	{
		x.a[i]=0;
	}
	return x;
}
void operator <<=(poly &x,ll y){x=x<<y;}
poly operator >>(poly x,ll y)
{
	ll t=x.a.size()-1;
	if(t<y)
	{
		poly ans;
		return ans;
	}
	for(int i=0;i<=t-y;i++)
	{
		x.a[i]=x.a[i+y];
	}
	for(int i=1;i<=y;i++)
	{
		x.a.pop_back();
	}
	return x;
}
void operator >>=(poly &x,ll y){x=x>>y;}
poly inv(poly x,ll y)
{
	poly ans;
	if(y==1)
	{
		ans.a.push_back(power(x.a[0],MOD-2));
		return ans;
	}
	ll t=1,tt=y+1>>1;
	while(t<(tt<<1))t<<=1;
	ans=inv(mod(x,t),tt);
	t=1;
	while(t<(y<<1))t<<=1;
	return (ans*2-mod(mod(ans*ans,t)*x,t))%MOD;
}

FFT

C++
ll power(ll x,ll y)
{
	ll ans=1;
	while(y)
	{
		if(y&1)
		{
			ans*=x;
			ans%=MOD;
		}
		x*=x;
		x%=MOD;
		y>>=1;
	}
	return ans;
}
struct poly
{
	vector<ll>a;
};
poly get(ll x)
{
	poly ans;
	for(ll i=0;i<=x;i++)
	{
		ll q;
		cin>>q;
		ans.a.push_back(q);
	}
	while(!ans.a.empty()&&!ans.a.back())ans.a.pop_back();
	return ans;
}
void ask(poly x)
{
	if(x.a.empty())
	{
		return ;
	}
	for(ll i=0;i<x.a.size();i++)
	{
		cout<<x.a[i]<<" ";
	}
}
const double PI=acos(-1);
ll rev[MAXN];
complex<double>wj[MAXN];
ll lowbit(ll x)
{
	return x&-x;
}
void FFT(complex<double>*x,ll y)
{
	for(register int i=0;i<y;i++)
	{
		if(i<rev[i])swap(x[i],x[rev[i]]);
	}
	ll e=y>>1;
	for(register int i=1;i<=e;i<<=1)
	{
		for(register int j=0;j<y;j+=(i<<1))
		{
			for(register int k=0;k<i;k++)
			{
				complex<double>q=x[j+k],w=x[j+i+k]*wj[e/i*k];
				x[j+k]=q+w;
				x[j+i+k]=q-w;
			}
		}
	}
}
complex<double>ffa[MAXN],ffb[MAXN];
poly operator *(poly x,poly y)
{
	poly ans;
	if(x.a.empty())
	{
		return x;
	}
	if(y.a.empty())
	{
		return y;
	}
	ll xl=x.a.size()-1,yl=y.a.size()-1;
	for(register int i=0;i<x.a.size();i++)
	{
		ffa[i]=x.a[i];
	}
	for(register int i=0;i<y.a.size();i++)
	{
		ffb[i]=y.a[i];
	}
	ll t=1;
	while(t<=xl+yl)t<<=1;
	for(register int i=xl+1;i<=t;i++)
	{
		ffa[i]=0;
	}
	for(register int i=yl+1;i<=t;i++)
	{
		ffb[i]=0;
	}
	for(register int i=1;i<t;i++)
	{
		rev[i]=(rev[i>>1]>>1)|((i&1)<<__lg(t)-1);
	}
	ll tttt=t>>1;
	wj[0]={1,0};
	for(int i=1;i<tttt;i<<=1)
	{
		wj[i]={cos(PI/tttt*i),sin(PI/tttt*i)};
	}
	for(int i=1;i<tttt;i++)
	{
		wj[i]=wj[i-lowbit(i)]*wj[lowbit(i)];
	}
	FFT(ffa,t);
	FFT(ffb,t);
	for(register int i=0;i<t;i++)ffa[i]=ffa[i]*ffb[i]/(double)t;
	for(int i=1;i<tttt;i<<=1)
	{
		wj[i]={cos(PI/tttt*i),-sin(PI/tttt*i)};
	}
	for(int i=1;i<tttt;i++)
	{
		wj[i]=wj[i-lowbit(i)]*wj[lowbit(i)];
	}
	FFT(ffa,t);
	for(register int i=0;i<=xl+yl;i++)
	{
		ans.a.push_back(ffa[i].real()+0.5);
	}
	return ans;
}
void operator *=(poly &x,poly y){x=x*y;}
void swap(poly &x,poly &y)
{
	poly ans=x;
	x=y;
	y=ans;
}
poly operator +(poly x,poly y)
{
	ll xl=x.a.size();
	ll yl=y.a.size();
	if(xl<yl)swap(x,y),swap(xl,yl);
	for(ll i=0;i<yl;i++)
	{
		x.a[i]+=y.a[i];
	}
	while(!x.a.empty()&&!x.a.back())x.a.pop_back();
	return x;
}
void operator +=(poly &x,poly y){x=x+y;}
poly operator -(poly x,poly y)
{
	ll xl=x.a.size();
	ll yl=y.a.size();
	while(xl<yl)x.a.push_back(0),xl++;
	for(ll i=0;i<xl;i++)
	{
		x.a[i]-=y.a[i];
	}
	while(!x.a.empty()&&!x.a.back())x.a.pop_back();
	return x;
}
void operator -=(poly &x,poly y){x=x-y;}
poly operator *(poly x,ll y)
{
	for(register int i=0;i<x.a.size();i++)
	{
		x.a[i]*=y;
	}
	while(!x.a.empty()&&!x.a.back())x.a.pop_back();
	return x;
}
void operator *=(poly &x,ll y){x=x*y;}
poly operator /(poly x,ll y)
{
	for(register int i=0;i<x.a.size();i++)
	{
		x.a[i]/=y;
	}
	while(!x.a.empty()&&!x.a.back())x.a.pop_back();
	return x;
}
void operator /=(poly &x,ll y){x=x/y;}
poly operator %(poly x,ll y)
{
	for(register int i=0;i<x.a.size();i++)
	{
		x.a[i]%=y;
		x.a[i]+=y;
		x.a[i]%=y;
	}
	while(!x.a.empty()&&!x.a.back())x.a.pop_back();
	return x;
}
void operator %=(poly &x,ll y){x=x%y;}
poly mod(poly x,ll y)
{
	while(x.a.size()>y)x.a.pop_back();
	return x;
}
poly mtt(poly x,poly y)
{
	poly x0=x/32768,x1=x%32768;
	poly y0=y/32768,y1=y%32768;
	return (x0*y0%MOD*1073741824%MOD+(x0*y1+x1*y0)%MOD*32768%MOD+x1*y1)%MOD;
}
poly operator <<(poly x,ll y)
{
	ll t=x.a.size()-1;
	for(int i=1;i<=y;i++)
	{
		x.a.push_back(0);
	}
	for(int i=t+y;i>=y;i--)
	{
		x.a[i]=x.a[i-y];
	}
	for(int i=y-1;i>=0;i--)
	{
		x.a[i]=0;
	}
	return x;
}
void operator <<=(poly &x,ll y){x=x<<y;}
poly operator >>(poly x,ll y)
{
	ll t=x.a.size()-1;
	if(t<y)
	{
		poly ans;
		return ans;
	}
	for(int i=0;i<=t-y;i++)
	{
		x.a[i]=x.a[i+y];
	}
	for(int i=1;i<=y;i++)
	{
		x.a.pop_back();
	}
	return x;
}
void operator >>=(poly &x,ll y){x=x>>y;}
poly inv(poly x,ll y)
{
	poly ans;
	if(y==1)
	{
		ans.a.push_back(power(x.a[0],MOD-2));
		return ans;
	}
	ll t=1,tt=y+1>>1;
	while(t<(tt<<1))t<<=1;
	ans=inv(mod(x,t),tt);
	t=1;
	while(t<(y<<1))t<<=1;
	return (ans*2-mod(mtt(mod(mtt(ans,ans),t),x),t))%MOD;
}
poly reverse(poly x)
{
	ll xl=x.a.size()-1;
	for(int i=0;(i<<1)<=xl;i++)
	{
		swap(x.a[i],x.a[xl-i]);
	}
	return x;
}
poly operator /(poly x,poly y)
{
	ll xl=x.a.size()-1;
	ll yl=y.a.size()-1;
	x=reverse(x);
	y=reverse(y);
	return reverse(mod(mtt(x,mod(inv(y,xl-yl+1),xl-yl+1)),xl-yl+1));
}
poly operator %(poly x,poly y)
{
	return (x-mtt(x/y,y))%MOD;
}