题目链接: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 - [概率题]的更多相关文章

  1. HDU5985 Lucky Coins 概率dp

    题意:给你N种硬币,每种硬币有Si个,有Pi 概率朝上,每次抛所有硬币抛起,所有反面的拿掉,问每种硬币成为最后的lucky硬币的概率. 题解:都知道是概率dp,但是模拟赛时思路非常模糊,很纠结,dp[ ...

  2. HDU.5985.Lucky Coins(概率DP)

    题目链接 \(Description\) 有n(n<=10)种硬币,已知每种硬币的数量和它抛一次正面朝上的概率pi.进行如下过程:每次抛一次所有硬币,将正面朝下的硬币去掉.重复该过程直到只剩一种 ...

  3. HDU 2843 I Will Win(概率题?,怨念颇深,简单)

    题目 真不想说什么,,,这神题真讨厌,,,多校的.. //又是一道神题... #include<stdio.h> #include<string.h> //最大公约数 int ...

  4. HDU 5985 Lucky Coins 数学

    Lucky Coins 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5985 Description Bob has collected a lot ...

  5. HDU 5985 概率

    n种硬币各有cnt[i]枚,每轮下其有p[i]概率保留,问各种硬币只有它存活到最后一轮的概率. 设k轮后i硬币存活概率$a[i][k]=(1-p^k_i)^{cnt[i]}$ 则最后只有第i种硬币存活 ...

  6. hdu 5676 ztr loves lucky numbers

    题目链接:hdu 5676 一开始看题还以为和数位dp相关的,后来才发现是搜索题,我手算了下,所有的super lucky number(也就是只含数字4, 7且4, 7的数量相等的数)加起来也不过几 ...

  7. HDU 2096 小明A+B --- 水题

    HDU 2096 /* HDU 2096 小明A+B --- 水题 */ #include <cstdio> int main() { #ifdef _LOCAL freopen(&quo ...

  8. HDU 1248 寒冰王座(全然背包:入门题)

    HDU 1248 寒冰王座(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票 ...

  9. poj3519 Lucky Coins Sequence矩阵快速幂

    Lucky Coins Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. java中使用队列:java.util.Queue(转)

    队列是一种特殊的线性表,是运算受到限制的一种线性表,只允许在表的一端进行插入,而在另一端进行删除元素的线性表.队尾(rear)是允许插入的一端.队头(front)是允许删除的一端.空队列是不含元素的空 ...

  2. SNF快速开发平台MVC-EasyUI3.9之-WebApi和MVC-controller层接收的json字符串的取值方法和调用后台服务方法

    最近项目组很多人问我,从前台页面传到后台controller控制层或者WebApi 时如何取值和运算操作. 今天就都大家一个在框架内一个取值技巧 前台JS调用代码: 1.下面是选中一行数据后右键点击时 ...

  3. [svc]对称加密/非对称加密细枝末节-如何做到数据传输的authentication/data integrity/confidentiality(私密)

    对称/非对称/混合加密的冷知识 数据在互联网上传输,要考虑安全性. 讲到安全,要从三方面考虑: 1.authentication 每一个IP包的认证,确保合法源的数据 2.data integrity ...

  4. 升级python(linux)

    查看系统当前python版本 2: [root@wangyuelou ~]# python     Python 2.4.3 (#1, May  5 2011, 16:39:10)     [GCC ...

  5. tensorflow 笔记8:RNN、Lstm源码,训练代码输入输出,维度分析

    tensorflow 官网信息:https://www.tensorflow.org/api_docs/python/tf/contrib/rnn/BasicLSTMCell tensorflow 版 ...

  6. linux每日命令(6):rm命令

    rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除.对于链接文件,只是删除了链接,原有文件均保持不变. rm是一个危险的命令,使用的 ...

  7. RSA加密和解密工具类

    import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; import java.security.*; i ...

  8. pandas 学习总结

    pandas  学习总结 作者:csj 更新时间:2018.04.02 shenzhen email:59888745@qq.com home: http://www.cnblogs.com/csj0 ...

  9. 【iCore4 双核心板_FPGA】例程七:状态机实验——状态机使用

    实验现象:按键每按下一次,三色LED改变一次状态. 核心代码: //--------------------module_rst_n---------------------------// modu ...

  10. PaaS 应用引擎

    这里主要是梳理一下应用引擎(XXXX App Engine),它一般被归类到PaaS领域.应用引擎即提供了各种编程语言开发的应用所需的一整套运行环境:它开箱即用,你只需部署应用的代码即可,无需前期的环 ...