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

xiaoxiao2021-02-27  419

Probability|Given

  UVA - 11181  #include <iostream> #include <bits/stdc++.h> using namespace std; const int N = 500; const int inf = 0x3f3f3f3f; const int maxn=1e6+2; typedef long long LL; double p[N], vis[N], ans, a[N]; int n, m; void dfs(int pos,int cnt,double sum) { if(cnt>m||pos-cnt-1>n-m) return ; if(pos==n+1) { for(int i=1;i<=n;i++) if(vis[i]) p[i]+=sum; ans+=sum; return ; } vis[pos]=0; dfs(pos+1,cnt,sum*(1-a[pos])); vis[pos]=1; dfs(pos+1,cnt+1,sum*a[pos]); return ; } int main() { int t, ncase=1; while(scanf("%d %d", &n, &m),n||m) { printf("Case %d:\n",ncase++); for(int i=1;i<=n;i++) scanf("%lf", &a[i]); memset(p,0,sizeof(p)); ans=0; dfs(1,0,1.0); for(int i=1;i<=n;i++) { printf("%.6f\n",p[i]/ans); } } return 0; }
转载请注明原文地址: https://www.6miu.com/read-2642.html

最新回复(0)