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. Java6 WebService的发布

    Java6 WebService的发布   WebService服务发布往往比较混乱,Axis2的发布形式与XFire发布方式差别很大,而Java6 Web服务的发布与Axis2.XFire的Web服 ...

  2. 2017.6.26 接口测试工具postman使用总结

    参考来自: http://www.cnblogs.com/sunshine-sky66/p/6369963.html http://www.cnplugins.com/tool/specify-pos ...

  3. 2017.5.15 markdown简明教程

    0.说明 markdown是一种书写格式,html是一种发布格式.markdown的语法种类只对应html标记的一小部分(只涵盖纯文本). 不在markdown涵盖范围的标签,都可以直接在文档里用ht ...

  4. 不依赖Excel是否安装的Excel导入导出类

    本文利用第三方开源库NPOI实现Excel97-2003,Excel2007+的数据导入导出操作. 不依赖Office是否安装.NPOI开源项目地址:http://npoi.codeplex.com/ ...

  5. Python列表切成多个/生成多个空列表

    li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18] #arr是被分割的list,n是每个chunk中含n元素. def chunks(arr, n) ...

  6. Android Studio Gradle项目中加入JNI so文件

    首先在Android Studio(版本号1.2.2)project的app文件夹下创建整个jni文件夹,jni文件夹里写Android.mk.Application.mk以及各类C/C++和汇编源文 ...

  7. 【Excle】在重复数据中对日期排序并查询最新的一条记录

    现在存在以下数据: 需要查询出以下数据 姓名       日期 张三       2017-12-14 李四       2017-12-16 在E1中写入以下公式:=IF(D2=MAX(IF($C$ ...

  8. oc 经常用到弹出view的方法

    #pragma mark 弹出view -(void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur { CAKeyframeA ...

  9. 浅谈BloomFilter【上】基本概念和实现原理

        在日常生活中.包括在设计计算机软件时,我们常常要推断一个元素是否在一个集合中.     比方在字处理软件中,须要检查一个英语单词是否拼写正确(也就是要推断 它是否在已知的字典中).在 FBI. ...

  10. Httpclient 实现带参文件上传

    这里直接贴出的是我封装好的doPostFile方法,httpclient 的版本是3.1. public static String doPostFile(String url, Part[] par ...