UVa11181 Probability|Given DFS+概率

来源:互联网 发布:java swing 表格 编辑:程序博客网 时间:2024/04/29 08:10
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 

3 2
0.10
0.20
0.30
5 1
0.100.100.100.100.100 0

Output for Sample Input
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 <map>#include <cmath>#include <algorithm>#include <cstring>#include <string>using namespace std;#define LL long longdouble a[25],sum[25];int visit[25];int n,m;void dfs(int i,int j){    if(j==0){        double temp=1;        for(int t=1;t<=n;t++){            if(visit[t]==1){                temp*=a[t];            }else{                temp*=(1-a[t]);            }        }        sum[0]+=temp;        for(int t=1;t<=n;t++){            if(visit[t]==1){                sum[t]+=temp;            }        }    }else{        for(int t=i+1;t<=n;t++){            visit[t]=1;            dfs(t,j-1);            visit[t]=0;        }    }}int main(){    int ca=1;    while(~scanf("%d%d",&n,&m),n||m){        for(int i=1;i<=n;i++){            scanf("%lf",&a[i]);        }        for(int i=0;i<25;i++){            sum[i]=0;            visit[i]=0;        }        dfs(0,m);        printf("Case %d:\n",ca++);        for(int i=1;i<=n;i++){            printf("%.6lf\n",sum[i]/sum[0]);        }    }    return 0;}


0 0
原创粉丝点击