hdu4336 概率dp

来源:互联网 发布:律师 防身用具 知乎 编辑:程序博客网 时间:2024/06/05 21:13

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, 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
10.120.1 0.4
 

Sample Output
10.00010.500
 

/**hdu 4336 概率dp、状态压缩dp题目大意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率为p1,p2,````pN.           每包至多一张卡片,可能没有卡片。           求需要买多少包才能拿到所以的N张卡片,求次数的期望。解题思路:状态压缩求概率。           dp[(1<<n)-1]=0,dp[i]=(sum(dp[i|(1<<j)]*a[j])+1)/(sum(a[j]));j为所有j&(1<<i)==0,dp[0]即为所求*/#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;int n;double a[30],dp[(1<<20)+3];int main(){    while(~scanf("%d",&n))    {        double ans=1.0;        for(int i=0;i<n;i++)        {            scanf("%lf",&a[i]);            ans-=a[i];        }        int maxx=(1<<n)-1;        dp[maxx]=0.0;        for(int i=maxx-1;i>=0;i--)        {            double cnt=0,sum=1;            for(int j=0;j<n;j++)            {                if(i&(1<<j)) cnt+=a[j];                else   sum+=dp[i|(1<<j)]*a[j];            }            dp[i]=sum/(1-cnt-ans);        }        printf("%.5lf\n",dp[0]);    }    return 0;}


1 0