题目链接

Card Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2711    Accepted Submission(s): 1277
Special Judge

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

题意:

有N(1<=N<=20)张卡片,每包中含有这些卡片的概率为p1,p2,````pN.
每包至多一张卡片,可能没有卡片。
求需要买多少包才能拿到所以的N张卡片,求次数的期望。

分析:

n为20,2^20 = 1 048 576;

所以可以用每一位来表示这种卡片有没有存在,还是逆推。

逆推公式:

d[i] = 1.0 + d[i]*p2 + d[i | (1<<j)]*p[j];

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <algorithm>
#define LL __int64
const int maxn = (<<) + ;
using namespace std;
double d[maxn], p[]; int main()
{
int n, i, j;
double sum, tmp;
while(~scanf("%d", &n))
{
memset(d, , sizeof(d));
sum = ;
for(i = ; i < n; i++)
{
scanf("%lf", &p[i]);
sum += p[i];
}
tmp = 1.0-sum;
d[(<<n)-] = ;
for(i = (<<n)-; i >= ; i--)
{
double p2 = tmp, p3 = ;
for(j = ; j < n; j++)
{
if(i&(<<j))
p2 += p[j]; //p2表示没有抽到新的卡片的概率和
else
p3 += p[j] * d[i|(<<j)];
}
d[i] += (1.0+p3)/(1.0-p2);
}
printf("%.4lf\n", d[]);
}
return ;
}

hdu 4336 Card Collector (概率dp+位运算 求期望)的更多相关文章

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

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

  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. HDU 4336 Card Collector(动态规划-概率DP)

    Card Collector Problem Description In your childhood, do you crazy for collecting the beautiful card ...

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

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

  5. HDU - 4336:Card Collector(min-max容斥求期望)

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

  6. sgu 495. Kids and Prizes (简单概率dp 正推求期望)

    题目链接 495. Kids and Prizes Time limit per test: 0.25 second(s)Memory limit: 262144 kilobytes input: s ...

  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 期望)题解

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

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

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

随机推荐

  1. 常见 WEB 安全漏洞(转)

    SQL注入 成因:程序未对用户的输入的内容进行过滤,从而直接代入数据库查询,所以导致了sql 注入 漏洞 . 思路:在URL处可以通过 单引号 和 and 1=1 and 1=2 等语句进行手工测试s ...

  2. ast.literal_eval(jsonStr) json格式字符串转dict 2种方式

    json.loads(jsonStr, encoding='utf-8')import ast ast.literal_eval(jsonStr)

  3. keras: 在构建LSTM模型时,使用变长序列的方法

    众所周知,LSTM的一大优势就是其能够处理变长序列.而在使用keras搭建模型时,如果直接使用LSTM层作为网络输入的第一层,需要指定输入的大小.如果需要使用变长序列,那么,只需要在LSTM层前加一个 ...

  4. Delphi编写WebService体会

    源:Delphi编写WebService体会 Dispatch: 派遣,分派 Invoke: 调用 Invokable: 可调用接口 TReomtable: WebService中自定义类都是继承自该 ...

  5. STS、Eclipse报java.lang.OutOfMemoryError: PermGen space 内存溢出

    我使用的工具是STS, Eclipse同理: 打开如下界面: 左则选择项目启动使用的Tomcat-->在右侧面板Tab项中选择" Arguments":在VM argumen ...

  6. 对于glut和freeglut的一点比较和在VS2013上的配置问题

    先大概说一下glut.h和freeglut.h 首先要知道openGL是只提供绘图,不管窗口的,所以你需要给它一个绘图的区域(openGL能跨平台也与此有些关系) glut.h和freeglut.h都 ...

  7. BZOJ 1207 [HNOI2004]打鼹鼠:dp【类似最长上升子序列】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1207 题意: 有一个n*n的网格,接下来一段时间内会有m只鼹鼠出现. 第i只鼹鼠会在tim ...

  8. python学习笔记:第六天(流程控制语句)

    Python3 条件控制 1.if 语句 <1> 一般形式 Python中if语句的一般形式如下所示: if condition_1: statement_block_1 elif con ...

  9. openfire性能调优

    1. 参考 http://blog.csdn.net/foxisme2/article/details/7521139 http://blog.csdn.net/foxisme2/article/de ...

  10. C/C++协程的实现方式总结

    1.利用 C 语言的 setjmp 和 longjmp,函数中使用 static local 的变量来保存协程内部的数据. 函数原型:int setjmp(jmp_buf envbuf); void  ...