CF148D Bag of mice

\(f(i,j)\)表示\(i\)只白鼠,\(j\)只黑鼠\(A\)先手胜的概率

全白先手必胜,有一只黑鼠时,先手若抽到黑鼠则后手必胜,所以先手首回合必抽到白鼠

\(\large f(i,0)=1,f(i,1)=\frac i{i+1}\)

先手抽白鼠,胜\(\large\frac i{i+j}\),先手抽黑鼠,后手白鼠\(0\)

先手抽黑鼠,后手抽黑鼠,跑一只白鼠

\[\frac i{i+j}\times{\frac{j-1}{i+j-1}}\times\frac i{i+j-2}\times f(i-1,j-2)\\ \]

先手抽黑鼠,后手抽黑鼠,跑黑鼠

\[\frac i{i+j}\times\frac{j-1}{i+j-1}\times\frac{j-2}{i+j-2}\times f(i,j-3) \]

最后

\[f(i,j)=\frac i{i+j}+\frac i{i+j}\times{\frac{j-1}{i+j-1}}\times\frac i{i+j-2}\times f(i-1,j-2)+\frac i{i+j}\times\frac{j-1}{i+j-1}\times\frac{j-2}{i+j-2}\times f(i,j-3) \]

\(O(wb)\)

double f[N][N]; int n,m;
int main(){
	scanf("%d%d",&n,&m);
	for(int i = 1;i <= n;++i){
		f[i][0] = 1.0;
		f[i][1] = 1.0*i/(i+1);
	}
	if(m == 1 || !m) { printf("%.15f",f[n][m]); return 0; } 
	for(int i = 1;i <= n;++i)
		for(int j = 2;j <= m;++j){
			f[i][j] = (1.0*i)/(j+i) + (1.0*j)/(j+i) * (1.0*j-1)/(j+i-1) * (1.0*i)/(j+i-2) * f[i-1][j-2];
            if(j^2) f[i][j] += (1.0*j)/(j+i) * (1.0*j-1)/(j+i-1) * (1.0*j-2)/(j+i-2) * f[i][j-3];
	}
	printf("%.15f",f[n][m]);
}
posted @ 2020-10-16 11:19  INFP  阅读(75)  评论(0编辑  收藏  举报