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. 第四篇 Replication:事务复制-订阅服务器

    本篇文章是SQL Server Replication系列的第四篇,详细内容请参考原文. 订阅服务器就是复制发布项目的所有变更将传送到的服务器.每一个发布需要至少一个订阅,但是一个发布可以有多个订阅. ...

  2. PHP导出CSV UTF-8转GBK不乱码的解决办法

    折腾了几次 搜索了好久 终于找到办法 /** * http://yige.org/php/ * @ string 需要转换的文字 * @ encoding 目标编码 **/ function conv ...

  3. [转]EntityFramework走马观花之CRUD(上)

    学习Entity Framework技术期间查阅的优秀文章,出于以后方便查阅的缘故,转载至Blog,可查阅原文:http://blog.csdn.net/bitfan/article/details/ ...

  4. IDEA文件编码修改

    上图标注 1 所示,IDE 的编码默认是 UTF-8,Project Encoding 虽然默认是 GBK,但是一般我都建议修改为 UTF-8.上图标注 2 所示,IntelliJ IDEA 可以对 ...

  5. 解决 Xamarin 拖拽Plain Text 于Layout上时 出现 “The layout could not be loaded:java.lang.System.arraycopy([CI[CII)V” 错误

    右键项目属性

  6. fzuoj Problem 2182 水题

    http://acm.fzu.edu.cn/problem.php?pid=2182 Problem 2182 水题 Accept: 188    Submit: 277Time Limit: 100 ...

  7. Java Web项目里开发获取上个页面连接地址的问题

    近期做的项目有个问题,就是需要获取上个页面连接地址,我用的IE浏览器,在用location.href连接到新地址的时候,在新地址页面用document.referrer的方法获取不到原地址,我测试了下 ...

  8. mysql 线程池 数据库连接池

    当客户端请求的数据量比较大的时候,使用线程池可以节约大量的系统资源,使得更多的CPU时间和内存可以高效地利用起来.而数据库连接池的使用则将大大提高程序运行效率,同时,我们可以通过其自身的管理机制来监视 ...

  9. eclipse的自动提示功能

    一般情况下按ALT+/即可提示,若想按任意字母都有提示,则可以打开eclipse的自动提示功能,打开或关闭该提示功能的步骤如下: 打开eclipse后一次点Window->Perferences ...

  10. C# 文件读取(一)

    1. 读写文件的步骤: 创建一个文件流 -- 创建相应的读写器 -- 执行读写操作 -- 关闭读写器 -- 关闭文件流 创建一个文件流:   FileStream objfs = new FileSt ...