HDU4336 Card Collector 【容斥原理】【数学期望】

来源:互联网 发布:知乎 红楼梦 编辑:程序博客网 时间:2024/06/04 18:10

 Card Collector


 
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
题意:方便面里都可以吃出卡片,吃出每种卡片的概率已给出,问你集齐一套卡片的期望
思路:这个只要会算期望了,基本解决,弱的我连期望也不会了,概率学的圈还回去了
给出一组样例解释,一点即懂,类似于gcd那个题目,Goal=每张卡片的期望-两张卡片的期望+三张……
每张卡片的期望就等于1/p[i],
以样例二为例,集齐单张卡片的期望  temp[1|1]=1/0.1=10  temp[1|2]=1/0.4=2.5
集齐两张temp[2|1,2]=1/(0.1+0.4)=2 
ans=temp[1]-temp[2]=12.5-2=10.5
3 0.1 0.2 0.3
temp[1|1]=10 temp[1|2]=5 temp[1|3]=3.333
temp[2|1,2]=3.333 temp[2|1,3]=2.5  temp[2|2,3]=2
tenp[3|1,2,3]=1.6666
10+5-3.333+3.333-2.5-2+1.66=12.166
#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>#include<algorithm>using namespace std;#define ll long long#define MOD 1000000007#define N 110double p[N];int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        double sum=0;        for(int i=0;i<n;i++)        {            scanf("%lf",&p[i]);            sum+=p[i];        }        double ans=0;        for(int i=1;i<(1<<n);i++)        {            int cnt=0;            double sum=0;            for(int j=0;j<n;j++)              if(i&(1<<j))              {                  sum+=p[j];                  cnt++;              }            if(cnt&1)ans+=1.0/sum;            else ans-=1.0/sum;        }        printf("%.5lf\n",ans);    }    return 0;}


原创粉丝点击