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. MVC 部分视图

    ASP.NET MVC 里的部分视图,相当于 Web Form 里的 User Control.我们的页面往往会有许多重用的地方,可以进行封装重用. 使用部分视图有以下优点:   1. 可以简写代码. ...

  2. NetAnalyzer笔记 之 八 NetAnalyzer2016使用方法(2)

    [创建时间:2016-05-06 22:07:00] NetAnalyzer下载地址 在写本篇的时候,NetAnalyzer 3.1版本已经发布,所以本篇就以最新版本的为例继续使用,并且顺带说明一下, ...

  3. 正则表达式获取URL参数

    使用到的正则表达式: [^\?&]?参数名=[^&]+ document.location.getURLPara = function (name) { var reg = new R ...

  4. Linux应用开发环境搭建

    因为笔者是一名大学生,对Linux内核开发方向非常感兴趣,可是实在是能(ji)力(shu)有(cha)限(jin),仅仅能从Linux应用开发開始,由浅入深,逐步进步,登上人生高峰,因此,昨天搭建了开 ...

  5. ngui点击与场景点击判断

    注:NGUI 组件上加上 BoxCollider 并设置区域大小 public void OnMouseDown()    { if (UICamera.hoveredObject == null) ...

  6. 线程 (detach的作用)

      线程状态在一个线程的生存期内,可以在多种状态之间转换.不同操作系统可以实现不同的线程模型,定义许多不同的线程状态,每个状 态还可以包含多个子状态.但大体说来,如下几种状态是通用的:       就 ...

  7. Android设置定时执行执行一次任务

    private Handler handler = new Handler(){ public void handleMessage(Message msg) { super.handleMessag ...

  8. LoadRunner测试下载功能点脚本(方法一)

    性能需求:对系统某页面中,点击下载功能做并发测试,以获取在并发下载文件的情况下系统的性能指标. 备注:页面上点击下载时的文件可以是word.excel.pdf等. 问题1:录制完下载的场景后,发现脚本 ...

  9. WPF XAML之bing使用StringFormat(转)

    释义 BindingBase.StringFormat 属性 获取或设置一个字符串,该字符串指定如果绑定值显示为字符串,应如何设置该绑定的格式.        命名空间: System.Windows ...

  10. hdu Intelligent IME

    算法:字典树 题意:手机9键拼音:2:abc  3:def 4:ghi 5:jkl 6:mno 7:pqrs 8:tuv 9:wxyz: 第一行读入一个T,代表测试组数: 之后输入两个整数n和m, 有 ...