[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.
算法思路
- 这一题在比赛的时候我想的太多了,切分的子问题太多反而导致求解变得十分困难,最后导致我们没有A下这一题,可以说责任在我。
- 思路的话十分简单,首先,我们先计算出到达第k步的时候硬币i死亡的概率
kill[i][j]=(1−p[i]j)num[i]
我们就可以计算出到达第i步之后i存活的概率recv[i][j]=1−kill[i][j]
那么,我们就可以得到某一个硬币i成为lucky coins的概率ans[i]=∑j=1max(recv[i][j]−recv[i][j+1)∗∏k=0,k≠inkill[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;}
- [hdu5985]概率题的推导
- hdu5985 概率,推导
- 全概率与条件概率的组合推导
- hdu 4652 经典概率题+公式推导
- 贝叶斯垃圾邮件分类问题中联合概率的推导
- 概率论 --- 对于全错位排列概率公式的证明推导
- sigmoid与条件概率的关系及推导过程
- hdu4089(公式推导)概率dp
- 马尔科夫链 吸收概率方程组推导
- 推荐系统之概率矩阵分解的详细推导过程(Probabilistic Matrix Factorization,PMF)
- 多变量条件概率等价式推导
- 全概率公式、贝叶斯公式推导过程
- 全概率公式、贝叶斯公式推导过程
- 全概率公式、贝叶斯公式推导过程
- 全概率公式、贝叶斯公式推导过程
- 全概率公式、贝叶斯公式推导过程
- 多变量条件概率等价式推导
- 全概率公式、贝叶斯公式推导过程
- 一道简单的趣味编程
- 个人学习安卓基础的案例逻辑-数据存储
- php-fpm多用户运行的两种方式
- Java的反射机制
- iOS中属性@property中的关键字的使用说明
- [hdu5985]概率题的推导
- PHPCMS插件开发教程
- JavaScript比较day4
- leetcode(43) - Multiply Strings
- 面向对象的风格和基于对象的风格程序设计
- Android开发之context
- 集合大聚会
- java 对象赋值
- stm32 固件库之:uart