UVA

来源:互联网 发布:淘宝联盟能用电脑吗 编辑:程序博客网 时间:2024/05/01 04:10

点击打开题目链接

N friends go to the local super market together. The probability of their buying something from themarket is p1, p2, p3, . . . , pN respectively. After their marketing is finished you are given the informationthat exactly r of them has bought something and others have bought nothing. Given this informationyou will have to find their individual buying probability.

Input

The input file contains at most 50 sets of inputs. The description of each set is given below:First line of each set contains two integers N (1 ≤ N ≤ 20) and r (0 ≤ r ≤ N). Meaning of N andr are given in the problem statement. Each of the next N lines contains one floating-point number pi(0.1 < pi < 1) which actually denotes the buying probability of the i-th friend. All probability valuesshould have at most two digits after the decimal point.Input is terminated by a case where the value of N and r is zero. This case should not be processes.

Output

For each line of input produce N +1 lines of output. First line contains the serial of output. Each of thenext N lines contains a floating-point number which denotes the buying probability of the i-th friendgiven that exactly r has bought something. These values should have six digits after the decimal point.Follow the exact format shown in output for sample input. Small precision errors will be allowed. Forreasonable precision level use double precision floating-point numbers.

Sample Input

3 2

0.10

0.20

0.30

5 1

0.10

0.10

0.10

0.10

0.10

0 0

Sample Output

Case 1:

0.413043

0.739130

0.847826

Case 2:

0.200000

0.200000

0.200000

0.200000

0.200000


题目大意:

一共n个人,其中k个人购物。给出n及每个人可能购买的概率,求n人中k人购买情况下每个人的购买概率。

思路:

条件概率 P(A|B)/ P(B) ,A为某个人购买的概率,B为k个人买了东西。然后dfs枚举所有情况,概率累计相加即可

附上AC代码:

#include<iostream>#include<cstdio>using namespace std;int vis[25];double ans[25];double p[25];int N, K;void init(){    for(int i = 0; i < 25; i++){        vis[i] = 0;        ans[i] = 0;    }}void dfs(int cnt, int k){    if(k == K){        double tmp=1.0;        for(int i = 1; i <= N; i++){            if(vis[i]) tmp *= p[i];            else tmp *= (1 - p[i]);        }        ans[0] += tmp;        for(int i = 1; i <= N; i++){            if(vis[i]) ans[i] += tmp;        }    }    else{        for(int i = cnt + 1; i <= N; i++){            vis[i] = 1;            dfs(i, k + 1);            vis[i] = 0;        }    }}int main(){    int kase = 0;    ios :: sync_with_stdio(false);    while(cin >> N >> K){        init();        if(N == 0 && K == 0) break;        for(int i = 1; i <= N; i++){            cin >> p[i];        }        dfs(0, 0);        printf("Case %d:\n", ++kase);        for(int i = 1; i <= N; i++){            printf("%.6f\n", ans[i] / ans[0]);        }    }    return 0;}

原创粉丝点击