Card Collector
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.
InputThe 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.OutputOutput 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 种卡片,买一包零食最多有一张,可能没有,然后给出每种卡片的出现概率。想要每种都收集至少一张,问需要买的零食包数期望
题解:用dp做,用数字的二进制来表示状态,例如,4 种卡片的话 1110 表示 2-4 卡片至少有一种,第 1 种没有的情况
那么: dp[i] = SUM( pk * dp[i+k] ) + ( 1 - SUM(pk) ) * dp[i] + 1 (设 k 指的是缺少的卡片,pk 是获得这种卡片的概率)
即 dp[i] = ( SUM( pk * dp[i+k] ) + 1 ) / SUM(pk)
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; const int MAXN = (<<)+;
int n;
double p[];
double dp[MAXN]; int main()
{
while(scanf("%d",&n)!=EOF)
{
for (int i=;i<n;i++)
scanf("%lf",&p[i]); int mmm = (<<n)-; //二进制为 n 个 1
dp[mmm]=;
for (int i=mmm-;i>=;i--) //所有情况都遍历到
{
double all_p=;
dp[i]=;
for (int j=;j<n;j++)
{
if ( (i&(<<j))== ) //第 j 种卡片没有
{
dp[i]+=p[j]*dp[i+(<<j)]; //dp [i+(1<<j)] 肯定赋过值了
all_p+=p[j];
}
}
dp[i]/=all_p;
}
printf("%lf\n",dp[]);
}
}
Card Collector的更多相关文章
- HDOJ 4336 Card Collector
容斥原理+状压 Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 4336:Card Collector(容斥原理)
http://acm.split.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Special Judge Problem Descriptio ...
- Card Collector(HDU 4336)
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu4336 Card Collector 状态压缩dp
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 4336 Card Collector(动态规划-概率DP)
Card Collector Problem Description In your childhood, do you crazy for collecting the beautiful card ...
- 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(Min-Max容斥)
[HDU4336]Card Collector(Min-Max容斥) 题面 Vjudge 题解 原来似乎写过一种状压的做法,然后空间复杂度很不优秀. 今天来补一种神奇的方法. 给定集合\(S\),设\ ...
- 【HDU4336】Card Collector (动态规划,数学期望)
[HDU4336]Card Collector (动态规划,数学期望) 题面 Vjudge 题解 设\(f[i]\)表示状态\(i\)到达目标状态的期望 \(f[i]=(\sum f[j]*p[j]+ ...
- HDU 4336——Card Collector——————【概率dp】
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- [HDU4336]Card Collector(min-max容斥,最值反演)
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- 安装php扩展模块参数memcache和memcached在php中的应用
一, memcache和memcached的区别与关系统php要想去访问memcached就得需要memcache扩展,这个道理和php连接mysql一样. 你不安装memcache扩展就没法识别me ...
- 一个人的安全部之ELK接收Paloalto日志并用钉钉告警
起因 通报漏洞后,开发未能及时修复漏洞,导致被攻击,领导说我发现被攻击的时间晚了,由于一个人安全部精力有限未能及时看IPS告警,于是做了个钉钉告警. 本人环境介绍 ubuntu 14.04 pytho ...
- django前后端数据传输学习记录
在开发过程中会遇到这样的情况 后台返回了一堆的数据,是一个列表 例如 datas = [{"a":1, "b":2}, {"c": 3,&q ...
- elasticsearch 基础性操作
1 基础概念 Elasticsearch是一个近实时的系统,从你写入数据到数据可以被检索到,一般会有1秒钟的延时.Elasticsearch是基于Lucene的,Lucene的读写是两个分开的句柄,往 ...
- Word文档打不开怎么办
目前一些主流的办公软件给大家日常工作带来了很大便利,比如:Microsoft Office或金山WPS!我们在愉快地使用它们的同时,多少也遇到了一些让人尴尬或头疼的问题,比如:精心制作的文档,突然打不 ...
- Jsp:useBean使用详解
<jsp:useBean>标签用来在jsp页面中创建一个Bean实例,定义语法如下: 一.<jsp:useBean>语法 <jsp:useBean id="id ...
- 【VBA】显示Excle内置对话框
点击上图中的"显示Excle内置对话框",显示效果如下: 源代码: Public Sub 显示Excel内置对话框() UserForm.Show End Sub 附件下载
- 通过mysql show processlist 命令检查mysql锁的方法
作者: 字体:[增加 减小] 类型:转载 时间:2010-03-07 show processlist 命令非常实用,有时候mysql经常跑到50%以上或更多,就需要用这个命令看哪个sql语句占用资源 ...
- Eclipse配色利器
1 http://eclipsecolorthemes.org/ 这是官网 2 安装后,window-preferences-general-appearance-color theme 即可找到多 ...
- SVN版本控制图标未显示或显示异常
TortoiseSVN下载的文件和文件夹如果缺失了那些花花绿绿的状态小图标,很容易逼死某些强迫症患者,更何况这些小图标用处多多 接下来我会逐步展示从常规到非常规的一系列解决方案(不包括重装重启这一类) ...