题目链接
https://www.lydsy.com/JudgeOnline/problem.php?id=2318
题解
记表示Alice胜的概率,表示Bob胜的概率,表示Alice抛出正面的概率,表示Bob抛出正面的概率,则:
化简得到
可以看出,当时,,否则。初值
但是的转移显然会TLE,事实上,当很大时,概率几乎不再改变,因此当时可以认为。
代码
#include <cstdio>
#include <algorithm>
const int maxn=100;
int t,n;
double p,q,f[maxn+10],g[maxn+10];
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%lf%lf",&n,&p,&q);
f[0]=0;
g[0]=1;
n=std::min(n,maxn);
for(int i=1; i<=n; ++i)
{
double a,b;
if(f[i-1]>g[i-1])
{
a=1-p;
b=1-q;
}
else
{
a=p;
b=q;
}
f[i]=(a*g[i-1]+(1-a)*b*f[i-1])/(a+b-a*b);
g[i]=(b*f[i-1]+(1-b)*a*g[i-1])/(a+b-a*b);
}
printf("%.6lf\n",f[n]);
}
return 0;
}