B-Sample Game
ding_ning123大佬题解
注:上述题解图片来自ding_ning123大佬题解
Code
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template <class T=int> T rd()
{T res=0;T fg=1;char ch=getchar();while(!isdigit(ch)) {if(ch=='-') fg=-1;ch=getchar();}while( isdigit(ch)) res=(res<<1)+(res<<3)+(ch^48),ch=getchar();return res*fg;
}
const int N=110,mod=998244353;int p[N],n,tot;
ll inv[N];
ll f[N],g[N];ll qmi(ll a,ll b)
{ll v=1;while(b){if(b&1) v=v*a%mod;a=a*a%mod;b>>=1;}return v;
}int main()
{n=rd();for(int i=1;i<=n;i++) p[i]=rd(),tot+=p[i];for(int i=1;i<=n;i++) inv[i]=qmi(tot-p[i],mod-2);f[n]=tot*inv[n]%mod;for(int i=n-1;i>=1;i--){f[i]=tot;for(int j=i+1;j<=n;j++) f[i]=(f[i]+p[j]*f[j]%mod)%mod;f[i]=f[i]*inv[i]%mod;}g[n]=(tot+2*p[n]*f[n]%mod)*inv[n]%mod;for(int i=n-1;i>=1;i--){g[i]=(tot+2*p[i]*f[i]%mod)%mod;for(int j=i+1;j<=n;j++) g[i]=(g[i]+p[j]*(g[j]+2*f[j])%mod)%mod;g[i]=g[i]*inv[i]%mod;}ll ans=1;ll invP=qmi(tot,mod-2);for(int i=1;i<=n;i++) ans=(ans+invP*p[i]%mod*(g[i]+2*f[i])%mod)%mod;printf("%lld\n",ans);return 0;
}
#4318. OSU!
【bzoj 4318】OSU!大佬题解
注:上述题解图片来自【bzoj 4318】OSU!大佬题解
Code1
上述递推式有一些瑕疵应该是:
fi,2=(1−pi)×fi−1,2+pi×(fi−1,2+2gi−1,1+1)f_{i,2}=(1-p_i)×f_{i-1,2}+p_i×(f_{i-1,2}+2g_{i-1,1}+1)fi,2=(1−pi)×fi−1,2+pi×(fi−1,2+2gi−1,1+1)
fi,3=(1−pi)×fi−1,3+pi×(fi−1,3+3gi−1,1+3gi−1,2+1)f_{i,3}=(1-p_i)×f_{i-1,3}+p_i×(f_{i-1,3}+3g_{i-1,1}+3g_{i-1,2}+1)fi,3=(1−pi)×fi−1,3+pi×(fi−1,3+3gi−1,1+3gi−1,2+1)
#include<cstdio>double f[100005][4];
double g[100005][3];
int n;
int main()
{scanf("%d",&n);for(int i=1;i<=n;i++){double p;scanf("%lf",&p);g[i][1]=p*(g[i-1][1]+1);g[i][2]=p*(g[i-1][2]+2*g[i-1][1]+1);f[i][1]=(1.0-p)*f[i-1][1]+p*(f[i-1][1]+1);f[i][2]=(1.0-p)*f[i-1][2]+p*(f[i-1][2]+2*g[i-1][1]+1);f[i][3]=(1.0-p)*f[i-1][3]+p*(f[i-1][3]+3*g[i-1][1]+3*g[i-1][2]+1);}printf("%.1lf\n",f[n][3]);return 0;
}
Code2
#include<cstdio>double f[100005][3];
int n;
int main()
{scanf("%d",&n);double ans=0;for(int i=1;i<=n;i++){double p;scanf("%lf",&p);f[i][0]=p*(f[i-1][0]+1);f[i][1]=p*(f[i-1][1]+2*f[i-1][0]+1);f[i][2]=p*(3*f[i-1][1]+3*f[i-1][0]+1);ans+=f[i][2];}printf("%.1lf\n",ans);return 0;
}