CF1156F Card Bag
Jerrywang09 · · 题解
不是很困难的概率 DP 题。
考虑摸牌的过程是什么样的。其一定形如先摸了若干张权值下降的牌,最后摸了两张权值相同的牌。
不难设出 DP 状态和转移:
其中,
考虑前缀和优化并预处理逆元,维护
// Title: Card Bag
// Source: CF1156F
// Author: Jerrywang
#include <cstdio>
#include <iostream>
#define ll long long
#define rep(i, s, t) for(int i=s; i<=t; ++i)
#define debug(x) cerr<<#x<<":"<<x<<endl;
const int N=5005, mod=998244353;
using namespace std;
char buf[1<<23], *p1=buf, *p2=buf;
#define gc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline int read()
{
int x=0, f=1; char c=gc();
while(c<'0' || c>'9') c=='-' && (f=-1), c=gc();
while('0'<=c && c<='9') x=(x<<3)+(x<<1)+c-'0', c=gc();
return x*f;
}
int n, cnt[N], a[N]; ll f[N][N], sum[N], inv[N];
inline void upd(ll &x, ll y)
{
x+=y; if(x>=mod) x-=mod;
}
ll qp(ll x, ll y=mod-2)
{
ll res=1;
for(; y; y>>=1, x=x*x%mod) if(y&1) res=res*x%mod;
return res;
}
int main()
{
#ifdef Jerrywang
freopen("E:/OI/in.txt", "r", stdin); ios::sync_with_stdio(0);
#endif
n=read();
rep(i, 1, n) a[i]=read(), cnt[a[i]]++;
rep(i, 0, n) inv[i]=qp(i);
rep(i, 1, n)
{
f[i][1]=cnt[i]*inv[n]%mod;
rep(j, 2, n)
{
upd(f[i][j], sum[j-1]*cnt[i]%mod);
// rep(k, 1, i-1)
// {
// f[i][j]=(f[i][j]+f[k][j-1]*cnt[i]%mod*qp(n-j+1)%mod)%mod;
// }
}
rep(j, 1, n)
{
upd(sum[j], f[i][j]*inv[n-j]%mod);
}
}
ll res=0;
rep(i, 1, n) rep(j, 1, n)
{
upd(res, f[i][j]*(cnt[i]-1)%mod*inv[n-j]%mod);
}
printf("%lld", res);
return 0;
}