HDU 5985/nowcoder 207D

题目链接: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$ 次实验后已经死掉的概率为 $Pleft( {X = 0,1,2, cdots ,k - 1} ight) = 1 - Pleft( {X = k} ight) = 1 - C_k^k {p_i}^k left( {1 - p_i } ight)^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]) imes prodlimits_{j e i} {die[t][j]}$,

因此答案即为 $sumlimits_{t = 1}^infty {left( {left( {stay[t][i] - stay[t + 1][i]} ight)prodlimits_{j e i} {die[t][j]} } ight)}$。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxk=13;

int k;
double die[105][maxk],stay[105][maxk];
double ans[maxk];

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d",&k);
        for(int i=1;i<=k;i++)
        {
            int n; double p;
            scanf("%d%lf",&n,&p);
            for(int t=1;t<=101;t++)
            {
                die[t][i]=pow(1-pow(p,t),n);
                stay[t][i]=1-die[t][i];
            }
        }

        if(k==1) //特判
        {
            printf("1.000000
");
            continue;
        }

        for(int i=1;i<=k;i++)
        {
            ans[i]=0;
            for(int t=1;t<=100;t++)
            {
                double mul=1;
                for(int j=1;j<=k;j++) if(i!=j) mul*=die[t][j];
                ans[i]+=(stay[t][i]-stay[t+1][i])*mul;
            }
            printf("%.6f%c",ans[i],i<k?' ':'
');
        }
    }
}
原文地址:https://www.cnblogs.com/dilthey/p/9754905.html