2318: Spoj4060 game with probability Problem|概率与期望

f[i]表示剩余i个石子A先手获胜的概率

g[i]表示剩余i个石子A后手获胜的概率

f[i]=p*g[i-1]+(1-p)*g[i];

g[i]=q*f[i-1]+(1-q)*f[i];

#include<cstdio>
#include<cstdlib>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
double f[101],g[101];
int main()
{
	int S;scanf("%d",&S);
	while(S--)
	{
		int n;scanf("%d",&n);n=min(n,100);
		double p,q;scanf("%lf%lf",&p,&q);
		f[0]=0;g[0]=1;
		for(int i=1;i<=n;i++)
		{
			if(f[i-1]>g[i-1])p=1-p,q=1-q;
			g[i]=(q*f[i-1]+(1-q)*p*g[i-1])/(p+q-p*q);
			f[i]=(p*g[i-1]+(1-p)*q*f[i-1])/(p+q-p*q);
			if(f[i-1]>g[i-1])p=1-p,q=1-q;
		}
		printf("%.6lf\n",f[n]);
	}
	return 0;
}


你可能感兴趣的:(概率与期望)