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[]);
}
}

最新文章

  1. MySQL中的group_concat函数
  2. Redis 缓存过期(maxmemory) 配置/算法 详解
  3. eclipse下的反编译
  4. android系统架构解析
  5. 如何配置Eclipse+Tomcat 开发环境【转】
  6. poj 1318
  7. C++Primer 第十四章
  8. centos 运用ssh的rsa算法实现无密码登录
  9. bzoj 1912 : [Apio2010]patrol 巡逻 树的直径
  10. bash 变量使用技巧
  11. 自制mpls ldp实验
  12. UserView--第二种方式(避免第一种方式Set饱和),基于Spark算子的java代码实现
  13. Spring Boot+Spring Security:获取用户信息和session并发控制
  14. Python之旅Day4 闭包函数 模块及模块倒入
  15. Java 7 for Absolute Beginners/Java 7基础教程--代码纠错
  16. scrapy之管道
  17. java程序中默认整形值常量是什么类型的?如何区分不同类型的整型数值常量?
  18. 部署描述符 web.xml
  19. ceph mimic版本 部署安装
  20. CH4901 关押罪犯

热门文章

  1. ubuntu16.04 ssh服无法远程连接解决办法
  2. Linux内核锁与中断处理
  3. ieda常用快捷键
  4. [PIC32--IDE]使用MPLAB IDE调试
  5. node - 写返回mime类型
  6. Unix环境部署
  7. Android--Handler的用法:在子线程中更新界面
  8. iOS自动化构建 xcode-select: error: tool &#39;xcodebuild&#39; requires Xcode, but active developer directory &#39;/Library/D...
  9. HBase中我认为比较常用的两个类:Scan和Filter
  10. [C++]二维数组还是一维数组?