uva-11181-条件概率

https://vjudge.net/problem/UVA-11181

    给出n个人可能买东西的概率pi,已知有r个人买了东西,问每个人可能买东西的概率是多少。

  设E:有r个人买了东西     Ei:第i个人买了东西

  则有ans[i]=P(Ei|E)= P(Ei*E) / P(E)

  对于P(E)我们可以枚举出所有有r个人购买的情况计算每种情况的概率然后累加,P(Ei*E)也能用这种方式得到,然后计算答案就好了。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<queue>
 4 #include<cstdio>
 5 #include<stack>
 6 #include<set>
 7 #include<map>
 8 #include<cmath>
 9 #include<ctime>
10 #include<time.h> 
11 #include<algorithm>
12 using namespace std;
13 #define mp make_pair
14 #define pb push_back
15 #define debug puts("debug")
16 #define LL long long 
17 #define pii pair<int,int>
18 #define eps 1e-12
19 int tot[1100000];
20 int main()
21 {
22     int n,r,i,j,k,cas=0;
23     double P[25];
24     double E,ans[25];
25     for(i=0;i<(1<<20);++i){
26         int tmp=0;
27         j=i;
28         while(j){
29             tot[i]+=(j&1);
30             j>>=1;
31         }
32     }
33     while(scanf("%d%d",&n,&r)==2&&(n||r)){
34         E=0;
35         memset(ans,0,sizeof(ans));
36         for(i=1;i<=n;++i) scanf("%lf",P+i);
37         int all=(1<<n);
38         for(i=0;i<all;++i){
39             if(tot[i]==r){
40             double tmp=1;
41                 for(j=0;j<n;++j){
42                     if(i&(1<<j)){
43                         tmp*=P[j+1];
44                     }
45                     else{
46                         tmp*=(1-P[j+1]);
47                     }
48                 }
49                 
50                 for(j=0;j<n;++j)
51                      if(i&(1<<j)) ans[j+1]+=tmp;
52                 E+=tmp;
53             }
54         }
55         printf("Case %d:\n",++cas);
56         for(i=1;i<=n;++i) printf("%.6f\n",ans[i]/E);
57     }
58     return 0; 
59 }

猜你喜欢

转载自www.cnblogs.com/zzqc/p/8976430.html
今日推荐