您的位置:首页 > 其它

HDU 5985 概率问题

2017-09-07 22:06 176 查看

概率问题

题意:

​ 有n种硬币,现在有一个方法可以得出一个或者一种lucky硬币,所有硬币抛下然后去掉背面朝上的,继续抛直到只剩一种硬币或者没有硬币为止。问每一种硬币成为lucky的概率是多少。

思路:

​ 若想知道在某一步只剩下第i种硬币的话,需要知道其它硬币已经没有的概率。

定义:die[i][j]表示第i种硬币在第j次抛全部被去掉的概率,很容易得出(1−pk)cnt 其中p为每次抛掷正面朝上的概率, cnt为这一种硬币的数量,k为第k次抛掷。

定义:alive[i][j] 表示第i种硬币在第j抛掷之后仍然有正面朝上的概率,很明显为1−die[i][k] .

那么对于求出第i种硬币的答案,在k次抛掷和k+1次抛掷中其它种类已经全部被去掉,而第i中硬币存在的概率,了即(alive[i][k]−alive[i][k+1])∗temp ,其中temp为其它种类的消失的概率。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <algorithm>

using namespace std;

const int maxn = 1000005;

int n;
double die[maxn][111];
double alive[maxn][111];
double ans[maxn];

double Pow(double x,int n) {
double ans=1.0;
while(n) {
if(n&1) {
ans*=x;
}
x*=x;
n/=2;
}
return ans;
}

int main()
{
//    freopen("in.txt","r",stdin);

int t;
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
double p;
int cnt;
for(int i = 1;i <= n; i++) {
scanf("%d%lf",&cnt,&p);
double temp;
for(int j = 1;j <= 100; j++) {
temp = (1.0-Pow(p,j));
temp = Pow(temp,cnt);
die[i][j] = temp;
alive[i][j] = 1-temp;
}
}
if(n == 1) {
printf("1.000000\n");
continue;
}
memset(ans,0,sizeof(ans));
for(int i = 1;i <= n; i++) {
for(int k = 1;k <= 100; k++) {
double temp = 1;
for(int j = 1;j <= n; j++) {
if(i == j) continue;
temp *= die[j][k];
}
ans[i] += (alive[i][k]-alive[i][k+1])*temp;
}
}
printf("%.6f",ans[1]);
for(int i = 2;i <= n; i++) {
printf(" %.6f",ans[i]);
}
printf("\n");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  概率