Lucky Coins
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5985
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
Hint
题意
有一堆硬币,每回合为正面的概率为P,每回合我们都会去掉当前翻面为反面的硬币。
问每种硬币剩到只剩下一个的概率是多少。
保证 0.4<P<0.6
题解:
给了概率的范围,显然这道题就是模拟扔就行了,随便扔个几十回合,这个概率就会降到很小的范围。
第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();
}