您的位置:首页 > 其它

hdu4336 Card Collector(期望dp)

2016-10-11 15:23 429 查看

hdu4336

题目

就是小时候的集卡,有N张卡,给出每张卡获得的概率,有可能没有卡,问开包的期望。

思路

概率正推期望逆推,用状态压缩表示拿到不用的卡片的期望值。开了一包还是自己的概率是当前状态有的卡的概率与开不到卡的概率和。不是自己的期望是所有当前能转移的状态的期望*由什么卡转来的概率的和+1。dp[i]-dp[i]*myself=tot+1

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long ll;

const int maxn=1<<20;

int n;
double p[25];
double none;
double dp[maxn],pp[maxn];
void pre()
{
for(int i=0; i<(1<<n); i++)
{
pp[i]=none;
for(int j=0; j<n; j++)
{
if(i&(1<<j))
pp[i]+=p[j];
}
}
}

int main()
{
while(scanf("%d",&n)!=EOF)
{
none=0;
for(int i=0; i<n; i++)
{
scanf("%lf",&p[i]);
none+=p[i];
}
none=1-none;
pre();
dp[(1<<n)-1]=0;
for(int i=(1<<n)-2; i>=0; i--)
{
double tot=0;
for(int j=0; j<n; j++)
{
if(!(i&(1<<j)))
{
tot+=dp[i|(1<<j)]*p[j];
}
}
dp[i]=(tot+1)/(1-pp[i]);
}
printf("%.4lf\n",dp[0]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: