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 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  (0.1<<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                             Output for Sample Input

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

Case 1:

0.413043

0.739130

0.847826

Case 2:

0.200000

0.200000

0.200000

0.200000

0.200000


Problem-setter: Shahriar Manzoor

Special Thanks: Derek Kisman

题意:有n个人,并且知道每个人买东西的概率,现在已知有r个人买了东西,依次求第i个人买东西的概率

思路:ans[i]保存有r个人买了东西的情况下并且第i个人买了东西的概率,all代表n给人中第r个买了东西的概率

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
//typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 25

double ans[N];
double p[N];
int n,r;

double dfs(int pos,int left,double pp)
{
    if(pos>n) return left==0 ? pp : 0;

    double sum=0;

    if(left)
	{
	    sum+=dfs(pos+1,left-1,pp*p[pos]);
	    ans[pos]+=sum;
	}
    sum+=dfs(pos+1,left,pp*(1-p[pos]));
    return sum;
}

int main()
{
	int i,j,ca=0;

    while(sff(n,r),n+r)
	{
		fre(i,1,n+1)
		  scanf("%lf",&p[i]);

		memset(ans,0,sizeof(ans));

	   double all=dfs(1,r,1);
	   pf("Case %d:\n",++ca);
	   for(i=1;i<=n;i++)
			pf("%.6lf\n",ans[i]/all);
	}

  return 0;
}
时间: 2024-10-16 08:18:56

Uva 11181 Probability|Given(概率dp)的相关文章

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---

BZOJ 2318: Spoj4060 game with probability Problem( 概率dp )

概率dp... http://blog.csdn.net/Vmurder/article/details/46467899 ( from : [辗转山河弋流歌 by 空灰冰魂] ) 这个讲得很好 , 推推公式就可以 O( n ) , 但是 n 最大是99999999 , 怎么破....其实 n 很大时概率基本不动了...所以只需计算到某一个较大值时就可以停下来了... ----------------------------------------------------------------

UVA 10529 - Dumb Bones (概率dp)

题目描述 You are trying to set up a straight line of dominos, standing on end, to be pushed over later for your entertainment. (Sure, it seems pointless to set something up only to knock it down again, but you have some strange hobbies) The tricky thing

UVA 10529 Dumb Bones 概率dp 求期望

题目链接:点击打开链接 题意: 要在一条直线上摆多米诺骨牌. 输入n, l, r 要摆n张排,每次摆下去向左倒的概率是l, 向右倒的概率是r 可以采取最优策略,即可以中间放一段,然后左右两边放一段等,摆放顺序任意. 问:在最佳策略下要摆成n张牌的期望次数. 思路: 点击打开链接 #include <cstdio> #include <iostream> #include <cstring> #include <queue> #include <algo

UVA - 11346 Probability (概率)

Description G - Probability Time Limit: 1 sec Memory Limit: 16MB Consider rectangular coordinate system and point L(X,Y) which is randomly chosen among all points in the area A which is defined in the following manner: A = {(x,y) | x is from interval

UVA 1637 Double Patience 概率DP

Double Patience Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Double Patience is a single player game played with a standard 36-card deck. The cards are shuffled and laid down on a table in 9 pile

UVA 10529-Dumb Bones(概率dp)

题意: 给出放一个多米诺骨牌,向左向右倒的概率,求要放好n个骨牌,需要放置的骨牌的期望次数. 分析: 用到区间dp的思想,如果一个位置的左面右面骨牌都已放好,考虑,放中间的情况, dp[i]表示放好前i个骨牌,要放的期望次数,枚举1-i,每个点做中间点求对应的期望,取最小值. dp[i]=min(L*dp[l]+R*dp[r]+1/(1.0-L-R)); #include <map> #include <set> #include <list> #include <

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