[hdu5985]概率题的推导

题目描述

Bob has collected a lot of coins in different kinds. He wants to know which kind of coins is lucky. He finds out a lucky kind of coins by the following way. He tosses all the coins simultaneously, and then removes the coins that come up tails. He then tosses all the remaining coins and removes the coins that come up tails. He repeats the previous step until there is one kind of coins remaining or there are no coins remaining. If there is one kind of coins remaining, then this kind of coins is lucky. Given the number of coins and the probability that the coins come up heads after tossing for each kind, your task is to calculate the probability for each kind of coins that will be lucky.

算法思路

  1. 这一题在比赛的时候我想的太多了,切分的子问题太多反而导致求解变得十分困难,最后导致我们没有A下这一题,可以说责任在我。
  2. 思路的话十分简单,首先,我们先计算出到达第k步的时候硬币i死亡的概率
    kill[i][j]=(1p[i]j)num[i]

    我们就可以计算出到达第i步之后i存活的概率
    recv[i][j]=1kill[i][j]

    那么,我们就可以得到某一个硬币i成为lucky coins的概率
    ans[i]=j=1max(recv[i][j]recv[i][j+1)k=0,kinkill[k][j]

    这个max是如何确定呢,我们知道所有的概率都在0.4-0.6之间,而总的硬币的个数在100000之内,我们就可以计算收敛的速度了。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

#define MAXN 75
#define MAXM 15

int t,n;
int num[MAXM];
double p[MAXM];
double killed[MAXM][MAXN+5];
double recv[MAXM][MAXN+5];
double ans[MAXM];

void Solve()
{
    int i,j,k;

    for(i=0;i<MAXM;i++)
        ans[i] = 0.0;

    if(n==1){
        printf("%.6f\n",1.0);
        return;
    }

    for(i=0;i<n;i++){
        double tmp = p[i];
        for(j=1;j<=MAXN;j++){
            killed[i][j] = pow(1-tmp,num[i]);
            recv[i][j] = 1 - killed[i][j];
            tmp *= p[i];
        }
    }

    for(i=0;i<n;i++){
        for(j=1;j<MAXN;j++){
            double tmp = 1.0;
            for(k=0;k<n;k++){
                if(k!=i)
                    tmp *= killed[k][j];
            }
            ans[i] += (recv[i][j]-recv[i][j+1])*tmp;
        }
    }

    for(i=0;i<n;i++)
        printf("%.6f%c",ans[i],(i==n-1)?'\n':' ');

    return;
}

int main()
{
    freopen("input","r",stdin);
    int i;

    scanf("%d",&t);

    while(t--){
        scanf("%d",&n);

        for(i=0;i<n;i++)
            scanf("%d%lf",&num[i],&p[i]);

        Solve();
    }
    return 0;
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值