Lucky Coins
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 927 Accepted Submission(s): 345
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
Source
题意:
算出每一个硬币成为luck币的概率。
POINT:
die[i][j]代表第 i 种硬币在 j 次投掷后全部被剔除的概率,
die[i][j]=(1−pj)n
(n代表这种硬币拥有的个数);
假设第 i 种硬币在 j 次投掷后成为幸运硬币, 那么该事件发生的概率为:
(alive[i][j]−alive[i][j+1])∗∏(die[k][j])
,(k为除了第 i 种硬币的其它种硬币),次数从1枚举到100即为该种硬币成为幸运硬币的最终概率。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
double die[20][111];
double alive[20][111];
double qkm(double base,int mi)
{
double ans=1;
while(mi){
if(mi&1) ans=ans*base;
mi>>=1;
base*=base;
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
int num;
double p;
scanf("%d %lf",&num,&p);
double base=p;
for(int k=1;k<=100;k++){
die[i][k]=qkm(1.0-base,num);
base=base*p;
alive[i][k]=1-die[i][k];
}
}
if(n==1){
printf("%.6lf\n",1.0);
continue;
}
for(int i=1;i<=n;i++){
double ans=0;
for(int k=1;k<=99;k++){
double base=1;
for(int j=1;j<=n;j++){
if(j==i) continue;
base=base*die[j][k];
}
base*=(alive[i][k]-alive[i][k+1]);
ans+=base;
}
printf("%.6lf%s",ans,i==n?"\n":" ");
}
}
}