【BZOJ】【P2318】【Spoj4060 game with probability Problem】【题解】【概率DP】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2318

【BZOJ】【P2318】【Spoj4060 game with probability Problem】【题解】【概率DP】_第1张图片

Code:

#include
using namespace std;
int main(){
	int T,n;
	double p,q;
	static double f[1002],g[1002];
	cin>>T;
	while(T--){
		cin>>n>>p>>q;n=min(n,1000);
		p=max(p,1-p);q=max(q,1-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;
			f[i]=(p*g[i-1]+(1-p)*q*f[i-1])/(1-(1-p)*(1-q));
			g[i]=(q*f[i-1]+(1-q)*p*g[i-1])/(1-(1-p)*(1-q));
			if(f[i-1]>g[i-1])p=1-p,q=1-q;
		}printf("%.6lf\n",f[n]);
	}
	return 0;
}


你可能感兴趣的:(OI)