1985. 神枪手

考点:概率dp

先回忆一下几何分布的期望:对于射击,生效的概率为 p,未生效的概率为 (1-p);那么第一次生效的期望就是 \frac{1}{p}

a[j+1]+dp[i-1][j]

#include<bits/stdc++.h> using namespace std; #define int long long #define endl '\n' using ll = long long; const int mod=998244353; int qpow(int x,int y){ int rs=1; while(y){ if(y&1) rs=rs*x%mod; x=x*x%mod; y>>=1; } return rs; } signed main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin>>n; vector<int> a(n+2); int sum=0; for(int i=1;i<=n;i++){ cin>>a[i]; sum+=a[i]; sum%=mod; } vector<vector<int>> dp(5010,vector<int>(5010)); dp[1][n]=0; for(int len=n-1;len;len--){ for(int i=1;i+len-1<=n;i++){ int j=i+len-1; int tmp=a[i-1]+a[j+1]; int inv=qpow(tmp,mod-2); if(i-1>=1){ dp[i][j]+=dp[i-1][j]*a[i-1]%mod*inv%mod; dp[i][j]%=mod; } if(j+1<=n){ dp[i][j]+=dp[i][j+1]*a[j+1]%mod*inv%mod; dp[i][j]%=mod; } dp[i][j]+=sum*inv%mod; dp[i][j]%=mod; } } int ans=0; int invs=qpow(sum,mod-2); for(int i=1;i<=n;i++){ ans+=(dp[i][i]+1)*a[i]%mod*invs%mod; ans%=mod; } cout<<ans; return 0; }