uva11181 Probability|Given

137 篇文章 0 订阅

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 nished 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 nd their
individual buying probability. Input The input le 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 oating-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 oating-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
oating-point numbers.

设恰有m个人买东西为E,第i个人买了东西为Ei。
题目所求P(Ei|E)=P(EiE)/P(E)。
P(E)可以通过一遍dfs枚举求得,还可以顺便在dfs中记录每个人选了商品没有,累加进P(EiE)。

#include<cstdio>
double p[25],tot,sum[25];
int n,m;
void dfs(double now,int k,int cnt,int con)
{
    if (k==n+1)
    {
        tot+=now;
        for (int i=1;i<=n;i++)
          if (con&(1<<i)) sum[i]+=now;
        return;
    }
    if (n-k>=m-cnt) dfs(now*(1-p[k]),k+1,cnt,con);
    if (cnt<m) dfs(now*p[k],k+1,cnt+1,con^(1<<k)); 
}
int main()
{
    int i,j,k,T=0;
    double x,y,z;
    while (scanf("%d%d",&n,&m)&&n)
    {
        for (i=1;i<=n;i++)
          sum[i]=0;
        tot=0;
        for (i=1;i<=n;i++)
          scanf("%lf",&p[i]);
        dfs(1,1,0,0);
        printf("Case %d:\n",++T);
        for (i=1;i<=n;i++)
          printf("%.6f\n",sum[i]/tot);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值