[HEOI2016/TJOI2016] 求和

\(\text{Problem}:\)[HEOI2016/TJOI2016]求和

\(\text{Solution}:\)

引理 \(1\)

\[{n\brace m}=\sum\limits_{i=0}^{m}\cfrac{(-1)^{m-i}i^{n}}{i!(m-i)!} \]

可以用二项式反演证明。

大力推导 \(f(n)\)

\[\begin{aligned} f(n)&=\sum\limits_{i=0}^{n}\sum\limits_{j=0}^{i}{i\brace j}\times 2^{j}\times j!\\ &=\sum\limits_{i=0}^{n}\sum\limits_{j=0}^{n}{i\brace j}\times 2^{j}\times j!\\ &=\sum\limits_{i=0}^{n}\sum\limits_{j=0}^{n}2^{j}\times j!\times \left(\sum\limits_{k=0}^{j}\cfrac{(-1)^{j-k}k^{i}}{k!(j-k)!}\right)\\ &=\sum\limits_{i=0}^{n}\sum\limits_{j=0}^{n}2^{j}\times \left(\sum\limits_{k=0}^{j}(-1)^{j-k}\binom{j}{k}k^{i}\right) \end{aligned} \]

发现 \(k^{i}\) 难以直接处理,考虑交换枚举顺序,有:

\[\begin{aligned} f(n)&=\sum\limits_{j=0}^{n}\sum\limits_{k=0}^{j}2^{j}(-1)^{j-k}\binom{j}{k}\sum\limits_{i=0}^{n}k^{i}\\ &=\sum\limits_{j=0}^{n}2^{j}\times j!\sum\limits_{k=0}^{j}\cfrac{(-1)^{j-k}}{(j-k)!}\cdot\cfrac{1-k^{n+1}}{k!(1-k)}\\ &=\sum\limits_{j=0}^{n}2^{j}\times j!\sum\limits_{k=0}^{j}A_{j-k}\times B_{k}\\ &=\sum\limits_{j=0}^{n}2^{j}\times j!\times C_{j} \end{aligned} \]

注意 \(B_{1}=n+1\)。利用 \(\text{NTT}\) 可以在 \(O(n\log n)\) 在时间复杂度内解决本题。

\(\text{Code}:\)

#include <bits/stdc++.h>
#pragma GCC optimize(3)
//#define int long long
#define ri register
#define mk make_pair
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define is insert
#define es erase
#define vi vector<int>
#define vpi vector<pair<int,int>>
using namespace std; const int N=300010, Mod=998244353;
inline int read()
{
	int s=0, w=1; ri char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar();
	return s*w;
}
int n,A[N],B[N],ans;
int rev[N],T=1,r[24][2],fac[N+5],inv[N+5];
inline int ksc(int x,int p) { int res=1; for(;p;p>>=1, x=1ll*x*x%Mod) if(p&1) res=1ll*res*x%Mod; return res; }
inline void Get_Rev() { for(ri int i=0;i<T;i++) rev[i]=(rev[i>>1]>>1)|((i&1)?(T>>1):0); }
inline void NTT(int *s,int type)
{
	for(ri int i=0;i<T;i++) if(rev[i]<i) swap(s[i],s[rev[i]]);
	for(ri int i=2,cnt=1;i<=T;i<<=1,cnt++)
	{
		int wn=r[cnt][type];
		for(ri int j=0,mid=(i>>1);j<T;j+=i)
		{
			for(ri int k=0,w=1;k<mid;k++,w=1ll*w*wn%Mod)
			{
				int x=s[j+k], y=1ll*w*s[j+mid+k]%Mod;
				s[j+k]=(x+y)%Mod;
				s[j+mid+k]=x-y;
				if(s[j+mid+k]<0) s[j+mid+k]+=Mod;
			}
		}
	}
	if(!type) for(ri int i=0,inv=ksc(T,Mod-2);i<T;i++) s[i]=1ll*s[i]*inv%Mod;
}
signed main()
{
	r[23][1]=ksc(3,119), r[23][0]=ksc(ksc(3,Mod-2),119);
	for(ri int i=22;~i;i--) r[i][0]=1ll*r[i+1][0]*r[i+1][0]%Mod, r[i][1]=1ll*r[i+1][1]*r[i+1][1]%Mod;
	fac[0]=1;
	for(ri int i=1;i<=N;i++) fac[i]=1ll*fac[i-1]*i%Mod;
	inv[N]=ksc(fac[N],Mod-2);
	for(ri int i=N;i;i--) inv[i-1]=1ll*inv[i]*i%Mod;
	n=read();
	for(ri int i=0;i<=n;i++)
	{
		A[i]=((i&1)?(-1):(1))*inv[i];
		if(A[i]<0) A[i]+=Mod;
		if(!i) B[i]=1;
		else if(i==1) B[i]=n+1;
		else B[i]=1ll*(ksc(i,n+1)-1)*ksc(i-1,Mod-2)%Mod*inv[i]%Mod;
	}
	while(T<=n+n) T<<=1;
	Get_Rev();
	NTT(A,1), NTT(B,1);
	for(ri int i=0;i<T;i++) A[i]=1ll*A[i]*B[i]%Mod;
	NTT(A,0);
	for(ri int i=0,pw=1;i<=n;i++) ans=(ans+1ll*fac[i]*pw%Mod*A[i]%Mod)%Mod, pw=pw*2%Mod;
	printf("%d\n",ans);
	return 0;
}
posted @ 2021-04-23 11:47  zkdxl  阅读(57)  评论(0编辑  收藏  举报