题目链接
参考博客:
题意:
给你n种硬币,每次抛所有的硬币并移除字朝上的硬币,当只剩下一种硬币的时候,该种硬币为Lucky Coins,求每种硬币能成为Lucky Coins的概率;
#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)//第x种硬币第y次投全部remove的概率
{
return pow(1-pow(p[x],y),num[x]);
}
double count_live(int x,int y)//第x种硬币第y次投仍未全被remove的概率
{
return 1-count_die(x,y);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(ans,0,sizeof(ans));
for(int i=0; i<n; i++)
{
scanf("%lf %lf",&num[i],&p[i]);//num[i]每种硬币的个数,p[i]为其概率
}
if(n==1)//特判
{
printf("1.000000\n");
}
else
{
for(int i=0; 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;//(count_live(j,i)-count_live(j,i+1))表示第j次该种硬币未被remove而j+1次时被remove,整个式子表示该种硬币为幸运硬币的概率;
}
}
for(int i=0; i<n; i++)
{
printf("%.6f%c",ans[i],i==n-1?'\n':' ');
}
}
}
}