标签:bzoj2318 spoj4060 game 概率dp
#include <stdio.h>
int main()
{
puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢");
puts("网址:blog.csdn.net/vmurder/article/details/46467899");
}
如果想选,对于
有
所以
如果想选,对于
有
所以
整理得:
然后剩
然后对于不想选的情况,那么
然而这样就没法用矩阵乘法了。。。
就需要黑科技,,当n很大时,其实概率已经基本不动了,,让n=min(n,1000)就好了Qwq。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 1010
using namespace std;
double f[N],g[N],p,q;
int main()
{
int i,T,n;
for(scanf("%d",&T);T--;)
{
scanf("%d",&n),n=min(n,1000);
scanf("%lf%lf",&p,&q);
f[0]=0,g[0]=1;
for(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;
}
【BZOJ2318】【spoj4060】game with probability Problem 概率DP
标签:bzoj2318 spoj4060 game 概率dp
原文地址:http://blog.csdn.net/vmurder/article/details/46467899