• 【LOJ #3120】「CTS2019 | CTSC2019」珍珠(生成函数+NTT)


    传送门

    奇数的EGFEGFexex2frac{e^x-e^{-x}}{2},偶数为ex+ex2frac{e^x+e^{-x}}{2}

    考虑枚举奇数的个数
    ans=n!i=0n2m(exex2y+ex+ex2)D[xn][yi]ans=n!sum_{i=0}^{n-2m}(frac{e^x-e^{-x}}{2}y+frac{e^x+e^{-x}}{2})^D[x^n][y^i]
    =n!2Di=0n2m((1+y)ex+(1y)ex)D[xn][yi]=frac{n!}{2^D}sum_{i=0}^{n-2m}((1+y)e^x+(1-y)e^{-x})^D[x^n][y^i]
    =n!2Di=0n2mj=0D(Dj)e(2jD)x(1+y)i(1y)Di=frac{n!}{2^D}sum_{i=0}^{n-2m}sum_{j=0}^{D}{Dchoose j}e^{(2j-D)x}(1+y)^i(1-y)^{D-i}

    =n!2Dj=0D(Dj)(2jD)ni=0n2m(1+y)D(1y)Di[yi]=frac{n!}{2^D}sum_{j=0}^{D}{Dchoose j}(2j-D)^nsum_{i=0}^{n-2m}(1+y)^D(1-y)^{D-i}[y^i]

    =n!2Dj=0D(Dj)(2jD)n(1+y)D(1y)Dj(1+y+y2...)[yn2m]=frac{n!}{2^D}sum_{j=0}^{D}{Dchoose j}(2j-D)^n(1+y)^D(1-y)^{D-j}(1+y+y^2...)[y^{n-2m}]

    =n!2Dj=0D(Dj)(2jD)n(1+y)D(1y)Dj11y[yn2m]=frac{n!}{2^D}sum_{j=0}^{D}{Dchoose j}(2j-D)^n(1+y)^D(1-y)^{D-j}frac{1}{1-y}[y^{n-2m}]

    对于j=Dj=D可以单独算
    否则暴力二项式展开后是一个卷积的形式
    直接NTTNTT算即可

    #include<bits/stdc++.h>
    using namespace std;
    #define re register
    #define ll long long
    #define pb push_back
    #define cs const
    #define bg begin
    #define pii pair<int,int>
    #define fi first
    #define se second
    #define poly vector<int>
    cs int RLEN=1<<20|1;
    inline char gc(){
    	static char ibuf[RLEN],*ib,*ob;
    	(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    	return (ib==ob)?EOF:*ib++;
    }
    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;
    }
    inline ll readl(){
    	char ch=gc();
    	ll res=0;bool 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;
    }
    template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
    template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
    cs int mod=998244353,G=3;
    inline int add(int a,int b){a+=b-mod;return a+(a>>31&mod);}
    inline void Add(int &a,int b){a+=b-mod,a+=a>>31&mod;}
    inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
    inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
    inline int mul(int a,int b){static ll r;r=1ll*a*b;return r>=mod?r%mod:r;}
    inline void Mul(int &a,int b){static ll r;r=1ll*a*b,a=r>=mod?r%mod:r;}
    inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
    inline int Inv(int x){return ksm(x,mod-2);}
    cs int N=100005;
    poly w[19];
    int rev[(1<<18)|5];
    inline void init_w(){
    	cs int C=18;
    	for(int i=1;i<=C;i++)w[i].resize(1<<(i-1));
    	int wn=ksm(G,(mod-1)/(1<<C));
    	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 init_rev(int lim){
    	for(int i=0;i<lim;i++)rev[i]=(rev[i>>1]>>1)|((i&1)*(lim>>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,a0,a1;mid<lim;mid<<=1,l++)
    	for(int i=0;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.bg()+1,f.bg()+lim);
    		for(int i=0,iv=Inv(lim);i<lim;i++)Mul(f[i],iv);
    	}
    }
    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],ifac[N];
    inline void init_inv(){
    	fac[0]=ifac[0]=1;
    	for(int i=1;i<N;i++)fac[i]=mul(fac[i-1],i);
    	ifac[N-1]=Inv(fac[N-1]);
    	for(int i=N-2;i;i--)ifac[i]=mul(ifac[i+1],i+1);
    }
    inline int C(int n,int m){
    	return n<m?0:mul(fac[n],mul(ifac[m],ifac[n-m]));
    }
    int d,n,m;
    
    
    int main(){
    	#ifdef Stargazer
    	freopen("lx.cpp","r",stdin);
    	#endif
    	init_w(),init_inv();
    	d=read(),n=read(),m=read();
    	int lim=n-2*m;
    	poly f(d+1,0),g(d+1,0);
    	for(int i=0;i<=d;i++){
    		if(i<=lim&&lim-i<=d){
    			f[i]=mul(ifac[i],ifac[lim-i]);
    			if((lim-i)&1)f[i]=dec(0,f[i]);
    		}
    		if(d>=1+lim+i)g[i]=mul(ifac[i],ifac[d-1-lim-i]);
    	}
    	f=f*g;
    	int res=0;
    	for(int i=0;i<d;i++)Add(res,mul(mul(C(d,i),ksm(((2*i-d)%mod+mod)%mod,n)),mul(mul(fac[i],fac[d-1-i]),f[i])));
    	for(int i=0;i<=min(lim,d);i++)Add(res,mul(C(d,i),ksm(d,n)));
    	cout<<mul(Inv(ksm(2,d)),res);
    }
    
  • 相关阅读:
    singleton 单例模式
    try catch finall 结构里的 return
    ConcurrentHashMap
    ConcurrentHashMap原理分析
    Java 实现两个整数变量的交换
    Java reflect 反射 3 Class.forname
    Java reflect 反射 2
    Java reflect 反射 1
    java class load 类加载
    HashMap源码解析
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/12328372.html
Copyright © 2020-2023  润新知