hdu 4336 Card Collector(期望 dp 状态压缩)
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.
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 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 ^-.
0.1 0.1 0.4
10.000
10.500
题目大意
买东西集齐全套卡片赢大奖。每个包装袋里面最多一张卡片,最少可以没有。且给了每种卡片出现的概率 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 状态压缩)的更多相关文章
- HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)
题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...
- HDU 4336 Card Collector 期望dp+状压
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Time Limit: 2000/1000 MS (Java/O ...
- hdu4336 Card Collector(概率DP,状态压缩)
In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...
- $HDU$ 4336 $Card\ Collector$ 概率$dp$/$Min-Max$容斥
正解:期望 解题报告: 传送门! 先放下题意,,,已知有总共有$n$张卡片,每次有$p_i$的概率抽到第$i$张卡,求买所有卡的期望次数 $umm$看到期望自然而然想$dp$? 再一看,哇,$n\le ...
- 【bzoj1076】[SCOI2008]奖励关 期望dp+状态压缩dp
题目描述 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝物以后也不能再 ...
- HDU 1074 Doing Homework (dp+状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...
- hdu 4336 Card Collector
dp+状态压缩 #include<cstdio> using namespace std; ]; <<]; int main() { int n; while(scanf(&q ...
- [HDU 4336] Card Collector (状态压缩概率dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题目大意:有n种卡片,需要吃零食收集,打开零食,出现第i种卡片的概率是p[i],也有可能不出现卡 ...
- HDU 4336 Card Collector:状压 + 期望dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意: 有n种卡片(n <= 20). 对于每一包方便面,里面有卡片i的概率为p[i],可 ...
随机推荐
- java.lang.NoSuchFieldError: deferredExpression解决
java.lang.NoSuchFieldError: deferredExpression这个问题的出现是在的lib下面有多个版本的jstl.jar包,解决办法很简单,只留下一个版本的jstl ...
- core_cm3文件函数一览
core_cm3是ARM公司推出来的统一规定,这是对下游芯片厂商的统一规定,因此可以再Cortex-M3(CM3)之间进行移植.此文件中定义了一些对特殊功能寄存器的C语言形式的操作,本质上是内敛汇编和 ...
- [Javascript] How to use JavaScript's String.replace
In JavaScript, you can change the content of a string using the replace method. This method signatur ...
- Chapter 4: Spring and AOP:Spring's AOP Framework -- draft
Spring's AOP Framework Let's begin by looking at Spring's own AOP framework - a proxy-based framewor ...
- Hacker(19)----检测Windows系统漏洞
想完全掌握Windows中存在的漏洞需要使用专业的漏洞扫描软件.目前常用的有MBSA(MircosoftBaselineSecurityAnalyzer).360安全卫士等. 一.使用MBSA检测系统 ...
- LDAP禁止匿名访问
LDAP默认是允许用户匿名访问的,如下图:在使用工具连接时,勾选匿名绑定后,不需要输入UserDN和密码就可能连接到LDAP服务器,但是只能进行read及search操作.不能做任何的修改及删除操作. ...
- extern C的作用详解
extern "C"的主要作用就是为了能够正确实现C++代码调用其他C语言代码.加上extern "C"后,会指示编译器这部分代码按C语言的进行编译,而不是C+ ...
- 使用hbase的api创建表时出现的异常
/usr/lib/jvm/java-7-openjdk-amd64/bin/java -Didea.launcher.port=7538 -Didea.launcher.bin.path=/usr/l ...
- scala中的implict
1.作为隐式参数 object Test { def main(args: Array[String]) { import FruitColor._ Fruit.get("apple&quo ...
- Freemarker日期函数处理【转】
Freemarker日期函数处理[转] (2012-08-01 14:32:13) 转载▼ 标签: 杂谈 string(当和一个日期值一起使用) 这个内置标签用指定的格式把日期转换成字符串,(把默 ...