[hdu5985]概率题的推导

来源:互联网 发布:无经验淘宝客服简历 编辑:程序博客网 时间:2024/06/10 19:02

题目描述

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 15int 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;}
0 0
原创粉丝点击