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-thfriend. 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
|
题意:n个小伙伴中有m个买了东西,告诉你每次小伙伴单独买东西的概率,问每个小伙伴买了东西的概率。
思路:设Ai为第i个小伙伴买了东西,B为小伙伴们恰好买了m个东西,P(A|B)=P(AB)/P(B)。通过dp来求前n个小伙伴中有m个小伙伴买东西的概率。
AC代码如下:
#include<cstdio>
#include<cstring>
using namespace std;
double dp[2][25],p[25];
int t,n,m;
int main()
{
int i,j,k,a,b,f;
double ret;
while(~scanf("%d%d",&n,&m))
{
if(n==0 && m==0)
break;
for(i=1;i<=n;i++)
scanf("%lf",&p[i]);
printf("Case %d:\n",++t);
if(m==0)
{
for(i=1;i<=n;i++)
printf("0.000000\n");
continue;
}
memset(dp,0,sizeof(dp));
dp[0][0]=1;
a=0;b=1;
for(i=1;i<=n;i++)
{
dp[b][0]=dp[a][0]*(1-p[i]);
for(j=1;j<=i;j++)
dp[b][j]=dp[a][j-1]*p[i]+dp[a][j]*(1-p[i]);
a^=1;b^=1;
}
ret=dp[a][m];
for(k=1;k<=n;k++)
{
memset(dp,0,sizeof(dp));
dp[0][0]=1;
a=0;b=1;
for(i=1;i<=n;i++)
if(i!=k)
{
dp[b][0]=dp[a][0]*(1-p[i]);
for(j=1;j<=i;j++)
dp[b][j]=dp[a][j-1]*p[i]+dp[a][j]*(1-p[i]);
a^=1;b^=1;
}
printf("%.6f\n",dp[a][m-1]*p[k]/ret);
}
}
}