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): 1435
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 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.

Input

The 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.

Output

Output 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

Source

2012 Multi-University Training Contest 4

Recommend

zhoujiaqi2010

求期望

方法一:状压

1.逆序枚举所有状态 d[i] 表示状态为i时收集完所有卡片的期望步数。

d[i] = 1 + ∑(d[i | (1 << j)] * p[j])(ps: 累加所有走一步会增加新一张卡片的期望步数) + (1 - t) * d[i](ps: t为增加一张新卡片的概率);

#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN (1 << 20)

double d[MAXN + 1];
double p[25];
int main()
{
    int n;
    while(~scanf("%d", &n))
    {
       memset(d, 0, sizeof(d));
       repu(i, 0, n) scanf("%lf", &p[i]);
       if((1 << n) - 1) d[(1 << n) - 1] = 0.0;
       double t;
       for(int i = (1 << n) - 2; i >= 0; i--)
       {
           d[i] += 1.0;
           t = 0.0;
           for(int j = 0; j < n; j++)
              if(!(i & (1 << j))) {
                d[i] += p[j] * d[i | (1 << j)];
                t += p[j];
              }
           d[i] /= t;
       }
       printf("%.4lf\n", d[0]);
    }

    return 0;
}

时间: 2024-10-17 20:11:36

Card Collector(HDU 4336)的相关文章

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(容斥原理)

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

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

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