uva 11181 Probability|Given

原题:
N friends go to the local super market together. The probability of their buying something from the market is p1,p2,p3,…,pNrespectively. 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 pi(0.1 < pi< 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
3 2
0.10
0.20
0.30
5 1
0.10
0.10
0.10
0.10
0.10
0 0
Sample Output
Case 1:
0.413043
0.739130
0.847826
Case 2:
0.200000
0.200000
0.200000
0.200000
0.200000
大意:
n个人去超市购物,其中有r个人买东西了。现在给你每个人去超市可能买东西的话的概率,问你在如果有r个人买东西,这r个人里面有第i个人的概率是多少?(i属于n)

#include <bits/stdc++.h>
using namespace std;
//fstream in,out;
double p[21];
bool vis[21];
double res[21],tot;
int n,r;
void dfs(int k,int c)
{
    if(c==r)
    {
        double tmp=1;
        for(int i=1;i<=n;i++)
            if(vis[i])
                tmp*=p[i];
            else
                tmp*=(1-p[i]);
        tot+=tmp;
        for(int i=1;i<=n;i++)
            if(vis[i])
                res[i]+=tmp;
    }
    else
    {
        for(int j=k+1;j<=n;j++)
        {
            if(!vis[j])
            {
                vis[j]=true;
                dfs(j,c+1);
                vis[j]=false;
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int k=0;
    while(cin>>n>>r,n+r)
    {
        memset(res,0,sizeof(res));
        memset(p,0,sizeof(p));
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
            cin>>p[i];
        tot=0;
        dfs(0,0);
        cout<<"Case "<<++k<<":"<<endl;
        for(int i=1;i<=n;i++)
            cout<<fixed<<setprecision(6)<<res[i]/tot<<endl;
    }
    return 0;
}

解答:
先算出n个人里面有r个人去购物的概率tot。然后算出第i个人确定在这r个人里面的概率res[i]。
结果就是res[i]/tot。虽然知道了公式怎么列,可是代码没想出来怎么写,刚开始算的时候寻思用生成组合数的方法去标记,结果自己试了一个C(20,10)的数据,跑了n久。肯定是超时,后来看了不少人的解答方式,发现了两种解法,一种如上的dfs。因为每个res[i]的值都是tot里面的一个子概率,所以在搜索res[i]的时候每次都把res[i]累加到tot上面即可。
另外一种方法是用动态规划,由于算出公式就没往动态规划上面考虑,浪费了一个好题目。
设置dp[i][j]表示前i个人中选j个人去购物,那么就有第i个人去购物和不去购物两种状态。
那么dp[i][j]=dp[i-1][j-1]p[i]+dp[i-1][j](1-p[i]),这样遍历i从1到n,j从1到r时tot值。
计算每个人的概率也是同时,不过要把第i个人的去掉,变成计算p1,p2,….pi-1,pi+1,…pn的dp值,比较麻烦。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值