# [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;
}

#### HDU-5985-Lucky Coins-2016青岛站D题-数学推导

2016-11-24 19:30:02

#### hdu5985 概率，推导

2017-10-09 14:13:17

#### HDU5985Lucky Coins 【数学题】

2017-02-21 20:10:01

#### HDU-5985-Lucky Coins（概率）

2017-03-24 16:13:01

#### HDU 5985 Lucky Coins（概率）

2017-10-14 22:20:45

#### HDU 3398 String

2016-09-10 10:03:38

#### Hdu 2407 Knots【概率】

2016-04-09 09:37:56

#### XTU 1233 Coins（DP）

2016-06-01 15:11:16

#### POJ 刷题顺序

2014-11-09 02:50:03

#### ZJCoj qwb has a lot of Coins ( 博弈

2017-06-05 00:57:34