多项式工业基础与全家桶

多项式工业基础与全家桶

开坑待填,放个常数巨大的板子先

别忘了这道题!P3338 [ZJOI2014]力

#define Maxn 200005
#define mod 998244353
inline int ksm(int x,int y=mod-2)
{
	int ret=1;
	for(;y;y>>=1,x=1ll*x*x%mod) if(y&1) ret=1ll*ret*x%mod;
	return ret;
}
const int G=3,invG=ksm(3,mod-2);
int tr[Maxn<<1];
struct Poly
{
	int len,f[Maxn<<1];
	Poly(int L=0) { len=L,memset(f,0,sizeof(f)); }
	inline void ntt(int n,int opt)
	{
		for(int i=0;i<n;i++) tr[i]=(tr[i>>1]>>1)|((i&1)?(n>>1):0);
		for(int i=0;i<n;i++) if(i<tr[i]) swap(f[i],f[tr[i]]);
		for(int p=2;p<=n;p<<=1)
		{
			int tG=ksm((opt==1)?G:invG,(mod-1)/p),L=p>>1;
			for(int i=0;i<n;i+=p)
			{
				int buf=1;
				for(int j=0;j<L;j++)
				{
					int tmp=1ll*buf*f[i+j+L]%mod;
					f[i+j+L]=f[i+j]-tmp;
					if(f[i+j+L]<0) f[i+j+L]+=mod;
					f[i+j]=f[i+j]+tmp;
					if(f[i+j]>=mod) f[i+j]-=mod;
					buf=1ll*buf*tG%mod;
				}
			}
		}
	}
	Poly friend operator + (Poly x,Poly y)
	{
		x.len=max(x.len,y.len);
		for(int i=0;i<=x.len;i++) x.f[i]+=y.f[i];
		for(int i=0;i<=x.len;i++) if(x.f[i]>=mod) x.f[i]-=mod;
		return x;
	}
	Poly friend operator - (Poly x,Poly y)
	{
		x.len=max(x.len,y.len);
		for(int i=0;i<=x.len;i++) x.f[i]-=y.f[i];
		for(int i=0;i<=x.len;i++) if(x.f[i]<0) x.f[i]+=mod;
		return x;
	}
	Poly friend operator * (Poly x,int y)
	{
		for(int i=0;i<=x.len;i++) x.f[i]=1ll*x.f[i]*y%mod;
		return x;
	}
	Poly friend operator * (Poly x,Poly y)
	{
		int L=1;
		for(;L<=(x.len+y.len);L<<=1);
		int invl=ksm(L);
		x.ntt(L,1),y.ntt(L,1);
		for(int i=0;i<L;i++) x.f[i]=1ll*x.f[i]*y.f[i]%mod;
		x.ntt(L,-1);
		for(int i=0;i<L;i++) x.f[i]=1ll*x.f[i]*invl%mod;
		x.len=x.len+y.len;
		return x;
	}
	Poly friend operator % (Poly x,int y)
	{
		for(int i=y+1;i<=x.len;i++) x.f[i]=0;
		x.len=y;
		return x;
	}
	Poly invf()
	{
		Poly ret(0);
		ret.f[0]=ksm(f[0]);
		int L=1; for(;L<len;L<<=1);
		for(int p=2;p<=L;p<<=1) ret=((ret*2)-(*this%(p-1))*ret*ret)%(p-1);
		return ret;
	}
	Poly friend operator / (Poly x,Poly y)
	{
		y=y.invf();
		int L=1;
		for(;L<=(x.len+y.len);L<<=1);
		int invl=ksm(L);
		x.ntt(L,1),y.ntt(L,1);
		for(int i=0;i<L;i++) x.f[i]=1ll*x.f[i]*y.f[i]%mod;
		x.ntt(L,-1);
		for(int i=0;i<L;i++) x.f[i]=1ll*x.f[i]*invl%mod;
		x.len=x.len+y.len;
		return x;
	}
};
posted @ 2022-05-02 11:16  EricQian06  阅读(53)  评论(0编辑  收藏  举报