nowcoder 207D - Lucky Coins - []

题目链接:https://www.nowcoder.com/acm/contest/207/D

时间限制: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$ 种硬币成为幸运币的各自的概率。

题解:

首先,假设 $f[t][i]$ 代表在 $t$ 次投掷之后,第 $i$ 种硬币已全部被去掉的概率;

那么,考虑如何计算 $f[t][i]$,因为属于第 $i$ 种的所有硬币,被去掉的概率是互不影响的,所以可以分开计算:

即先计算第 $i$ 种硬币中的第一个,它在 $t$ 次投掷之后依然存留的概率为 $p[i] ^ t$,则它在 $t$ 次投掷之后被去掉的概率为 $1 - p[i] ^ t$,

所以 $n[i]$ 个硬币全部被去掉的概率为 $f[t][i] = (1 - p[i] ^ t) ^ {n[i]}$;

其次,显然 $1 - f[t][i]$ 就代表在 $t$ 次投掷之后,第 $i$ 种硬币至少还有一个存留的概率;

不妨再假设 $g[t][i]$ 代表在 $t$ 次投掷之后,第 $i$ 种硬币成为幸运币的概率;

不难得到公式 $g\left[ t \right]\left[ i \right] = \left( {1 - f\left[ t \right]\left[ i \right]} \right) \cdot \prod\limits_{j \ne i} {f\left[ t \right]\left[ j \right]}$,

以及 

猜你喜欢

转载自www.cnblogs.com/dilthey/p/9754905.html