zoukankan      html  css  js  c++  java
  • 【洛谷P5394】【模板】—下降幂多项式乘法(指数级生成函数)

    传送门

    首先考虑对于xnx^{underline n}的点值构建EGFEGF

    =i=nini!xi=i=n1(in)!xi=xnex=sum_{i=n}^{infty}frac{i^{underline n}}{i!}x^i=sum_{i=n}^{infty}frac{1}{(i-n)!}x^i=x^ne^x

    考虑对于f(x)=i=0aixif(x)=sum_{i=0}^{infty}a_ix^{underline i}的点值构造EGFEGF

    g(x)=i=0f(i)i!xi=i=0xii!j=0ajijg(x)=sum_{i=0}^{infty}frac{f(i)}{i!}x^i=sum_{i=0}^{infty}frac{x^i}{i!}sum_{j=0}^{infty}a_ji^{underline j}

    =j=0aji=0iji!xi=j=0ajxjex=sum_{j=0}^{infty}a_jsum_{i=0}^{infty}frac{i^{underline j}}{i!}x^i=sum_{j=0}^{infty}a_jx^je^x

    =exi=0aixi=e^xsum_{i=0}^{infty}a_ix^i

    所以只需要用普通多项式的系数乘个exe^x就得到了点值的EGFEGF
    点值还原原多项式只需要乘一个exe^{-x}即可

    #include<bits/stdc++.h>
    using namespace std;
    const int RLEN=1<<20|1;
    inline char gc(){
    	static char ibuf[RLEN],*ib,*ob;
    	(ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    	return (ob==ib)?EOF:*ib++;
    }
    #define gc getchar
    inline int read(){
    	char ch=gc();
    	int res=0,f=1;
    	while(!isdigit(ch))f^=ch=='-',ch=gc();
    	while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    	return f?res:-res;
    }
    #define ll long long
    #define re register
    #define pii pair<int,int>
    #define fi first
    #define se second
    #define pb push_back
    #define cs const
    const int mod=998244353,g=3;
    inline int add(int a,int b){return a+b>=mod?a+b-mod:a+b;}
    inline void Add(int &a,int b){a=add(a,b);}
    inline int dec(int a,int b){return a>=b?a-b:a-b+mod;}
    inline void Dec(int &a,int b){a=dec(a,b);}
    inline int mul(int a,int b){return 1ll*a*b>=mod?1ll*a*b%mod:a*b;}
    inline void Mul(int &a,int b){a=mul(a,b);}
    inline int ksm(int a,int b,int res=1){for(;b;b>>=1,a=mul(a,a))(b&1)?(res=mul(res,a)):0;return res;}
    inline void chemx(int &a,int b){a<b?a=b:0;}
    inline void chemn(int &a,int b){a>b?a=b:0;}
    cs int N=(1<<18)+1,C=20;
    #define poly vector<int>
    int rev[N<<2];
    poly w[C+1];
    inline void init_w(){
    	int wn=ksm(g,(mod-1)/(1<<C));
    	for(int i=1;i<=C;i++)w[i].resize(1<<(i-1));
    	w[C][0]=1;
    	for(int i=1;i<(1<<(C-1));i++)w[C][i]=mul(w[C][i-1],wn);
    	for(int i=C-1;i;i--){
    		for(int j=0;j<(1<<(i-1));j++)
    		w[i][j]=w[i+1][j<<1];
    	}
    }
    inline void ntt(poly &f,int lim,int kd){
    	for(int i=0;i<lim;i++)if(i>rev[i])swap(f[i],f[rev[i]]);
    	for(int mid=1,l=1;mid<lim;mid<<=1,l++)
    	for(int i=0,a0,a1;i<lim;i+=(mid<<1))
    		for(int j=0;j<mid;j++){
    			a0=f[i+j],a1=mul(f[i+j+mid],w[l][j]);
    			f[i+j]=add(a0,a1),f[i+j+mid]=dec(a0,a1);
    		}
    	if(kd==-1){
    		reverse(f.begin()+1,f.begin()+lim);
    		for(int i=0,inv=ksm(lim,mod-2);i<lim;i++)Mul(f[i],inv);
    	}
    }
    inline void init_rev(int lim){
    	for(int i=0;i<lim;i++)rev[i]=(rev[i>>1]>>1)|((i&1)*(lim>>1));
    }
    inline poly operator *(poly a,poly b){
    	int deg=a.size()+b.size()-1,lim=1;
    	while(lim<deg)lim<<=1;
    	init_rev(lim);
    	a.resize(lim),ntt(a,lim,1);
    	b.resize(lim),ntt(b,lim,1);
    	for(int i=0;i<lim;i++)Mul(a[i],b[i]);
    	ntt(a,lim,-1),a.resize(deg);
    	return a;
    }
    int fac[N<<2],ifac[N<<2];
    inline void init(){
    	fac[0]=ifac[0]=1;
    	for(int i=1;i<N;i++)fac[i]=mul(fac[i-1],i);
    	ifac[N-1]=ksm(fac[N-1],mod-2);
    	for(int i=N-2;i;i--)ifac[i]=mul(ifac[i+1],i+1);
    }
    poly a,b,ex;
    int main(){
    	int n=read(),m=read();
    	init(),init_w();
    	for(int i=0;i<=n;i++)a.pb(read());
    	for(int i=0;i<=m;i++)b.pb(read());
    	for(int i=0;i<=n+m;i++)ex.pb(ifac[i]);
    	a.resize(n+m+1),b.resize(n+m+1);
    	a=a*ex,b=b*ex;
    	for(int i=0;i<=n+m;i++)Mul(a[i],mul(b[i],fac[i]));
    	for(int i=0;i<=n+m;i++)if(i&1)ex[i]=mod-ex[i];
    	a=a*ex;
    	for(int i=0;i<=n+m;i++)cout<<a[i]<<" ";
    }
    
  • 相关阅读:
    团队作业六
    团队作业五
    团队作业(四)
    团队作业(三)
    团队作业二
    宇宙圣斗士队介绍
    团队作业之七
    团队作业之六
    团队作业五
    奥特曼小分队之四(Work Breakdown Structure)
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/12328707.html
Copyright © 2011-2022 走看看