luogu P5401 [CTS2019]珍珠

luogu

问题等价于求出现偶数次的颜色种数在\([\max(D-(n-2m),0),D]\)之间的序列个数.所以我们可以先枚举所有颜色出现次数,然后考虑对应的序列方案数

如果有\(k\)种颜色出现偶数次,那么对应序列个数为\(n![x^n]((\frac{e^x+e^{-x}}{2})^k*(\frac{e^x-e^{-x}}{2})^{D-k})\).为了方便计算,这里可以考虑求\(g_k\)表示强制\(k\)种颜色出现偶数次,对应式子为

\(g_k=n![x^n]((\frac{e^x+e^{-x}}{2})^k*(e^x)^{D-k})\)

\(=n!2^k[x^n]\sum_{j=0}^{k}\binom{k}{j}e^{jx}e^{-(k-j)x}*e^{(D-k)x}\)

\(=n!2^k[x^n]\sum_{j=0}^{k}\binom{k}{j}e^{(D+2(j-k))x}\)

然后考虑求\(f_k\)为恰好\(k\)种颜色出现偶数次的序列个数,枚举\(f_k\)中的\(k\)种颜色对应是\(g_i(i\le k)\)的哪\(j\)种,有\(g_i=\sum_{k=i}^{D}\binom{k}{i}f_k\),二项式反演得

\(f_i=\sum_{k=i}^{D}(-1)^{k-i}\binom{k}{i}g_k\)

\(=\sum_{k=i}^{D}(-1)^{k-i}\binom{k}{i}n!2^k[x^n]\sum_{j=0}^{k}\binom{k}{j}e^{(D+2(j-k))x}\)

\(=n![x^n]\sum_{k=i}^{D}(-1)^{k-i}\binom{k}{i}2^k\sum_{j=0}^{k}\binom{k}{j}e^{(D+2(j-k))x}\)

\(=n![x^n]\sum_{d=0}^{D}e^{(D-2d)x}\sum_{k=i}^{D}2^k(-1)^{k-i}\binom{k}{i}\binom{k}{k-d}\)

要求的答案其实是

\(ans=\sum_{i=\max(D-(n-2m),0)}^{D}f_i\)

\(=\sum_{i=\max(D-(n-2m),0)}^{D}n![x^n]\sum_{d=0}^{D}e^{(D-2d)x}\sum_{k=i}^{D}2^k(-1)^{k-i}\binom{k}{i}\binom{k}{k-d}\)

\(=n![x^n]\sum_{d=0}^{D}e^{(D-2d)x}\sum_{k=1}^{D}\binom{k}{k-d}2^k\sum_{i=\max(D-(n-2m),0)}^{D}(-1)^{k-i}\binom{k}{i}\)

\(=n![x^n]\sum_{d=0}^{D}e^{(D-2d)x}\sum_{k=1}^{D}\frac{k!}{d!(k-d)!}2^k\sum_{i=\max(D-(n-2m),0)}^{D}(-1)^{k-i}\frac{k!}{i!(k-i)!}\)

\(=n![x^n]\sum_{d=0}^{D}\frac{e^{(D-2d)x}}{d!}\sum_{k=1}^{D}\frac{1}{(k-d)!}2^kk!k!\sum_{i=\max(D-(n-2m),0)}^{D}\frac{(-1)^{k-i}}{i!(k-i)!}\)

由于\(d\)\(i\)无关所以可以两次卷积算.注意\(e^{f(x)}\)\(n\)次项系数中的\(\frac{1}{n!}\)和外面的\(n!\)可以正好抵消

#include<bits/stdc++.h>
#define LL long long
#define db double

using namespace std;
const int N=(1<<18)+10,mod=998244353;
int rd()
{
    int x=0,w=1;char ch=0;
    while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+(ch^48);ch=getchar();}
    return x*w;
}
int fpow(int a,int b){int an=1;while(b){if(b&1) an=1ll*an*a%mod;a=1ll*a*a%mod,b>>=1;}return an;}
int ginv(int a){return fpow(a,mod-2);}
int W[18],iW[18],rdr[N];
void ntt(int *a,int n,bool op)
{
	int l=0,y;
	while(1<<l<n) ++l;
	for(int i=0;i<n;++i)
	{
		rdr[i]=(rdr[i>>1]>>1)|((i&1)<<(l-1));
		if(i<rdr[i]) swap(a[i],a[rdr[i]]);
	}
	for(int i=1,p=0;i<n;i<<=1,++p)
	{
		int ww=op?W[p]:iW[p];
		for(int j=0;j<n;j+=i<<1)
			for(int k=0,w=1;k<i;++k,w=1ll*w*ww%mod)
				y=1ll*a[j+k+i]*w%mod,a[j+k+i]=(a[j+k]-y+mod)%mod,a[j+k]=(a[j+k]+y)%mod;
	}
	if(!op) for(int i=0,w=ginv(n);i<n;++i) a[i]=1ll*a[i]*w%mod;
}
int n,m,k,aa[N],bb[N],fac[N],iac[N];
int C(int a,int b){return b<0||a<b?0:1ll*fac[a]*iac[b]%mod*iac[a-b]%mod;}

int main()
{
	for(int i=1,p=0;p<18;i<<=1,++p)
		W[p]=fpow(3,(mod-1)/(i<<1)),iW[p]=ginv(W[p]);
	fac[0]=1;
	for(int i=1;i<=N-5;++i) fac[i]=1ll*fac[i-1]*i%mod;
	iac[N-5]=ginv(fac[N-5]);
	for(int i=N-5;i;--i) iac[i-1]=1ll*iac[i]*i%mod;
	n=rd(),m=rd(),k=rd(),k=max(n-(m-k*2),0);
	int len=1;
	while(len<=n+n) len<<=1;
	for(int i=k;i<=n;++i) aa[i]=iac[i];
	for(int i=0;i<=n;++i) bb[i]=(i&1)?mod-iac[i]:iac[i];
	ntt(aa,len,1),ntt(bb,len,1);
	for(int i=0;i<len;++i) aa[i]=1ll*aa[i]*bb[i]%mod;
	ntt(aa,len,0);
	for(int i=n+1;i<len;++i) aa[i]=0;
	for(int i=0;i<=n;++i) aa[i]=1ll*aa[i]*fac[i]%mod*fac[i]%mod*C(n,i)%mod*fpow((mod+1)>>1,i)%mod;
	memset(bb,0,sizeof(int)*len);
	for(int i=0;i<=n;++i) bb[i]=iac[n-i];
	ntt(aa,len,1),ntt(bb,len,1);
	for(int i=0;i<len;++i) aa[i]=1ll*aa[i]*bb[i]%mod;
	ntt(aa,len,0);
	int ans=0;
	for(int i=0;i<=n;++i) ans=(1ll*aa[n+i]*iac[i]%mod*fpow(n-2*i,m)%mod+mod+ans)%mod;
	printf("%d\n",ans);
	return 0;
}
posted @ 2020-06-01 19:31  ✡smy✡  阅读(147)  评论(0编辑  收藏  举报