11181-Probability

不得不说这道题十分猥琐啊,递归求解,我RE了接近20次,最后发现还是数组开小了。

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<vector>
#include<string.h>
using namespace std;
int biao[250000],n,r,zhi = 0;
double p[250000],possi[250000];
int v[250000][25];
void dfs(int cur,int buy)
{
    if(cur == n)
    {
        if(buy != r) return;
        possi[zhi] = 1;
        for(int i = 0;i < n;i++)
        {
            if(biao[i]) v[zhi][i] = 1;
            else v[zhi][i] = 0;
        }
        for(int i = 0;i < n;i++)
        {
            if(biao[i])
                possi[zhi] *= p[i];
            else possi[zhi] *= (1 - p[i]);
        }
        zhi++;
        return;
    }
    biao[cur] = 1; //买
    dfs(cur + 1,buy + 1);
    biao[cur] = 0;  //不买
    dfs(cur + 1,buy);
}
int main()
{
//    freopen("out.txt","w",stdout);
    int kase = 0;
    while(cin>>n>>r)
    {
        if(!n && !r) break;
        printf("Case %d:\n",++kase);
        memset(v,0,sizeof(v));
        memset(p,0,sizeof(p));
        memset(possi,1,sizeof(possi));
        memset(biao,0,sizeof(biao));
        for(int i = 0;i < n;i++)
            cin>>p[i];
        dfs(0,0);
        double fenmu = 0;
        for(int i = 0;i < zhi;i++)
            fenmu += possi[i];
        for(int i = 1;i <= n;i++)  //看看哪个容器里有i,把i买了的概率求出来
        {
            double fenzi = 0;
            for(int j = 0;j < zhi;j++)
            {
                double temp = 1;int flag =0;
                if(v[j][i - 1])  //这种情况i买了
                {
                    flag = 1;
                    for(int k = 0;k < n;k++)
                    {
                        if(v[j][k]) temp *= p[k];
                        else temp *= (1 - p[k]);
                    }
                }
                if(flag) fenzi += temp;
            }
            printf("%.6lf\n",(double)fenzi*1.0/fenmu);
        }
    }
    return 0;
}

11181-Probability

时间: 2024-08-10 15:07:54

11181-Probability的相关文章

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<

11181 - Probability|Given

N friends go to the local super market together. The probability of their buying something from themarket is p 1 ,p 2 ,p 3 ,...,p N respectively. After their marketing is finished you are given the informationthat exactly r of them has bought somethi

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