uva11181Probability|Given
枚举,条件概率。
2^20次方等于100w,是大约可以没准还能过的。
二进制枚举时,如果买东西的人恰好为r个,设概率为p,就将sum[i]+=p(sum[i]为r个人买东西时第i个人买东西的概率),tot+=p(tot为r个人买东西的概率)
要求的就是sum[i]/tot。
P(第i个人实际买东西)=P(r个人买东西且第i个人在其中)/P(r个人买东西)。
r为0时特判
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 20 + 10;
int n,r,N,cnt,kase;
double a[maxn],sum[maxn];
double tot,p; int main() {
kase=0;
while(scanf("%d%d",&n,&r)==2) {
printf("Case %d:\n",++kase);
if(!n&&!r) break;
for(int i=0;i<n;i++) scanf("%lf",&a[i]);
if(!r) {
for(int i=0;i<n;i++) printf("0.000000\n");
continue;
}
N=1<<n;
memset(sum,0,sizeof(sum)); tot=0;
for(int i=1;i<N;i++) {
cnt=0; p=1;
for(int j=0;j<n;j++) {
if(i&(1<<j)) {cnt++; p*=a[j];}
else p*=(1-a[j]);
}
if(cnt==r) {
for(int j=0;j<n;j++) if(i&(1<<j)) sum[j]+=p;
tot+=p;
}
}
for(int i=0;i<n;i++) printf("%.6lf\n",sum[i]/tot);
}
return 0;
}
uva11181Probability|Given的更多相关文章
- UVA11181Probability|Given(条件概率)
题目链接 紫书P327 题意:有n个人准备去超市逛,其中第i个人买东西的概率是 Pi .逛完以后你得知有 r 个人买了东西.根据这一信息,计算每个人实际买东西的概率.输入 n ( 1 <= n ...
随机推荐
- Windows下将txt导入MySQL及远程连接设置
1.修改字符编码,全部修改为gbk.这样修改,重启后又会恢复默认值. show variables like '%char%'; set character_set_database=gbk; 其中, ...
- Django 学习笔记之七 实现分页
接着上篇,在上篇的基础上实现网页数据分页显示 1.打开views.py,编辑如下 #coding:utf-8from django.shortcuts import render,get_object ...
- 合并2个dll成一个,好处你懂的
步骤一:先下载微软的工具 ilmerge.exe 地址:http://www.microsoft.com/en-us/download/details.aspx?id=17630 步骤二:安装好之后 ...
- 1452: [JSOI2009]Count - BZOJ
Description Input Output Sample Input Sample Output 1 2HINT 一开始还想什么离线做,其实不用,空间足够,我们直接开100个二维树状数组,然后就 ...
- Tern Server Timeout
- crud springmvc
实体类:Student.java package demo.entity; public class Student { private int id; private String name; pr ...
- Unity3D IOS IPhone添加Admob的方法
原地址:http://dong2008hong.blog.163.com/blog/static/4696882720140403119293/ 首先阅读官方文档https://developers. ...
- ZOJ 2724 Windows Message Queue (优先级队列,水题,自己动手写了个最小堆)
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
- .net web部署(IIS Express && Nancy Self-Hosting)
http://d.hatena.ne.jp/fkmt5/20140330/1396195246 [1]Nancy Web配置注意事项 添加url:netsh http add urlacl url=h ...
- poj 1730 Perfect Pth Powers
这个有2种方法. 一种是通过枚举p的值(p的范围是从1-32),这样不会超时,再就是注意下精度用1e-8就可以了,还有要注意负数的处理…… #include<iostream> #incl ...