HDU - 4336:Card Collector(min-max容斥求期望)
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
题意:题面不好看,题意很简单,就是给定S个物品,然后每次取到物品i的概率为pi,∑pi<=1; 求把所有物品都至少取到一次的期望。
思路:有一个专门这样的算法,叫min-max容斥。 他解决问题的方式:假设有S个对象,求把所有东西都取到的期望,不直接求,而是通过求子集的期望,然后容斥得到结果。 T是S的子集,我们得到每个子集T的期望,然后乘上容斥系数,累加起来就是答案。 假设我们dfs得到了S的子集T,并且得到至少取到这个子集的一个的概率p,则其期望为1/p;

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
double p[maxn],ans; int N;
void dfs(int pos,double now,int opt)
{
if(pos==N+) {
if(opt>){
if(opt&) ans+=1.0/now;
else ans-=1.0/now;
}
return ;
}
dfs(pos+,now,opt);
dfs(pos+,now+p[pos],opt+);
}
int main()
{
while(~scanf("%d",&N)){
for(int i=;i<=N;i++) scanf("%lf",&p[i]);
ans=; dfs(,0.0,);
printf("%.4lf\n",ans);
}
return ;
}
HDU - 4336:Card Collector(min-max容斥求期望)的更多相关文章
- HDU 4336 Card Collector(状压 + 概率DP 期望)题解
题意:每包干脆面可能开出卡或者什么都没有,一共n种卡,每种卡每包爆率pi,问收齐n种卡的期望 思路:期望求解公式为:$E(x) = \sum_{i=1}^{k}pi * xi + (1 - \sum_ ...
- 【HDU4336】Card Collector(Min-Max容斥)
[HDU4336]Card Collector(Min-Max容斥) 题面 Vjudge 题解 原来似乎写过一种状压的做法,然后空间复杂度很不优秀. 今天来补一种神奇的方法. 给定集合\(S\),设\ ...
- HDU 4336 Card Collector 期望dp+状压
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Time Limit: 2000/1000 MS (Java/O ...
- HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)
题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...
- hdu 4336 Card Collector —— Min-Max 容斥
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4336 bzoj 4036 的简单版,Min-Max 容斥即可. 代码如下: #include<cst ...
- $HDU$ 4336 $Card\ Collector$ 概率$dp$/$Min-Max$容斥
正解:期望 解题报告: 传送门! 先放下题意,,,已知有总共有$n$张卡片,每次有$p_i$的概率抽到第$i$张卡,求买所有卡的期望次数 $umm$看到期望自然而然想$dp$? 再一看,哇,$n\le ...
- HDU 4336 Card Collector(容斥)
题意:要收集n种卡片,每种卡片能收集到的概率位pi,求收集完这n种卡片的期望.其中sigma{pi} <=1; 思路:容斥原理.就是一加一减,那么如何算期望呢.如果用二进制表示,0表示未收集到, ...
- 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】
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- string与位运算
1.String String a="abc"; 会在常量池中开辟一个空间,保存"abc" String b=new String("abc&q ...
- nethogs 按进程实时统计网络带宽利用率的工具
安装 sudo apt-get install nethogs 使用 sudo nethogs
- NSMutableString和NSString区别,及相互转换方法
NSString是一个不可变的字符串对象.这不是表示这个对象声明的变量的值不可变,而是表示它初始化以后,你不能改变该变量所分配的内存中的值,但你可以重新分配该变量所处的内存空间.而NSMutableS ...
- Dapper 条件语句(Where) 中参数使用
public static List<ECInput> GetECInputList(DateTime beginDate,DateTime endDate,string[] barcod ...
- 工作流引擎Activiti使用总结(转)
1.简单介工作流引擎与Activiti 对于工作流引擎的解释请参考百度百科:工作流引擎 1.1 我与工作流引擎 在第一家公司工作的时候主要任务就是开发OA系统,当然基本都是有工作流的支持,不过当时使用 ...
- Windows 10上强制Visual Studio 2017 以管理员身份运行
1. 打开VS的安装目录,找到devenv.exe,右键,选择“兼容性疑难解答”. 2. 选择“疑难解答程序” 3. 选择“该程序需要附加权限” 4. 确认用户帐户控制后,点击测试程序,不然这个对话框 ...
- nyoj20——有向无环图深搜模板
吝啬的国度 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市, ...
- Vlmcsd(KMS)激活服务器程序
1.下载vlmcsd程序 2-1.虚拟机版本: 新建Linux虚拟机,硬件仅保留内存(最小14MB,推荐16MB).处理器(1个1核心).软盘(指向floppy144.flp).网络适配器(桥接模式) ...
- Linux 忘记密码解决方法,Linux 远程登录
一.Linux 忘记密码解决方法 很多朋友经常会忘记Linux系统的root密码,linux系统忘记root密码的情况该怎么办呢?重新安装系统吗?当然不用!进入单用户模式更改一下root密码即可. 步 ...
- iptables(五)iptables匹配条件总结之二(常用扩展模块)
iprange扩展模块 之前我们已经总结过,在不使用任何扩展模块的情况下,使用-s选项或者-d选项即可匹配报文的源地址与目标地址,而且在指定IP地址时,可以同时指定多个IP地址,每个IP用" ...