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],可 ...
随机推荐
- poj 2226 Muddy Fields(最小点覆盖+巧妙构图)
Description Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= ...
- hdu 4869 Turn the pokers(组合数+费马小定理)
Problem Description During summer vacation,Alice stay at home for a long time, with nothing to do. S ...
- GitHub上可能用到的开源
AGi18n :https://github.com/angelolloqui/AGi18n 可以简单地本地化你的iOS app,从代码和XIB文件中提取文本转化成可本地化的字符串,且不会改变XIB文 ...
- Android: 在WebView中获取网页源码
1. 使能javascript: ? 1 webView.getSettings().setJavaScriptEnabled(true); 2. 编写本地接口 ? 1 2 3 4 5 final c ...
- HDU 3697 Selecting courses(贪心)
题目链接:pid=3697" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=3697 Prob ...
- [Angular 2] Event in deep
This lesson talks about the benefits of using the parens-based (click) syntax so that Angular 2 can ...
- [Redux] Extracting Presentational Components -- AddTodo
The code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { ...
- [Redux] Extracting Presentational Components -- Todo, TodoList
Code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { todo ...
- oracle函数之replace
replace('将要更改的字符串','被替换掉的字符串','替换字符串'): ','****') from tmall_tcmessage; 输出为 '158****3367'
- Java 装箱、拆箱 包装器
先说java的基本数据类型.java基本数据类型:byte.short.int.long.float.double.char.boolean 基本数据类型的自动装箱(autoboxing).拆箱(un ...