\(\text{Problem}:\)CF1278F Cards 加强版
\(\text{Solution}:\)
设 \(p=\frac{1}{m}\),要求的是:
\[\sum\limits_{i=0}^{n}\binom{n}{i}p^{i}(1-p)^{n-i}i^{k} \]将 \(i^{k}\) 利用第二类斯特林数展开,有:
\[\begin{aligned} ans&=\sum\limits_{i=0}^{n}\binom{n}{i}p^{i}(1-p)^{n-i}\sum\limits_{j=0}^{k}{k\brace j}i^{\underline{j}}\\ &=\sum\limits_{j=0}^{k}{k\brace j}\sum\limits_{i=j}^{n}i^{\underline{j}}\binom{n}{i}p^{i}(1-p)^{n-i}\\ &=\sum\limits_{j=0}^{k}{k\brace j}\sum\limits_{i=j}^{n}\binom{n-j}{i-j}n^{\underline{j}}p^{i}(1-p)^{n-i}\\ &=\sum\limits_{j=0}^{k}{k\brace j}n^{\underline{j}}p^{j}\sum\limits_{i=0}^{n-j}\binom{n-j}{i}p^{i}(1-p)^{n-i-j}\\ &=\sum\limits_{j=0}^{k}{k\brace j}n^{\underline{j}}p^{j} \end{aligned} \]至此,可以在 \(O(k\log k)\) 的时间复杂度内求出答案,但无法通过本题。
考虑利用二项式反演展开 \({k\brace j}\),有:
\[\begin{aligned} ans&=\sum\limits_{j=0}^{k}p^{j}\binom{n}{j}j!\sum\limits_{i=0}^{j}\frac{(-1)^{j-i}i^{k}}{i!(j-i)!}\\ &=\sum\limits_{j=0}^{k}p^{j}\binom{n}{j}\sum\limits_{i=0}^{j}\binom{j}{i}(-1)^{j-i}i^{k}\\ &=\sum\limits_{i=0}^{k}i^{k}\sum\limits_{j=i}^{k}p^{j}\binom{n}{j}(-1)^{j-i}\binom{j}{i}\\ &=\sum\limits_{i=0}^{k}i^{k}(-1)^{-i}\sum\limits_{j=i}^{k}(-p)^{j}\binom{n}{j}\binom{j}{i}\\ &=\sum\limits_{i=0}^{k}i^{k}(-1)^{i}\sum\limits_{j=i}^{k}(-p)^{j}\binom{n-i}{j-i}\binom{n}{i}\\ &=\sum\limits_{i=0}^{k}i^{k}(-1)^{i}(-p)^{i}\binom{n}{i}\sum\limits_{j=0}^{k-i}(-p)^{j}\binom{n-i}{j}\\ \end{aligned} \]发现 \(\sum_{j=0}^{k-i}\) 中,当 \(n>k\) 时无法使用二项式定理,难以直接展开,而 \(i\) 和 \(k\) 同阶,故考虑递推求解。
设 \(g_{i}=\sum\limits_{j=0}^{k-i}(-p)^{j}\binom{n-i}{j}\),有:
\[\begin{aligned} g_{i}&=\sum\limits_{j=0}^{k-i}(-p)^{j}\binom{n-i}{j}\\ &=\sum\limits_{j=0}^{k-i}(-p)^{j}\left(\binom{n-i-1}{j}+\binom{n-i-1}{j-1}\right)\\ &=(-p+1)g_{i+1}+(-p)^{k-i}\binom{n-i-1}{k-i} \end{aligned} \]边界条件为 \(g_{k}=1\)。预处理 \(p\) 的幂,可以 \(O(k)\) 递推出 \(g_{i}\)。
将 \(g_{i}\) 带回原式,有:
\[ans=\sum\limits_{i=0}^{k}i^{k}p^{i}\binom{n}{i}g_{i} \]由于 \(i^{k}\) 是积性函数,线性筛即可。求 \(\binom{n}{i}\) 将其展开为下降幂形式,边算边乘。总时间复杂度 \(O(k)\)。
为方便,将 \(n\leq k\) 与 \(n>k\) 分别实现。
\(\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=10000010, 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,P,K,up,iiv[N],pw[N],pw2[N],g[N],ans;
int pri[N/10],cnt,w[N]; bool book[N];
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 Init()
{
iiv[1]=1;
for(ri int i=2;i<=up;i++) iiv[i]=1ll*(Mod-Mod/i)*iiv[Mod%i]%Mod;
pw[0]=1;
P=ksc(P,Mod-2);
for(ri int i=1;i<=up;i++) pw[i]=1ll*pw[i-1]*P%Mod;
w[1]=1;
for(ri int i=2;i<=up;i++)
{
if(!book[i]) pri[++cnt]=i, w[i]=ksc(i,K);
for(ri int j=1;j<=cnt&&i*pri[j]<=up;j++)
{
book[i*pri[j]]=1;
w[i*pri[j]]=1ll*w[i]*w[pri[j]]%Mod;
if(i%pri[j]==0) break;
}
}
}
signed main()
{
n=read(), P=read(), K=read();
up=min(n,K);
Init();
int Q=(Mod+1-P)%Mod;
if(n<=K)
{
pw2[0]=1;
if(Q) for(ri int i=1;i<=n;i++) pw2[i]=1ll*pw2[i-1]*Q%Mod;
for(ri int i=0,now=1,inv=1;i<=n;i++)
{
ans=(ans+1ll*w[i]*pw[i]%Mod*now%Mod*inv%Mod*pw2[n-i]%Mod)%Mod;
now=1ll*now*(n-i)%Mod;
inv=1ll*inv*iiv[i+1]%Mod;
}
printf("%d\n",ans);
return 0;
}
g[K]=1;
int now=1;
for(ri int i=K-1;~i;i--)
{
now=1ll*now*(n-i-1)%Mod*iiv[K-i]%Mod;
g[i]=1ll*now*pw[K-i]%Mod;
if((K-i)&1) g[i]=Mod-g[i];
g[i]=(g[i]+1ll*g[i+1]*Q%Mod)%Mod;
}
for(ri int i=0,now=1,inv=1;i<=K;i++)
{
ans=(ans+1ll*w[i]*pw[i]%Mod*now%Mod*inv%Mod*g[i]%Mod)%Mod;
now=1ll*now*(n-i)%Mod;
inv=1ll*inv*iiv[i+1]%Mod;
}
printf("%d\n",ans);
return 0;
}