Card Collector

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) 

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5
 6 const int MAXN = (1<<20)+10;
 7 int n;
 8 double p[25];
 9 double dp[MAXN];
10
11 int main()
12 {
13     while(scanf("%d",&n)!=EOF)
14     {
15         for (int i=0;i<n;i++)
16             scanf("%lf",&p[i]);
17
18         int mmm = (1<<n)-1;     //二进制为 n 个 1
19         dp[mmm]=0;
20         for (int i=mmm-1;i>=0;i--)  //所有情况都遍历到
21         {
22             double all_p=0;
23             dp[i]=1;
24             for (int j=0;j<n;j++)
25             {
26                 if ( (i&(1<<j))==0 )    //第 j 种卡片没有
27                 {
28                     dp[i]+=p[j]*dp[i+(1<<j)];   //dp [i+(1<<j)] 肯定赋过值了
29                     all_p+=p[j];
30                 }
31             }
32             dp[i]/=all_p;
33         }
34         printf("%lf\n",dp[0]);
35     }
36 }

时间: 2024-08-24 16:38:57

Card Collector的相关文章

hdu 4336 Card Collector

Card Collector http://acm.hdu.edu.cn/showproblem.php?pid=4336 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Special Judge Problem Description In your childhood, do you crazy for collecting the beautiful cards in

HDU 4336:Card Collector 期望+状压

Card Collector 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意: 去商店里买零食,每包零食里最多有1张卡片,也有可能没有,问要集齐所有n(n≤20)种卡片所需要购买零食个数的期望. 题解: 设dp[i](二进制,对应位为1表示已经有该卡片)为以当前状态为起点还需要购买零食个数的期望,则dp[0]即答案 对DP求解期望有问题的可以看下这里 代码 #include<stdio.h>#include<string.h

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

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3407    Accepted Submission(s): 1665Special Judge Problem Description In your childhood, do you crazy for collecting the beautiful

hdu 4336 Card Collector (概率dp+位运算 求期望)

题目链接 Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2711    Accepted Submission(s): 1277Special Judge Problem Description In your childhood, do you crazy for collecting the beaut

HDU 4336 Card Collector(概率dp+状压)

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3014    Accepted Submission(s): 1445 Special Judge Problem Description In your childhood, do you crazy for collecting the beautifu

Card Collector(HDU 4336)

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3001    Accepted Submission(s): 1435Special Judge Problem Description In your childhood, do you crazy for collecting the beautiful

HDU 4336:Card Collector(容斥原理)

http://acm.split.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Special Judge Problem Description 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

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

Card Collector Problem Description 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

hdoj 4336 Card Collector 【概率dp】

题目:hdoj 4336 Card Collector 题意:集齐卡片抽大奖,每个卡片概率,及其卡片个数,然后问你及其卡片要买卡片数量的期望. 分析:最多20张卡片,用状态压缩来表示是否拿了某个卡片. 比如现在有状态10010,表示拿了第2 3 5的状态下的期望. 我们要求它,我们可以先得到11010,10110,10011,的期望,然后乘以各自位没拿的概率就是总的期望.在除去总概率就是当前的期望, 不是很懂啊///数学智商有点捉急 AC代码: #include <cstdio> #inclu

HDU-4336 Card Collector(状压概率DP||容斥原理)

Card Collector http://acm.hdu.edu.cn/showproblem.php?pid=4336 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Special Judge Problem Description In your childhood, do you crazy for collecting the beautiful cards in