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

题意:题面不好看,题意很简单,就是给定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 ;
}

最新文章

  1. C#中的简单工厂和单例
  2. (转) The major advancements in Deep Learning in 2016
  3. SQL Server 错误日志收缩(ERRORLOG)
  4. struts2获取web元素(request、session、application)
  5. 【08_238】Product of Array Except Self
  6. 【iOS】Quartz2D基本图形
  7. HUST 1376 Random intersection
  8. 1.XML规范
  9. Layui的一点小理解(上)
  10. PCI_Making Recommendations
  11. JavaScript之Number、String、Array常用属性与方法手册
  12. SAP MM 销售订单库存与普通库存之间相互转换过账后对于EBEWH以及MBEWH表的更新
  13. 51Nod - 1384
  14. intelij idea常用功能介绍
  15. html 腳本
  16. dma传输数据长度,在启动前必须确保是一个大于0的数字
  17. html 经验之谈
  18. C语言--第三次作业
  19. idea的一些快捷键
  20. java的集合框架详解

热门文章

  1. eclipse中把多个项目放在一个文件夹里
  2. 关于Google play无法下载应用
  3. cassandra 之 在spark-shell 中使用 spark cassandra connector 完整案例
  4. JavaScript封装Ajax工具函数及jQuery中的ajax,xhr在IE的兼容
  5. 【Error】local variable &#39;xxx&#39; referenced before assignment
  6. Check for Palindromes
  7. C++进阶2. typedef用法
  8. 决定整理一下canvas的基础学习
  9. 如何让VS2013编写的程序在xp下运行
  10. APUE学习笔记——10.11~10.13 信号集、信号屏蔽字、未决信号