Problem Description
In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, for example, if you collect all the  people in the famous novel Water Margin, you will win an amazing award. 

As a smart boy, you notice that to win the award, you must buy much more snacks than it seems to be. To convince your friends not to waste money any more, you should find the expected number of snacks one should buy to collect a full suit of cards.
Input
The first line of each test case contains one integer N ( <= N <= ), indicating the number of different cards you need the collect. The second line contains N numbers p1, p2, ..., pN, (p1 + p2 + ... + pN <= ), indicating the possibility of each card to appear in a bag of snacks. 

Note there is at most one card in a bag of snacks. And it is possible that there is nothing in the bag.
 
Output
Output one number for each test case, indicating the expected number of bags to buy to collect all the N different cards.

You will get accepted if the difference between your answer and the standard answer is no more that ^-.
 
Sample Input
0.1 

0.1 0.4
 
Sample Output
10.000
10.500
 
Source

题目大意

买东西集齐全套卡片赢大奖。每个包装袋里面最多一张卡片,最少可以没有。且给了每种卡片出现的概率 p[i],以及所有的卡片种类的数量 n(1<=n<=20),问集齐卡片需要买东西的数量的期望值。需要注意的是 包装袋中可以没有卡片,也就是说:segma{ p[i] }<=1.0,i=0,2,...,n-1

 

做法分析

由于卡片最多只有 20 种,使用状态压缩,用 0 表示这种卡片没有收集到, 1 表示这种卡片收集到了

令:f[s] 表示已经集齐的卡片种类的状态的情况下,收集完所有卡片需要买东西次数的期望

买一次东西,包装袋中可能:

        1. 没有卡片

        2. 卡片是已经收集到的

        3. 卡片是没有收集到的

于是有:

        f[s] = 1 + ((1-segma{ p[i] })f[s]) + (segma{ p[j]*f[s] }) + (segma{ p[k]*f[s|(1<<k)] })

        其中:    i=0,2,...,n-1

                      j=第 j 种卡片已经收集到了,即 s 从右往左数第 j 位是 1:s&(1<<j)!=0

                      k=第 k 种卡片没有收集到,即 s 从右往左数第 k 位是 0:s&(1<<k)==0

移项可得:

        segma{ p[i] }f[s] = 1 + segma{ p[i]*f[s|(1<<i) },i=第i 种卡片没有收集到

目标状态是:f[0]

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 26
#define M 1<<21
double p[N];
double dp[M];
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
//double sum=0;
for(int i=;i<n;i++){
scanf("%lf",&p[i]);
//sum+=p[i];
} int all=(<<n)-;
dp[all]=;
for(int i=all-;i>=;i--){
dp[i]=;
double tmp=;
for(int j=;j<n;j++){
if(i&(<<j)) continue;
dp[i]=dp[i]+dp[i|(<<j)]*p[j];
tmp+=p[j];
}
dp[i]/=tmp;
} printf("%lf\n",dp[]); }
return ;
}

hdu 4336 Card Collector(期望 dp 状态压缩)的更多相关文章

  1. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

  2. HDU 4336 Card Collector 期望dp+状压

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Time Limit: 2000/1000 MS (Java/O ...

  3. hdu4336 Card Collector(概率DP,状态压缩)

    In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...

  4. $HDU$ 4336 $Card\ Collector$ 概率$dp$/$Min-Max$容斥

    正解:期望 解题报告: 传送门! 先放下题意,,,已知有总共有$n$张卡片,每次有$p_i$的概率抽到第$i$张卡,求买所有卡的期望次数 $umm$看到期望自然而然想$dp$? 再一看,哇,$n\le ...

  5. 【bzoj1076】[SCOI2008]奖励关 期望dp+状态压缩dp

    题目描述 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝物以后也不能再 ...

  6. HDU 1074 Doing Homework (dp+状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...

  7. hdu 4336 Card Collector

    dp+状态压缩 #include<cstdio> using namespace std; ]; <<]; int main() { int n; while(scanf(&q ...

  8. [HDU 4336] Card Collector (状态压缩概率dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题目大意:有n种卡片,需要吃零食收集,打开零食,出现第i种卡片的概率是p[i],也有可能不出现卡 ...

  9. HDU 4336 Card Collector:状压 + 期望dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意: 有n种卡片(n <= 20). 对于每一包方便面,里面有卡片i的概率为p[i],可 ...

随机推荐

  1. hdu 5072 Coprime (容斥)

    Problem Description There are n people standing in a line. Each of them has a unique id number. Now ...

  2. 关于时间的操作(JavaScript版)——依据不同区时显示对应的时间

    如今项目基本上告一段落了,难得有一定的闲暇,今天利用数小时完毕了一个功能模块--依据不同区时显示对应的时间,这方面网上基本没有现成的样例,如今将代码粘贴例如以下: <!DOCTYPE HTML ...

  3. oracle 使用sql获取数据库表,表的字段

    --第一种方法: 查询dba_tab_columns select COLUMN_NAME,DATA_TYPE,DATA_LENGTH  from   dba_tab_columns where  t ...

  4. Button和ImageButton

    Button----button ImageButton----图片button 共同拥有特征: 都能够作为一个button产生点击事件 不同点 1. Button有text的属性.ImageButt ...

  5. CentOS7 安装LNMP(Linux+Nginx+MySQL+PHP)

    由于工作须要,须要学习php,本来想安装lamp的可是考虑到如今nginxserver有良好的性能且应用广泛. 这里我决定搭建Linux(CentOS7+Nginx+MySQL+PHP)下的webse ...

  6. [CSAPP笔记][第六章存储器层次结构]

    第六章 存储器层次结构 在简单模型中,存储器系统是一个线性的字节数组,CPU能够在一个常数访问每个存储器位置. 虽然是一个行之有效的模型,但没有反应现代系统实际工作方式. 实际上,存储器系统(memo ...

  7. 【转载】CocoaPods安装和使用教程

    转自:http://code4app.com/article/cocoapods-install-usage 目录 CocoaPods是什么? 如何下载和安装CocoaPods? 如何使用CocoaP ...

  8. WebApi2官网学习记录---JSON与XML的序列化

    JSON序列化: WebAPI的默认序列库使用的是Json.NET,可以在Globally中配置使用DataContractJsonSerializer 进行序列化 protected void Ap ...

  9. Java------------运算符优先级速记口诀

    单目乘加位关系,逻辑三目后赋值. 单目:单目运算符+ –(负数) ++ -- 等 乘加(乘除加减):算数单目运算符* / % + - 位:位移单目运算符<< >> 关系:关系单 ...

  10. 0301——SearchController

    创建显示的页面 SearchViewController * searchVC = [[SearchViewController alloc]init]; 告诉搜索控制器将结果显示在创建的页面上 se ...