11181 - Probability|Given

N friends go to the local super market together. The probability of their buying something from the
market is p 1 ,p 2 ,p 3 ,...,p N respectively. After their marketing is finished you are given the information
that exactly r of them has bought something and others have bought nothing. Given this information
you will have to find their individual buying probability.
Input
The input file contains at most 50 sets of inputs. The description of each set is given below:
First line of each set contains two integers N (1 ≤ N ≤ 20) and r (0 ≤ r ≤ N). Meaning of N and
r are given in the problem statement. Each of the next N lines contains one floating-point number p i
(0.1 < p i < 1) which actually denotes the buying probability of the i-th friend. All probability values
should have at most two digits after the decimal point.
Input is terminated by a case where the value of N and r is zero. This case should not be processes.
Output
For each line of input produce N +1 lines of output. First line contains the serial of output. Each of the
next N lines contains a floating-point number which denotes the buying probability of the i-th friend
given that exactly r has bought something. These values should have six digits after the decimal point.
Follow the exact format shown in output for sample input. Small precision errors will be allowed. For
reasonable precision level use double precision floating-point numbers.
Sample Input

3 2
0.10
0.20
0.30
5 1
0.10
0.10
0.10
0.10
0.10
0 0
Sample Output

Case 1:
0.413043
0.739130
0.847826
Case 2:
0.200000
0.200000
0.200000
0.200000
0.200000

题意:

有 n 个人准备去逛超市,其中第 i 人购物的概率是 Pi ,逛完以后得知有 r 人买了东西,根据这一信息,计算每人买了东西的实际概率。

x[i] 表示 第i个人是否买了东西 1买了0没买

然后用 next_permutation();

把所有的情况 表示出来

在暴力求解了。。。条件概率,贝叶斯公式

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
double  r[21];
double sum[21];
int x[21];
double  tot;
int n,m;
void init(){
    memset(r,0,sizeof(r));
    memset(sum,0,sizeof(sum));
    memset(x,1,sizeof(x));
    for(int i=0;i<n-m;i++){
        x[i]=0;
    }
    tot=0;
    for(int i=0;i<n;i++){
        cin>>r[i];
    }
}
int main(){
    int k=1;double temp=0.0;
    while(cin>>n>>m&&n+m){
        init();
        do{
            temp=1;
            for(int i=0;i<n;i++){
                if(x[i])temp*=r[i];
                else temp*=1-r[i];
            }
            tot+=temp;
            for(int i=0;i<n;i++){
                if(x[i])sum[i]+=temp;
            }
        }while(next_permutation(x,x+n));
        cout<<"Case "<<k++<<":"<<endl;
        for(int i=0;i<n;i++){
           printf("%f\n",sum[i]/tot);
        }
    }
}

时间: 2024-11-05 15:57:12

11181 - Probability|Given的相关文章

uva 11181 - Probability|Given

条件概率公式:P( A|B ) = P( AB ) / P( B ) 表示在事件B发生的前提下,事件A发生的概率: 对本道题: 设事件E:r个人买了东西: 事件Ei:第i个人买了东西: 则要求的是P( Ei | E ); 计算P( E ) 用全概率公式即可,采用递归枚举出所有r个人买东西的情况,然后计算出其总的概率: 计算P( Ei ) 就是在上面递归枚举的过程中将选上第i个人的情况的概率加起来:(在这种情况下,其概率就是:在E发生的前提下的概率) 代码: #include<cstdio> #

概率论 --- Uva 11181 Probability|Given

Uva 11181 Probability|Given Problem's Link:   http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18546 Mean: n个人去逛超市,第i个人会购买东西的概率是Pi.出超市以后发现有r个人买了东西,问你每个人购买东西的实际概率是多少. analyse: 转换模型: 有n个员工,每个员工被选出来的概率是Pi.最后选出了r个,问你第i个员工在这r个中的概率是多少. 设: 事件A---

【UVA】11181 - Probability|Given(条件概率)

一道条件概率题,数学烂真的伤不起,一开始都不知道怎么求条件概率. P(e) = p(e|E)/p(E). 用e出现的情况的概率,除以所有情况出现的概率,递归枚举每个人是否买东西了. 14026058 11181 Probability|Given Accepted C++ 0.102 2014-08-12 08:25:51 效率可能有点差. #include<cstdio> #include<cstring> #include<iostream> #include<

Uva 11181 Probability|Given(概率dp)

Problem G Probability|Given Input: Standard Input Output: Standard Output N friends go to the local super market together. The probability of their buying something from the market is respectively. After their marketing is finished you are given the

UVa 11181 Probability|Given (条件概率 &amp; 深度优先搜索)

题目 题目大意 有\(n\)个人准备去超市逛, 其中第\(i\)个人买东西的概率是\(P_i\).逛完以后你得知有\(r\)个人买了东西.根据这一信息, 请计算出每个人实际买了东西的概率.输入\(n\)(\(1 ≤ n ≤ 20\))和 \(r\)(\(0 ≤ r ≤ n\)), 输出每个人实际买了东西的概率. 题解 用DFS枚举每一种可能的情况, sum[n]表示总概率, sum[i]表示第\(i\)个人买了东西的概率之和, 则答案为sum[i]/sum[n]. 代码 #include <cs

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

题意:有n个人,已知每个人买东西的概率,求在已知r个人买了东西的条件下每个人买东西的概率. 分析:二进制枚举个数为r的子集,按定义求即可. #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator

Uva - 11181 Probability|Given (条件概率)

设事件B为一共有r个人买了东西,设事件Ai为第i个人买了东西. 那么这个题目实际上就是求P(Ai|B),而P(Ai|B)=P(AiB)/P(B),其中P(AiB)表示事件Ai与事件B同时发生的概率,同时总状态并不多,因此我们可以枚举买东西的状态预处理出P(AiB)和P(B),再代入计算即可. 枚举就是一般的dfs,关键是明白这个过程. 1 #include <cstdio> 2 #include <cstring> 3 int n,r; 4 double p[25],b[25],s

UVA 11181 Probability|Given 数学 条件概率

题目链接: https://vjudge.net/problem/UVA-11181 题目描述: 有n个人, 已知每个人买东西的概率, 现在n个人中一共有r个人买了东西, 问每个人实际买东西的概率是多少 解题思路: 一开始我设Xi为第i个人实际买东西的概率, X1 + X2 + ...... + Xn == r, 又已知P1*X1 + P2*X2 + ...... + Pn*Xn == 1 求每一个Xi, 我又犯蠢了, 我竟然想这样求出来每一个Xi......如果这道题Xi为已知求Pi就能求了,

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes