UVA - 11181 Probability|Given(条件概率+dfs枚举组合)

点击打开题目链接

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chook_lxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值