【BZOJ4008】亚瑟王（HNOI2015）-概率DP

$g\left(1\right)=1-\left(1-{p}_{1}{\right)}^{r}$

$f\left(i,j\right)+=\left(1-\left(1-{p}_{i}{\right)}^{r-j+1}\right)f\left(i-1,j-1\right)$

$f\left(i,j\right)+=\left(1-{p}_{i}{\right)}^{r-j}f\left(i-1,j\right)$

#include <bits/stdc++.h>
using namespace std;
int T,n,r;
long double p[510],d[510],power[510][510];
long double sumr[510]={0},sumc[510]={0},f[510][510]={0};

int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&r);
for(int i=1;i<=n;i++)
{
scanf("%Lf%Lf",&p[i],&d[i]);
power[i][0]=1.0;
for(int j=1;j<=r;j++)
power[i][j]=power[i][j-1]*(1-p[i]);
}

f[0][0]=1.0;
for(int i=1;i<=n;i++)
for(int j=0;j<=min(i,r);j++)
{
f[i][j]=0.0;
if (j>0) f[i][j]+=f[i-1][j-1]*(1-power[i][r-j+1]);
if (j<i) f[i][j]+=f[i-1][j]*power[i][r-j];
}

long double ans=0.0,sum;
for(int i=1;i<=n;i++)
{
sum=0.0;
for(int j=0;j<=min(i-1,r);j++)
sum+=f[i-1][j]*(1-power[i][r-j]);
ans+=sum*d[i];
}
printf("%.10Lf\n",ans);
}

return 0;
}
posted @ 2018-05-23 14:55  Maxwei_wzj  阅读(123)  评论(0编辑  收藏  举报