hdu 4336 Card Collector

 

 

Card Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1881 Accepted Submission(s): 872
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 //203MS    280K    586 B    C++
 2 //容斥原理的做法 
 3 #include<stdio.h>
 4 int main(void)
 5 {
 6     int n;
 7     double p[25];
 8     while(scanf("%d",&n)!=EOF)
 9     {
10         double ans=0;
11         for(int i=0;i<n;i++)
12             scanf("%lf",&p[i]);
13         for(int i=1;i<(1<<n);i++){ //枚举所有情况,0的时候不用考虑 
14             double temp=0;
15             int cnt=0;
16             for(int j=0;j<n;j++){
17                 if(i&1<<j){  //符合第i种情况 
18                     temp+=p[j];
19                     cnt++;
20                 }
21             }
22             if(cnt&1) ans+=1.0/temp; //奇加偶减 
23             else ans-=1.0/temp;
24         }
25         printf("%.4lf\n",ans);
26     }
27     return 0;
28 }      

 

 

 

状态压缩+概率DP做法参考: http://www.2cto.com/kf/201208/145664.html

 1 /*
 2 
 3 250MS    8508K    649 B    C++
 4 状态压缩+概率DP
 5 dp[i]表示(2进制数i上0为没收集到,1为收集到)差i收集完的次数 
 6 dp[i]=(p0+pi)*dp[i]+p[k1]*(dp[i^(1<<k1)])+..+1;
 7  
 8 */
 9 #include<stdio.h>
10 double dp[1<<21];
11 double p[21];
12 int main(void)
13 {
14     int n;
15     while(scanf("%d",&n)!=EOF)
16     {
17         double p0=1.0; //抽不到卡片的概率 
18         for(int i=0;i<n;i++){
19             scanf("%lf",&p[i]);
20             p0-=p[i];
21         }
22         dp[(1<<n)-1]=0; //全部收集到,不用再收集 
23         for(int i=(1<<n)-2;i>=0;i--){
24             double temp=0; //发生状态i中所有事件的概率 
25             double sum=1.0; //状态i中要收集的数量 
26             for(int j=0;j<n;j++){
27                 if(i&(1<<j)) temp+=p[j]; //如果事件j在状态i中 
28                 else sum+=p[j]*dp[i|(1<<j)]; //前一状态 
29             }
30             dp[i]=sum/(1-p0-temp); 
31         }
32         printf("%.4lf\n",dp[0]);
33     }
34     return 0;
35 } 

 

posted @ 2013-10-04 10:07  heaventouch  阅读(125)  评论(0编辑  收藏  举报