HDU 5985 Lucky Coins 數學

Lucky Coins

題目鏈接:

http://acm.hdu.edu.cn/showproblem.php?pid=5985php

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.c++

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.git

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.this

Sample Input

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

Sample Output

1.000000
0.210526 0.473684
0.124867 0.234823 0.420066code

Hint

題意

有一堆硬幣,每回合爲正面的機率爲P,每回合咱們都會去掉當前翻面爲反面的硬幣。ip

問每種硬幣剩到只剩下一個的機率是多少。ci

保證 0.4<P<0.6rem

題解:

給了機率的範圍,顯然這道題就是模擬扔就好了,隨便扔個幾十回合,這個機率就會降到很小的範圍。get

第i個硬幣第j回合全死掉的機率爲 (1-P^j)^num[i]

活下來的機率固然是1-死掉的。

代碼

#include<bits/stdc++.h>
using namespace std;
const int maxn = 15;
int n;
double num[maxn],ans[maxn],p[maxn];
double count_die(int x,int y){
    return pow(1-pow(p[x],y),num[x]);
}
double count_live(int x,int y){
    return 1-count_die(x,y);
}
void solve(){
    scanf("%d",&n);
    memset(ans,0,sizeof(ans));
    for(int i=0;i<n;i++)
        cin>>num[i]>>p[i];
    if(n==1){
        printf("1.000000\n");
        return;
    }
    for(int i=1;i<100;i++){
        for(int j=0;j<n;j++){
            double tmp = 1;
            for(int k=0;k<n;k++){
                if(k==j)continue;
                tmp*=count_die(k,i);
            }
            ans[j]+=(count_live(j,i)-count_live(j,i+1))*tmp;
        }
    }
    for(int i=0;i<n;i++)
        if(i==0)printf("%.6f",ans[i]);
        else printf(" %.6f",ans[i]);
    printf("\n");
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--)solve();
}
相關文章
相關標籤/搜索