多项式工业基础与全家桶

多项式工业基础与全家桶

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

别忘了这道题!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;
	}
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值