题目链接: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. String为什么是不可变的?

    面试官Q1:请问为什么String是不可变的,能谈谈吗? 我们知道不管是面试初级.中级还是高级Java开发工程师,String永远都是一个绕不开的话题,而且问的问题也是各不相同,下面我们从几个角度来看 ...

  2. 在Asp.Net中操作PDF – iTextSharp - 操作图片

    iTextSharp支持所有主流的图片格式,比如:jpg, tif, gif, bmp, png和wmf.在iTextSharp中使用Image.GetInstance()方法创建图片有很多种方式,或 ...

  3. 【Android】Android输入子系统

    成鹏致远 | lcw.cnblogs.com | 2013-10-25 Linux输入子系统回顾 1:为什么要回顾linux输入子系统?这个问题后面自然就知道了 1.linux输入子系统设备是基于平台 ...

  4. goldengate–使用filter+@GETENV在线重新初始化指定的table

    goldengate–使用filter+@GETENV在线重新初始化指定的table 转载:http://www.easyora.net/blog/using_filter_getenv_functi ...

  5. python虚拟环境virtualenv的安装与使用

    如果我们要同时开发多个应用程序,每个应用可能需要各自拥有一套“独立”的Python运行环境,我们可以使用virtualenv解决这个问题,它可以为一个应用创建一套“隔离”的Python运行环境. 一. ...

  6. Hadoop2.6新增用户隔离

    1.hadoop文件权限介绍 (这部分内容参考成品  https://blog.csdn.net/skywalker_only/article/details/40709447) 之前在论坛看到一个关 ...

  7. javascript and jquery 代码块

    1.5秒隐藏 (setTimeout ) setTimeout( function(){ $('.ad_midd').slideUp(); } , 5000 )

  8. supervisor开机自动启动脚本+redis+MySQL+tomcat+nginx进程自动重启配置

    [root@mongodb-host supervisord]# cat mongo.conf [program:mongo]command=/usr/local/mongodb/bin/mongod ...

  9. Java知多少(27)继承的概念与实现

    继承是类与类之间的关系,是一个很简单很直观的概念,与现实世界中的继承(例如儿子继承父亲财产)类似. 继承可以理解为一个类从另一个类获取方法和属性的过程.如果类B继承于类A,那么B就拥有A的方法和属性. ...

  10. Oracle Enterprise Linux 6.4 下挂载ISCSI 设备

    Oracle Enterprise Linux 6.4 下挂载ISCSI 设备一.发现① 要求安装iscsi客户端软件   yum install iscsi-initiator-utils ② 发现 ...