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

题意:

n类卡片,买一包零食可能得到一张卡片,可能没有,问最后集齐n类卡片期望买几包零食

题解:

每一包零食,有3种可能

1、没有卡片

2、有 没有收集过的卡片

3、有 已经收集了的卡片

将卡片拥有情况状态压缩成s,p[j]表示没有收集过的卡片的概率

dp[s]=1+ 没有卡的概率*dp[s]+ 收集过的卡片的概率和*dp[s] +  Σ dp[(1<<j)|s]*p[j]

=1+ dp[s]*(没有卡的概率+收集过的卡片的概率和) +  Σ dp[(1<<j)|s]*p[j]

=1+ dp[s]*(1-Σ p[j])+  Σ dp[(1<<j)|s]*p[j]

移项,dp[s]-dp[s]*(1-Σ p[j])= 1+ Σ dp[(1<<j)|s]*p[j]

∴dp[s]* Σ p[j]=1+ Σ dp[(1<<j)|s]*p[j]

∴ dp[s]= (1+ Σ dp[(1<<j)|s]*p[j])/ Σ p[j]

#include<cstdio>
#include<cstring>
using namespace std;
int n;
double p[20],dp[1<<20];
double sum1,sum2;
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++) scanf("%lf",&p[i]);
        for(int i=(1<<n)-2;i>=0;i--)
        {
            sum1=sum2=0;
            for(int j=0;j<n;j++)
             if(!((1<<j)&i))
             {
                 sum1+=p[j];
                 sum2+=dp[(1<<j)|i]*p[j];
             }
            dp[i]=(sum2+1)/sum1;
        }
        printf("%.4lf\n",dp[0]);
    }
}
时间: 2024-07-30 13:49:07

hdu 4336 Card Collector的相关文章

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

HDU 4336 Card Collector(容斥原理 or 状压求期望dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 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,

hdu 4336 Card Collector(期望)

http://acm.hdu.edu.cn/showproblem.php?pid=4336 有N种卡片,每一袋零食里面最多有一张卡片,给出一袋零食里面每种卡片的概率,问平均要买多少袋零食能收集到所有的卡片. 状态压缩一下,共有1<<n-1个状态,设dp[sta]表示当前状态到目标状态平均买的零食数目,已知终态dp[1<<n-1] = 0,dp[sta]可由一下状态得到: 这一袋零食里没有卡片,概率为p(没有一张卡片的概率),状态转移到sta: 这一袋零食里面有卡片j,但是他已经拥

[HDU 4336] Card Collector (状态压缩概率dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题目大意:有n种卡片,需要吃零食收集,打开零食,出现第i种卡片的概率是p[i],也有可能不出现卡片.问你收集齐n种卡片,吃的期望零食数是多少? 状态压缩:f[mask],代表收集齐了mask,还需要吃的期望零食数. 打开包装,有3种情况,第一种:没有卡片,概率(1-sigma(p[i])) 第二种,在已知种类中:概率sigma(p[j]) 第三种,在未知种类中:p[k] 因此 f[mask]

hdu 4336 Card Collector——最值反演

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4336 点集中最早出现的元素的期望是 min ,最晚出现的元素的期望是 max :全部出现的期望就是最晚出现的元素的期望. #include<cstdio> #include<cstring> #include<algorithm> #define db double using namespace std; const int N=25,M=(1<<20)+5; i

hdu 4336 Card Collector(期望 dp 状态压缩)

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