HDU 5985/nowcoder 207D - Lucky Coins - [概率题]
题目链接:https://www.nowcoder.com/acm/contest/207/D
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5985
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
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.
输入描述:
The first line is the number of test cases. For each test case, the first line contains an integer k representing the number of kinds. Each of the following k lines describes a kind of coins, which contains an integer and a real number representing the number of coins and the probability that the coins come up heads after tossing. It is guaranteed that the number of kinds is no more than 10, the total number of coins is no more than 1000000, and the probabilities that the coins come up heads after tossing are between 0.4 and 0.6.
输出描述:
For each test case, output a line containing k real numbers with the precision of 6 digits, which are the probabilities of each kind of coins that will be lucky.
输入
3
1
1000000 0.5
2
1 0.4
1 0.6
3
2 0.4
2 0.5
2 0.6
输出
1.000000
0.210526 0.473684
0.124867 0.234823 0.420066
题意:
给出 $k$ 种硬币,每种硬币有 $n[i]$ 个硬币,且该种硬币在抛掷之后,正面朝上的概率为 $p[i]$,
现在Bob同时抛掷所有硬币,去掉所有背面朝上的之后,继续抛掷,反复如此直到没有硬币留下或者只有一种硬币留下,
若只有一种硬币留下,则认为这种硬币是幸运币,现在要求这 $k$ 种硬币成为幸运币的各自的概率。
题解:
假设第 $i$ 种硬币在第 $t$ 步之后已经全部死掉的概率为 $die[t][i]$,不妨根据二项分布公式计算,对于某一个硬币来说,$t$ 次实验互相独立,它正面朝上的事件发生概率为 $p_i$,则这枚硬币在 $t$ 次实验后已经死掉的概率为 $P\left( {X = 0,1,2, \cdots ,k - 1} \right) = 1 - P\left( {X = k} \right) = 1 - C_k^k {p_i}^k \left( {1 - p_i } \right)^0 = 1 - {p_i} ^k$,
由于第 $i$ 种硬币中的 $n_i$ 个硬币互不影响,所以这 $n_i$ 个硬币全部死掉的概率 $die[t][i] = (1 - {p_i}^k )^{n_i}$,
需要注意的是,第 $i$ 种硬币在第 $t$ 步之后已经全部死掉这个事件,是包含了 $n_i$ 枚硬币在小于 $t$ 步时就已经全死掉了的情况的;
我们记 $stay[t][i] = 1 - die[t][i]$,此处 $stay[t][i]$ 代表了第 $i$ 种硬币在第 $t$ 步之后还有存留的概率,
同样需要注意的是,第 $i$ 种硬币在第 $t$ 步之后还有存留这个事件,是包含了 $n_i$ 枚硬币在大于 $t$ 步后依旧还有存留的情况的;
那么,我们如果要求,第 $i$ 种硬币在第 $t$ 步之后还有存留,但是在第 $t+1$ 步时就全部死掉,的概率呢?
考虑所有第 $t$ 步后还活着的第 $i$ 种硬币,只有两个选择:在第 $t+1$ 步后生,在第 $t+1$ 步后死。如此一来,我们去掉在第 $t+1$ 步后依然活着的可能性,就能得到上面所求事件概率为 $stay[t][i] - stay[t+1][i]$,
即第 $i$ 种硬币在第 $t$ 步之后还有存留,但是在第 $t+1$ 步时就全部死掉的概率为$stay[t][i] - stay[t+1][i]$,
那么,我们求第 $i$ 种硬币恰好在第 $t$ 步成为幸运币的概率就是 $(stay[t][i] - stay[t+1][i]) \times \prod\limits_{j \ne i} {die[t][j]}$,
因此答案即为 $\sum\limits_{t = 1}^\infty {\left( {\left( {stay[t][i] - stay[t + 1][i]} \right)\prod\limits_{j \ne i} {die[t][j]} } \right)}$。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxk=; int k;
double die[][maxk],stay[][maxk];
double ans[maxk]; int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d",&k);
for(int i=;i<=k;i++)
{
int n; double p;
scanf("%d%lf",&n,&p);
for(int t=;t<=;t++)
{
die[t][i]=pow(-pow(p,t),n);
stay[t][i]=-die[t][i];
}
} if(k==) //特判
{
printf("1.000000\n");
continue;
} for(int i=;i<=k;i++)
{
ans[i]=;
for(int t=;t<=;t++)
{
double mul=;
for(int j=;j<=k;j++) if(i!=j) mul*=die[t][j];
ans[i]+=(stay[t][i]-stay[t+][i])*mul;
}
printf("%.6f%c",ans[i],i<k?' ':'\n');
}
}
}
HDU 5985/nowcoder 207D - Lucky Coins - [概率题]的更多相关文章
- HDU5985 Lucky Coins 概率dp
题意:给你N种硬币,每种硬币有Si个,有Pi 概率朝上,每次抛所有硬币抛起,所有反面的拿掉,问每种硬币成为最后的lucky硬币的概率. 题解:都知道是概率dp,但是模拟赛时思路非常模糊,很纠结,dp[ ...
- HDU.5985.Lucky Coins(概率DP)
题目链接 \(Description\) 有n(n<=10)种硬币,已知每种硬币的数量和它抛一次正面朝上的概率pi.进行如下过程:每次抛一次所有硬币,将正面朝下的硬币去掉.重复该过程直到只剩一种 ...
- HDU 2843 I Will Win(概率题?,怨念颇深,简单)
题目 真不想说什么,,,这神题真讨厌,,,多校的.. //又是一道神题... #include<stdio.h> #include<string.h> //最大公约数 int ...
- HDU 5985 Lucky Coins 数学
Lucky Coins 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5985 Description Bob has collected a lot ...
- HDU 5985 概率
n种硬币各有cnt[i]枚,每轮下其有p[i]概率保留,问各种硬币只有它存活到最后一轮的概率. 设k轮后i硬币存活概率$a[i][k]=(1-p^k_i)^{cnt[i]}$ 则最后只有第i种硬币存活 ...
- hdu 5676 ztr loves lucky numbers
题目链接:hdu 5676 一开始看题还以为和数位dp相关的,后来才发现是搜索题,我手算了下,所有的super lucky number(也就是只含数字4, 7且4, 7的数量相等的数)加起来也不过几 ...
- HDU 2096 小明A+B --- 水题
HDU 2096 /* HDU 2096 小明A+B --- 水题 */ #include <cstdio> int main() { #ifdef _LOCAL freopen(&quo ...
- HDU 1248 寒冰王座(全然背包:入门题)
HDU 1248 寒冰王座(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票 ...
- poj3519 Lucky Coins Sequence矩阵快速幂
Lucky Coins Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- excel表格 xls、xlsx 读取
public static void main(String[] args) throws Exception { // getdslContext(); String file = "F: ...
- MySql按周,按月,按日分组统计数据
知识关键词:DATE_FORMAT select DATE_FORMAT(create_time,'%Y%u') weeks,count(caseid) count from tc_case grou ...
- Android在开发中的使用技巧之解决ScrollView嵌套RecyclerView出现的系列问题
根据已上线的app里总结出来的实用小技巧 相信大家都遇到过ScrollView嵌套RecyclerView或者RecyclerView嵌套RecyclerView来使用, 也会遇到一堆奇奇怪怪的问题, ...
- SNF快速开发平台3.0之BS页面展示和九大优点-部分页面显示效果-Asp.net+MVC4.0+WebAPI+EasyUI+Knockout
一)经过多年的实践不断优化.精心维护.运行稳定.功能完善: 能经得起不同实施策略下客户的折腾,能满足各种情况下客户的复杂需求. 二)编码实现简单易懂.符合设计模式等理念: 上手快,见效快.方便维护,能 ...
- 空间谱专题13:联合解算DOA(ML/AP)
其中作者:桂. 时间:2017-10-16 07:51:40 链接:http://www.cnblogs.com/xingshansi/p/7675380.html 前言 主要记录二维测向中,分别利 ...
- Hexo NexT 博客后台管理指南
上篇文章讲到,将Hexo NexT 博客成功上传到GitHub 并绑定到我们自定义的域名下了. 但是却还是有个问题,那就是Hexo NexT 博客如何进行后台管理呢? 如果总是通过手动创建文件的方式, ...
- 【iCore1S 双核心板】DEMO V1.0 测试程序发布
iCore1S Demo V1.0程序说明 一.概要 本资料包含5个文件夹: 1.“ARM”里是iCore1S上ARM的程序包,开发环境为KEIL5.17: 2.“FPGA”里是iCore1S上FPG ...
- centos 扩容
1. 查看挂载点信息: [root@localhost]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/centos-root 18G 15G 2.9G 84% / ...
- 【QT】QT下载与安装
很简单 1.下载地址 http://download.qt.io/archive/qt/ 学个单词,archive. 选择一个版本下载,5.9.3. 2.安装 选择组件 MinGW就行了,MinGW- ...
- [Bayes] KL Divergence & Evidence Lower Bound
L lower是什么? L lower, 既然大于,那么多出来的这部分是什么?如下推导: 得出了KL的概念,同时也自然地引出了latent variable q.