HDU 4336 Card Collector 概率DP 好题

                    Card Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3141    Accepted Submission(s): 1512
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
 
 
 
 
题意:有n种卡片,你每次买一包零食,里面可能会有一张卡片或者没有卡片
给出里面出现每种卡片的概率
问:集齐这n种卡片需要买的零食的数量的期望值
 
状态压缩DP
 
因为n<=20,用一个数的二进制的0~n-1位表示一种状态,0表示还没有拿到第i种卡片,1表示拿到了。
dp[i]表示在i这种状态拿到所有卡片的期望
则所有卡片都拿到的状态是:all=(1<<n)-1
则dp[all]=0.0
目的:求出dp[0]
 
转移方程:
设j表示比i多拿到一种卡的状态,k表示多拿的这张卡是哪一种
则:
dp[i]=所有的(dp[j]+1.0)*p[k]+(1.0-所有的p[k]的和)*(dp[i]+1)
因为我在i这个状态,买了一包零食,如果里面没有卡片,或者里面的卡片是我已经拥有的卡片,那么我还是i这个状态
化简:dp[i]=所有的dp[j]*p[k]+所有的p[k]+(1.0-所有的p[k])+(1.0-所有的p[k])*dp[i]
     =[ ( 所有的dp[j]*p[k] ) + 1.0 ] / ( 所有的p[k]的和 )
对状态从all-1到0循环一遍就ok了
 
 
 1 #include<cstdio>
 2 #include<cstring>
 3 
 4 using namespace std;
 5 
 6 double p[23];
 7 double dp[1<<22];
 8 
 9 int main()
10 {
11     int n;
12     while(~scanf("%d",&n))
13     {
14         for(int i=0;i<n;i++)
15         {
16             scanf("%lf",&p[i]);
17         }
18 
19         dp[(1<<n)-1]=0.0;
20         for(int i=(1<<n)-2;i>=0;i--)
21         {
22             dp[i]=0.0;
23             double cnt=0.0;
24             for(int j=0;j<n;j++)
25             {
26                 if((i&1<<j)==0)
27                 {
28                     dp[i]+=dp[i|(1<<j)]*p[j];
29                     cnt+=p[j];
30                 }
31             }
32             dp[i]=(dp[i]+1.0)/cnt;
33         }
34         printf("%f\n",dp[0]);
35     }
36     return 0;
37 }
View Code

 

 

 

 
 
 

 

posted on 2015-07-27 15:44  _fukua  阅读(193)  评论(0编辑  收藏  举报