UVa 11181 Probability|Given

Problem G
Probability|Given
Input: 
Standard Input

Output: Standard Output

 

N friends go to the local super market together. The probability of their buying something from the market is respectively. After their marketing is finished you are given the information that exactly r of them has bought something and others have bought nothing. Given this information you 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 and r are given in the problem statement. Each of the next N lines contains one floating-point number  (0.1<<1) which actually denotes the buying probability of the i-th friend. All probability values should 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 the next N lines contains a floating-point number which denotes the buying probability of the i-th friend given 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. For reasonable precision level use double precision floating-point numbers.

 

Sample Input                             Output for Sample Input

3 2
0.10
0.20
0.30
5 1
0.10
0.10
0.10
0.10
0.10
0 0

Case 1:

0.413043

0.739130

0.847826

Case 2:

0.200000

0.200000

0.200000

0.200000

0.200000

 

条件概率,用dfs来实现所有情况的枚举。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<ctype.h>
#include<algorithm>
#include<string>
#define PI 3.14159265
#define maxn 160
#define INF 1<<25
typedef long long ll;
using namespace std;
int tot,tt;
double rec=1;
double num[25];
double fenmu;
int vv[25];
double ans[25];
int ii=0;
int dfs(int x,int cc)
{
    if(cc==tt)
    {
        rec=1;
        for(int i=0;i<tot;i++)
        {
            if(vv[i])
            rec*=num[i];
            else
            rec*=(1-num[i]);
        }
        for(int i=0;i<tot;i++)
        {
            if(vv[i]==1)
            ans[i]+=rec;
        }
        fenmu+=rec;
    }
    else
    for(int i=x;i<tot;i++)
    {
        if(vv[i]==0)
        {
            vv[i]=1;
            dfs(i,cc+1);
            vv[i]=0;
        }
    }
}
int main()
{
    int ttt=0;
    while(scanf("%d%d",&tot,&tt)&&!(tot==0&&tt==0))
    {
        ttt++;
        memset(ans,0,sizeof(ans));
        for(int i=0;i<tot;i++)
        scanf("%lf",&num[i]);
        memset(vv,0,sizeof(vv));
        fenmu=0;
        dfs(0,0);
        printf("Case %d:\n",ttt);
        for(int i=0;i<tot;i++)
        printf("%.6lf\n",ans[i]/fenmu);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值