HDU 5985 Lucky Coins(概率)

Problem Description

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.

Input

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.

Output

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.

Sample Input

3
1
1000000 0.5
2
1 0.4
1 0.6
3
2 0.4
2 0.5
2 0.6

Sample Output

1.000000
0.210526 0.473684
0.124867 0.234823 0.420066

题目大意

存在n种硬币,每种硬币有num个,每种硬币投掷时正面向上的概率为p。对所有的硬币进行投掷,如果反面向上就剔除,最后剩下一种硬币时该种硬币就为幸运硬币。求每一种硬币成为幸运硬币的概率。

解题思路

求第 i 次投掷时某种硬币成为幸运硬币的概率等于该种硬币留下来的概率*剩余的每一种硬币消失的概率。通过数组保存每一种状态。
die[i][j]代表第 i 种硬币在 j 次投掷后全部被剔除的概率, die[i][j]=(1pj)n (n代表这种硬币拥有的个数);
alive[i][j]代表第 i 中硬币在 j 次投掷后仍存在正面向上的概率, alive[i][j]=1die[i][j]
假设第 i 种硬币在 j 次投掷后成为幸运硬币, 那么该事件发生的概率为: (alive[i][j]alive[i][j+1])(die[k][j]) ,(k为除了第 i 种硬币的其它种硬币),次数从1枚举到可以保证精度的最大值即为该种硬币成为幸运硬币的最终概率。

代码实现

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define maxn 100
#define ll long long
double die[17][maxn],alive[17][maxn];
int n;
double quick_pow(double p2,int b)
{
    double ans=1;
    while(b>0)
    {
        if(b%2)
            ans*=p2;
        p2*=p2;
        b/=2;
    }
    return ans;
}

void initvalue(int kinds,int num,double p)
{
    double pi=p;
    for(int i=1;i<maxn;i++)
    {
        die[kinds][i]=quick_pow(1-pi,num);
        alive[kinds][i]=1-die[kinds][i];
        pi*=p;
    }
}

void solve()
{
    for(int i=0;i<n;i++)
    {
        double ans=0;
        for(int j=1;j<maxn-2;j++)
        {
            double temp=alive[i][j]-alive[i][j+1];
            for(int k=0;k<n;k++)
                if(k!=i)
                    temp*=die[k][j];
            ans+=temp;
        }
        printf(i==n-1?"%.6f\n":"%6f ",ans);
    }
}

int main()
{
    int T,num;
    double p;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%lf",&num,&p);
            initvalue(i,num,p);
        }
        if(n==1)
            printf("1.000000\n");
        else
            solve();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值