概率论 --- 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。

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----第i个员工在这r个员工中。

事件B----从n中选出r个员工。

求的就是在B事件发生的情况下,A事件发生的概率。

Pb为从n个员工中选出r个员工的概率总和,也就是C(n,r)中选中的r个算的是pi,未选中的算(1-pi)。

Pa为第i个员工在选中的r个员工中的概率总和,这个可以在算上面那个的时候一并算出。

最后的答案就是Pa/Pb.

数据很小,直接用递归枚举所有的组合数就行。

Time complexity: O(n^2)

 

Source code: 

/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-05-17-21.37
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define  LL long long
#define  ULL unsigned long long
using namespace std;

int n,k;
double p[25],ans[25];
bool vis[25];
void dfs(int N,int K) // 从N~n中选K个数的全部组合
{
        if(!K)
        {
                double tmp=1;
                for(int i=1;i<=n;++i)
                        if(vis[i]) tmp*=p[i];
                        else tmp*=(1-p[i]);
                ans[0]+=tmp;
                for(int i=1;i<=n;++i)
                        if(vis[i]) ans[i]+=tmp;
        }
        else
        {
                for(int i=N;i<=n;++i)
                {
                        vis[i]=1;
                        dfs(i+1,K-1);//从i+1~n中再选K-1个数
                        vis[i]=0;
                }
        }
}

int main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        int Cas=1;
        while(cin>>n>>k,n+k)
        {
                for(int i=1;i<=n;++i) cin>>p[i];
                memset(vis,0,sizeof vis);
                memset(ans,0,sizeof ans);
                dfs(1,k);
                printf("Case %d:\n",Cas++);
                for(int i=1;i<=n;++i) printf("%.6lf\n",ans[i]/ans[0]);
        }
        return 0;
}
/*

*/
View Code

 

目录
相关文章
|
8月前
UVa10484 - Divisibility of Factors(数论)
UVa10484 - Divisibility of Factors(数论)
41 1
|
8月前
UVa11565 - Simple Equations
UVa11565 - Simple Equations
35 0
|
存储 C语言
2019CCPC秦皇岛HDU - 6736 F - Forest Program(dfs找环 组合数学)
2019CCPC秦皇岛HDU - 6736 F - Forest Program(dfs找环 组合数学)
75 0
|
Java 机器学习/深度学习
2017 Multi-University Training Contest - Team 1 1011&&HDU 6043 KazaQ&#39;s Socks【规律题,数学,水】
KazaQ's Socks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 338    Accepted Submission(s)...
1032 0
|
Java Go
组合数学 - 母函数的变形 --- hdu 1171:Big Event in HDU
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24002    Accepted Submissio...
1047 0
数论 - 组合数学 + 素数分解 --- hdu 2284 : Solve the puzzle, Save the world!
Solve the puzzle, Save the world! Problem Description In the popular TV series Heroes, there is a tagline "Save the cheerleader, Save the world!".
908 0