HDU 4336 Card Collector 概率dp 状态压缩| 容斥原理

来源:互联网 发布:苏沉船 王晶 知乎 编辑:程序博客网 时间:2024/06/06 17:46

Card Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2994    Accepted Submission(s): 1431
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
10.120.1 0.4
 

Sample Output
10.00010.500
 

Source
2012 Multi-University Training Contest 4 


概率dp:

/** Author: ☆·aosaki(*’(OO)’*)  niconiconi★ **///#pragma comment(linker, "/STACK:1024000000,1024000000")//#include<bits/stdc++.h>#include <iostream>#include <sstream>#include <cstdio>#include <cstring>#include <algorithm>#include <functional>#include <cmath>#include <vector>#include <queue>#include <map>#include <set>#include <list>#include <stack>//#include <tuple>#define mem(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define lp(k,a) for(int k=1;k<=a;k++)#define lp0(k,a) for(int k=0;k<a;k++)#define lpn(k,n,a) for(int k=n;k<=a;k++)#define lpd(k,n,a) for(int k=n;k>=a;k--)#define sc(a) scanf("%d",&a)#define sc2(a,b) scanf("%d %d",&a,&b)#define lowbit(x) (x&(-x))#define ll long long#define pi pair<int,int>#define vi vector<int>#define PI acos(-1.0)#define pb(a) push_back(a)#define mp(a,b) make_pair(a,b)#define TT cout<<"*****"<<endl;#define TTT cout<<"********"<<endl;inline int gcd(int a,int b){    return a==0?b:gcd(b%a,a);}#define INF 1e9#define eps 1e-8#define mod 10007#define MAX 10010using namespace std;double p[22];double dp[1<<22];int main(){    //freopen("in.txt","r",stdin);    int n;    while(~sc(n))    {        double tt=0;        lp0(i,n)        {            scanf("%lf",&p[i]);            tt+=p[i];        }        tt=1-tt;        dp[(1<<n)-1]=0;        for(int i=(1<<n)-2;i>=0;i--)        {            double x=0,sum=1;            lp0(j,n)            {                if((i&(1<<j)))x+=p[j];                else sum+=p[j]*dp[i|(1<<j)];            }            dp[i]=sum/(1-tt-x);        }        printf("%.5lf\n",dp[0]);    }    return 0;}


容斥原理:

公式=w=



/** Author: ☆·aosaki(*’(OO)’*)  niconiconi★ **///#pragma comment(linker, "/STACK:1024000000,1024000000")//#include<bits/stdc++.h>#include <iostream>#include <sstream>#include <cstdio>#include <cstring>#include <algorithm>#include <functional>#include <cmath>#include <vector>#include <queue>#include <map>#include <set>#include <list>#include <stack>//#include <tuple>#define mem(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define lp(k,a) for(int k=1;k<=a;k++)#define lp0(k,a) for(int k=0;k<a;k++)#define lpn(k,n,a) for(int k=n;k<=a;k++)#define lpd(k,n,a) for(int k=n;k>=a;k--)#define sc(a) scanf("%d",&a)#define sc2(a,b) scanf("%d %d",&a,&b)#define lowbit(x) (x&(-x))#define ll long long#define pi pair<int,int>#define vi vector<int>#define PI acos(-1.0)#define pb(a) push_back(a)#define mp(a,b) make_pair(a,b)#define TT cout<<"*****"<<endl;#define TTT cout<<"********"<<endl;inline int gcd(int a,int b){    return a==0?b:gcd(b%a,a);}#define INF 1e9#define eps 1e-8#define mod 10007#define MAX 10010using namespace std;double p[22];int main(){    //freopen("in.txt","r",stdin);    int n;    while(scanf("%d",&n)==1)    {        lp0(i,n)            scanf("%lf",&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;}



0 0
原创粉丝点击