UVA - 11181 Probability|Given (条件概率)

Posted SomnusMistletoe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA - 11181 Probability|Given (条件概率)相关的知识,希望对你有一定的参考价值。

题意:有n个人,已知每个人买东西的概率,求在已知r个人买了东西的条件下每个人买东西的概率。

分析:二进制枚举个数为r的子集,按定义求即可。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 20 + 10;
const int MAXT = 10000 + 10;
using namespace std;
double p[MAXN];
bool vis[MAXN];
double ans[MAXN];
int N, r;
double solve(){
    double sum = 1;
    for(int i = 0; i < N; ++i){
        if(vis[i]) sum *= p[i];
        else sum *= (1 - p[i]);
    }
    for(int i = 0; i < N; ++i){
        if(vis[i]){
            ans[i] += sum;
        }
    }
    return sum;
}
int main(){
    int kase = 0;
    while(scanf("%d%d", &N, &r) == 2){
        if(!N && !r) return 0;
        memset(ans, 0, sizeof ans);
        for(int i = 0; i < N; ++i){
            scanf("%lf", &p[i]);
        }
        double sum = 0;
        for(int i = 0; i < (1 << N); ++i){
            memset(vis, false, sizeof vis);
            int cnt = 0;
            for(int j = 0; j < N; ++j){
                if(i & (1 << j)){
                    ++cnt;
                    vis[j] = true;
                }
            }
            if(cnt == r){
                sum += solve();
            }
        }
        printf("Case %d:\n", ++kase);
        for(int i = 0; i < N; ++i){
            printf("%.6f\n", ans[i] / sum);
        }
    }
    return 0;
}

  

以上是关于UVA - 11181 Probability|Given (条件概率)的主要内容,如果未能解决你的问题,请参考以下文章

uva 11181 Probability|Given

UVA11181Probability|Given(条件概率)

UVA 11181 Probability|Given 数学 条件概率

UVA 11181 Probability|Given

「UVA11181」 Probability|Given(概率

UVA - 11181 Probability|Given (条件概率)