http://acm.split.hdu.edu.cn/showproblem.php?pid=4336

Card Collector

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张卡,吃一包方便面得到第i张卡的概率为p[i],问收集N张卡吃的方便面包数的期望。
思路:容斥原理。奇数加偶数减。
 
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; double p[];
double dp[]; int main()
{
int n;
while(~scanf("%d", &n)) {
for(int i = ; i < n; i++)
scanf("%lf", &p[i]);
double ans = ;
double sum;
int cnt;
for(int i = ; i < ( << n); i++) {
sum = ; cnt = ;
for(int j = ; j < n; j++) {
if(i & ( << j)) {
cnt++;
sum += p[j];
}
}
if(cnt & ) ans += 1.0 / sum;
else ans -= 1.0 / sum;
}
printf("%f\n", ans);
}
return ;
}

HDU 4336:Card Collector(容斥原理)的更多相关文章

  1. hdu 4336 Card Collector 容斥原理

    读完题目就知道要使用容斥原理做! 下面用的是二进制实现的容斥原理,详见:http://www.cnblogs.com/xin-hua/p/3213050.html 代码如下: #include< ...

  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 (概率dp+位运算 求期望)

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

  6. HDU 4336 Card Collector 数学期望(容斥原理)

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意简单,直接用容斥原理即可 AC代码: #include <iostream> ...

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

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

  8. hdu 4336 Card Collector——最值反演

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4336 点集中最早出现的元素的期望是 min ,最晚出现的元素的期望是 max :全部出现的期望就是最晚出现 ...

  9. hdu 4336 Card Collector —— Min-Max 容斥

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4336 bzoj 4036 的简单版,Min-Max 容斥即可. 代码如下: #include<cst ...

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

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

随机推荐

  1. [转载]WGS84坐标与Web墨卡托坐标互转

    //经纬度转Wev墨卡托 dvec3 CMathEngine::lonLat2WebMercator(dvec3 lonLat) { dvec3 mercator; ; +lonLat.y)*PI/) ...

  2. Struts2上传图片时报404错误

    可能是struts配置文件中定义的拦截器导致的,后缀拦截导致,将该拦截器去掉,在action类里判断后缀 public String upload()throws Exception{ ActionC ...

  3. .NET业务实体类验证组件Fluent Validation

    认识Fluent Vaidation. 看到NopCommerce项目中用到这个组建是如此的简单,将数据验证从业务实体类中分离出来,真是一个天才的想法,后来才知道这个东西是一个开源的轻量级验证组建. ...

  4. NSCoding归档

    大家都知道ios中数据持久化的方式有plist sqlite coredata nscoding 而nscoding不需要关心模型属性有多少个,是什么类型,不需要定义归档文件的规则. 下面给个类大家用 ...

  5. JAVA的JVM虚拟机工作原理.V.1.0.0

    注意:一下内容纯属个人理解,如有错误,欢迎批评指正. (90度弯腰)谢谢. java在JVM上的运行过程: 1,编辑好的java代码(IDE无报错,测试运行无错误): 2,java源代码通过javac ...

  6. 背包九讲 附:USACO中的背包问题

    附:USACO中的背包问题 USACO是USA Computing Olympiad的简称,它组织了很多面向全球的计算机竞赛活动. USACO Trainng是一个很适合初学者的题库,我认为它的特色是 ...

  7. SVN空格问题的解决方法

    (注意:如果你的路径里有空格记得要在binpath的头尾用转义字符/"把整个个binpath框起来,D:\SVN\svnroot指svn资源库根目录) displayname指服务名称 de ...

  8. .NET: WPF Binding对数据的校验和转换以及多路Binding

    一.校验 一般需要对target上的值进行校验. xaml: <Window x:Class="WpfApplication1.MainWindow" xmlns=" ...

  9. 控件ListView

    ListView的简单用法,先在布局文件中添加ListView控件: 接下来修改MainActivity中的代码: 由上面的代码可以知道,数据是无法直接传递给ListView的,需要借助适配器来完成. ...

  10. C++新手之培养良好的编程风格

    内功深厚的武林高手出招往往平淡无奇.同理,编程高手也不会用奇门怪招写程序.良好的编程风格是产生高质量程序的前提. 下面以C++为例,来给大家介绍. 一. 命名约定 有不少人编程时用拼音给函数或变量命名 ...