UVA 11181 dfs 概率

N friends go to the local super market together. The probability of their buying something from the
market is p1, p2, p3, . . . , pN 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 pi
(0.1 < pi < 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.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个人买东西的概率p[i] ,   问你恰好有R个人买了东西,第i个人买东西的概率

题解: N的范围小  20

         暴力搜索所有情况就好了

   注意答案是  ans[i]/p   p为n个人与r个人买了东西的概率

//meek///#include<bits/stdc++.h>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<iostream>
#include<bitset>
using namespace std ;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
typedef long long ll;

const int N = 200;
const int M = 1000001;
const int inf = 0x3f3f3f3f;
const int MOD = 100003;
const double eps = 0.000001;

double ans[N],p[N];
int n;
double dfs(int cnt,int r,double pi) {
    if(cnt > n)
        if(r) return 0;
        else return pi;
    double sum = 0;
    if(r) {
        sum += dfs(cnt+1,r-1,pi*p[cnt]);
        ans[cnt] += sum;
    }
    sum += dfs(cnt+1,r,pi*(1-p[cnt]));
    return sum;
}
int main() {
    int cas = 1,r;
    while(~scanf("%d%d",&n,&r)) {
        if(n==0&&r==0) break;
        for(int i=1;i<=n;i++) scanf("%lf",&p[i]);
        mem(ans);
        printf("Case %d:\n", cas++);
        double p = dfs(1,r,1);
        for(int i=1;i<=n;i++)
            printf("%.6f\n", ans[i]/p);
    }
    return 0;
}
代码

 

posted @ 2015-12-25 13:26  meekyan  阅读(179)  评论(0编辑  收藏  举报