# uva11181 - 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.

# 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

给你每个人做一件事的概率，告诉你有几个人做了这件事，求每个人在这个条件下做这件事的概率。

哎~~概率论~P(A|B) = P(AB)/P(B)

B就是N个人里面有r人做这件事。P（B）怎么算？因为N不大，用DFS来。DFS(int cur,int r,double pi),cur是当前该判断第cur个人的状态，r是当前已经选了r个人，pi是当前已经确定的选择的概率。注意不能到r==R就返回，因为前面选到了R后面就不能再选，要到cur>N时判断r是否等于R。

#include<cstring>
#include<cstdio>
#include<iostream>
#include<climits>
#include<cmath>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
int N,R,cas=0;
double a[25],p[25];
double DFS(int cur,int r,double pi){
if(cur>N) return r==R?pi:0;
double s;
s=DFS(cur+1,r+1,pi*p[cur]);
a[cur]+=s;
s+=DFS(cur+1,r,pi*(1-p[cur]));
return s;
}
int main(){
freopen("in.txt","r",stdin);
while(scanf("%d%d",&N,&R),N||R){
int i;
for(i=1;i<=N;i++) scanf("%lf",&p[i]);
memset(a,0,sizeof(a));
double s=DFS(1,0,1);
printf("Case %d:\n",++cas);
for(i=1;i<=N;i++) printf("%.6lf\n",a[i]/s);
}
return 0;
}



• 本文已收录于以下专栏：

举报原因： 您举报文章：uva11181 - Probability|Given 条件概率 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)