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

Card Collector的更多相关文章

  1. HDOJ 4336 Card Collector

    容斥原理+状压 Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  2. HDU 4336:Card Collector(容斥原理)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Special Judge Problem Descriptio ...

  3. Card Collector(HDU 4336)

    Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. hdu4336 Card Collector 状态压缩dp

    Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  5. HDU 4336 Card Collector(动态规划-概率DP)

    Card Collector Problem Description In your childhood, do you crazy for collecting the beautiful card ...

  6. HDU 4336 Card Collector 期望dp+状压

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Time Limit: 2000/1000 MS (Java/O ...

  7. 【HDU4336】Card Collector(Min-Max容斥)

    [HDU4336]Card Collector(Min-Max容斥) 题面 Vjudge 题解 原来似乎写过一种状压的做法,然后空间复杂度很不优秀. 今天来补一种神奇的方法. 给定集合\(S\),设\ ...

  8. 【HDU4336】Card Collector (动态规划,数学期望)

    [HDU4336]Card Collector (动态规划,数学期望) 题面 Vjudge 题解 设\(f[i]\)表示状态\(i\)到达目标状态的期望 \(f[i]=(\sum f[j]*p[j]+ ...

  9. HDU 4336——Card Collector——————【概率dp】

    Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  10. [HDU4336]Card Collector(min-max容斥,最值反演)

    Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. 代理Delegation

    package com.ctl.test; class Person { private int id; private String name; public int getId() { retur ...

  2. OpenGL.Vertex Array Object (VAO) 【转】

    http://www.cppblog.com/init/archive/2012/02/21/166098.html 一 OpenGL抛弃glEnable(),glColor(),glVertex() ...

  3. 使用nvDXT.exe把图片转换成dds图片【转】

    从nvidia官网下载工具包DDS Utilities [https://developer.nvidia.com/legacy-texture-tools] 转换图片格式需要的工具是 nvdxt.e ...

  4. Snapdragon connect to android devices

    怎么都连不上,连不上连不上... 用adb devices是列出来的,开发者选项也设置了, 后来查了下 把adb的路径拖到系统环境变量里就可以了.终于连上了,今天不用加班了...

  5. 前端打包利器:webpack工具

    一.什么是WebPack,为什么要使用它? 1.为什要使用WebPack 现今的很多网页其实可以看做是功能丰富的应用,它们拥有着复杂的JavaScript代码和一大堆依赖包.为了简化开发的复杂度,前端 ...

  6. 设置textField的placegolder的字体大小和字体颜色

           由于项目的主题颜色为灰黑色,所以当使用textField的时候,placeholder内的字体默认是灰色,当程序执行的时候,差点儿看不到.        翻来翻去找到一种比較简单地方法, ...

  7. hive删除表报错

    metastore.RetryingHMSHandler: HMSHandler Fatal error: javax.jdo.JDODataStoreException: You have an e ...

  8. 2017.3.31 spring mvc教程(五)Action的单元测试

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  9. Warning: isMounted(...) is deprecated in plain JavaScript React classes.

    1.错误提示 警告:isMounted(…)在纯 react.js 类中被弃用. 2.原因解析 出现此错误提示的原因是源代码内有已被React舍弃的代码,但此并不影响程序运行. 在index.js 内 ...

  10. 浅谈struts2标签中的2个非经常常使用的标签的使用方法(radio和select)

    1.如图所看到的我们须要在前台的页面通过radio和select将相应的数据库中的数据显示到选项其中,这也是我们做项目中常常须要做的,动态的显示,而不是静态的显示. 首先我们须要在页面中导入strut ...