UVa11181 Probability|Given

暴力算概率即可。

用这个式子:P(Ai|B)=P(AiB)/P(B)

不过。。貌似有递推的常数做法?

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 20 + 1;

int N, R;
double p[MAXN], a[MAXN];

inline int bitcount(int x)
{return x == 0 ? 0 : (x & 1) + bitcount(x >> 1);}

int main()
{
    int t = 0;
    while(cin>>N>>R, (N + R))
    {
        for(int i = 0; i < N; i++) scanf("%lf", &p[i]);
        memset(a, 0, sizeof(a));

        printf("Case %d:\n", ++t);
        if(R == 0) {
            for(int i = 0; i < N; i++) puts("0.000000");
            continue;
        }

        double rp = 0.0;
        for(int state = 0; state < (1 << N); state++)
        {
            int cnt = bitcount(state);
            if(cnt != R) continue;

            double tmp = 1.0;
            for(int i = 0; i < N; i++)
                if(state & (1 << i)) tmp *= p[i];
                else tmp *= (1 - p[i]);
            rp += tmp;
            for(int i = 0; i < N; i++) 
                if(state & (1 << i)) a[i] += tmp;
        }

        for(int i = 0; i < N; i++) printf("%.6lf\n", a[i] / rp);
    }
    return 0;
}

 

posted @ 2018-07-11 18:18  俺是小程  阅读(106)  评论(0编辑  收藏  举报