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 ...
随机推荐
- Ubuntu重启网络/etc/init.d/networking restart报错
Linux版本:Ubuntu 12.04 配置网口后重启网络,提示/etc/init.d/networking restart is deprecated. $ sudo /etc/init.d/ne ...
- mac mysql安装
一.安装 1.下载软件包直接安装即可: http://rj.baidu.com/soft/detail/25675.html?ald 安装完成后root默认密码为空: 二.修改密码 直接修改密码会提示 ...
- SVN--下载、安装VisualSVN server 服务端和 TortoiseSVN客户端
前言: 在http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html的博客中已经很详细地介绍了SVN的服务器--VisualS ...
- sysconf和pathconf使用
问题描述: 查看系统运行时的限制值 问题解决: 执行效果: 源代码:
- 【BZOJ】【1385】【Baltic2000】Division expression
欧几里得算法 普通的求个gcd即可……思路题 因为要求尽量是整数……所以 $\frac{x_1}{x_2*x_3*x_4*....*x_n}$是最大的结果了,因为$x_2$必须为分母,$x_1$必须为 ...
- Enabled AWE
sp_configure RECONFIGURE GO sp_configure RECONFIGURE GO sp_configure RECONFIGURE GO sp_configure REC ...
- 利用vim阅读源代码一个好用的工具
阅读源代码时常常遇到找变量,函数定义的问题.vim为我们提供了一个好用的工具,ctags. 安装 ctags. 在 libvirt的源代码根目录运行 ctags -R . vim -t virConn ...
- [转载]Spring Bean Definition Inheritance
Following is the configuration file Beans.xml where we defined "helloWorld" bean which has ...
- zju 1037 Gridland(找规律,水题)
题目链接 多写几个案例,根据数据的奇偶性,就能找到规律了 #include<stdio.h> int main() { int t,n,m; double ans; scanf(" ...
- MYSQL日常操作命令再熟悉
1,创建用户及密码: CREATE USER 'user'@'%' IDENTIFIED BY 'password'; 2,创建数据库: create database PDB_chengang de ...