标签:max str deque line using size space cstring 进制
题意:有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 (条件概率)
标签:max str deque line using size space cstring 进制
原文地址:http://www.cnblogs.com/tyty-Somnuspoppy/p/7241801.html