Card Collector

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 108 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 (1 <= N <= 20), indicating the number of different cards you need the collect. The second line contains N numbers p1, p2, ..., pN, (p1 + p2 + ... + pN <= 1), 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 10^-4.
 
Sample Input
1
0.1
2
0.1 0.4
 
Sample Output
10.000
10.500
 
Source
 
Recommend
zhoujiaqi2010
 

题目大意:

有n个卡片,你如今买一包方便面,没包方便面出现当中一个卡片的概率为 p[i] 。问你集齐一套卡片须要的张数的数学期望。

解题思路:

概率DP,用位进制0表示这个卡片有了,1表示这个卡片还没有。那么 比如 “3” 用二进制表示 “1 1” 那么 数组 dp[3] 记录的就是 1号卡片和2号卡片都有的情况集齐一套卡片须要的张数的数学期望。

dp[sum]= ( 1+sum { dp[ sum + (1<<j )] *p[j] }   ) /sum{p[j] }

当中 ( i&(1<<j) )==0

解题代码:

#include <iostream>
#include <cstdio>
using namespace std; const int maxn=(1<<20)+10;
int n;
double dp[maxn];
double p[30]; void solve(){
int sum=(1<<n)-1;
dp[sum]=0;
for(int i=sum-1;i>=0;i--){
double tmp=0;
dp[i]=1;
for(int j=0;j<n;j++){
if( ( i&(1<<j) )==0 ){
dp[i]+=dp[i+(1<<j)]*p[j];
tmp+=p[j];
}
}
dp[i]/=tmp;
}
printf("%lf\n",dp[0]);
} int main(){
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++) scanf("%lf",&p[i]);
solve();
}
return 0;
}

HDU 4336 Card Collector(动态规划-概率DP)的更多相关文章

  1. HDU 4336——Card Collector——————【概率dp】

    Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. hdu 4336 Card Collector (概率dp+位运算 求期望)

    题目链接 Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意: 一共有n种卡片.每买一袋零食,有可能赠送一张卡片,也可能没有. 每一种卡片赠送的概率为p ...

  4. hdu 4336 Card Collector(状压dp/Min-Max反演)

    传送门 解题思路 第一种方法是状压\(dp\),设\(f(S)\)为状态\(S\)到取完的期望步数,那么\(f(S)\)可以被自己转移到,还可以被\(f(S|(1<<i))\)转移到,\( ...

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

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

  6. HDU 4050 wolf5x(动态规划-概率DP)

    wolf5x Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

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

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

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

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

  9. HDU 4336 Card Collector(状压 + 概率DP 期望)题解

    题意:每包干脆面可能开出卡或者什么都没有,一共n种卡,每种卡每包爆率pi,问收齐n种卡的期望 思路:期望求解公式为:$E(x) = \sum_{i=1}^{k}pi * xi + (1 - \sum_ ...

随机推荐

  1. (Problem 33)Digit canceling fractions

    The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplif ...

  2. JavaEE Tutorials (7) - 在会话bean中使用异步方法调用

    7.1异步方法调用88 7.1.1创建异步业务方法88 7.1.2从企业bean客户端调用异步方法897.2async示例应用90 7.2.1async—war模块的架构91 7.2.2运行async ...

  3. cmake 学习笔记(四)

    接前面的一二三,学习一下 CMakeCache.txt 相关的东西. CMakeCache.txt 可以将其想象成一个配置文件(在Unix环境下,我们可以认为它等价于传递给configure的参数). ...

  4. HTML高级选项卡(1)————表标签

    利用上述表格属性,能够简单的创建一个表格.并随意的分割行和列. <html> <head> <title>表格的应用</title> </head ...

  5. js面向对象继承

    前言 最近看到js面向对象这章节了,主要学习了原型和面向对象继承关系,为了梳理自己的知识逻辑,特此记录. js的面向对象 先说说我目前了解的js创建对象方法 1.写一个函数,然后通过new创建对象 2 ...

  6. JS+CSS打造三级折叠菜单,自动收缩其它级 js

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  7. 应用之间进行跳转,ComponentName的方式

    从应用A跳转到应用B, 关键代码如下: 有以下几个注意点: 1.ComponentName cn = new ComponentName("com.terry", "co ...

  8. PigCms 回复消息 "域名授权错误! 您使用的微信平台或源码为盗版"

    本文地址:http://duwei.cnblogs.com/ Pigcms 将自动回复的API 写死了, 这里提供一个可用的API 在 PigCms/Lib/Action/Home/Weixinact ...

  9. windows 下搭建 apache + php52 + postgreSQL7/8/9环境

    apache和php安装参考:[转]Windows7 64bit下配置Apache+PHP+MySQL 我这主要讲配置  apache 支持 postgresql9数据库: 1.将php5文件夹下的p ...

  10. MYSQL 备份用户权限

    MYSQL 备份用户权限 datadbblack 192.168.1.10 是 SELECT, RELOAD, SHOW DATABASES, LOCK TABLES  否 权限列表 1.Select ...