题目链接: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. 基于CentOS 搭建 Seafile 专属网盘

    系统要求:CentOS 7.2 64 位操作系统 安装 Seafile 安装依赖环境 使用 yum 安装 Python 及 MySQL: yum install python python-setup ...

  2. 【Android】Android的进程优先级

    android对于所有进程的处理态度都是尽可能不杀死.然而,资源总共就那么多,要是对所有进程都保持宽容的话,资源总会有消耗殆尽的时候.因此,在内存不足的情况,android系统需要根据一定的策略,选择 ...

  3. Apache Spark 2.2.0 正式发布

    本章内容: 待整理 参考文献: Apache Spark 2.2.0正式发布 Spark Release 2.2.0

  4. mac 安装memcached以及启动memcached

    参考链接:https://blog.csdn.net/whereismatrix/article/details/50485570

  5. linux tcp相关参数

    /etc/sysctl.conf文件 /etc/sysctl.conf是一个允许你改变正在运行中的Linux系统的接口.它包含一些TCP/IP堆栈和虚拟内存系统的高级选项,可用来控制Linux网络配置 ...

  6. JAVA(二)异常/包及访问权限/多线程/泛型

    成鹏致远 | lcw.cnblog.com |2014-01-28 异常 1.异常的基本概念 异常是导致程序中断运行的一种指令流 基本的异常处理格式:try...catch,try中捕获异常,出现异常 ...

  7. 【Java】Comparable和Comparator接口的区别

    Java提供了只包含一个compareTo()方法的Comparable接口.这个方法可以个给两个对象排序.具体来说,它返回负数,0,正数来表明已经存在的对象小于,等于,大于输入对象. Java提供了 ...

  8. 【iCore1S 双核心板_ARM】例程二:读取ARM按键状态

    实验原理: 按键的一端与STM32的GPIO(PB9)相连,且PB9外接一个1k大小的限流上接电阻. 初始化时把PB9设置成输入模式,当按键弹起时,PB9由于上拉电阻的作用呈高电平(3.3V): 当按 ...

  9. Java连接各种数据库写法

    # 示例配置参考,涵盖几乎所有的主流数据库 ############# Oracle数据库 ######################## #   数据库驱动名 driver=oracle.jdbc ...

  10. java InputStream和OutputStream

    InputStream类型 类 功能 构造器参数 如何使用 ByteArrayInputStream 允许将内存的缓冲区当做InputStreams使用 缓冲区,字节将从中取出 作为一种数据源:将其与 ...