Subject:
https://www.luogu.com.cn/problem/CF148D
Nn in the bagn white mice andmmm black rats,AAA andBBB takes turns from the bag, whoever catches the white first wins. AAA randomly grabs one at a time,BBAfter B randomly catches one, another random mouse will run out. If neither person catches white, thenBBB赢. AAA catch first, askAAThe probability of A winning.
Idea:
triples (A, i, j) (A,i,j)(A,i,j ) meansiii white mouse,jjj black mice,AAA first catch this state,
usef (A, i, j) f(A,i,j)f(A,i,j ) means(A, i, j) (A,i,j)(A,i,j ) indicates the state to start the game,AAThe probability of A winning, the answer isf (A, n, m) f(A,n,m)f(A,n,m)。
( A , i , j ) i i + j → A 赢 ( A , i , j ) j i + j → ( B , i , j − 1 ) \begin{aligned} &(A,i,j)\quad\frac{i}{i+j}\rightarrow\quad A赢\\ &(A,i,j)\quad\frac{j}{i+j}\rightarrow\quad (B,i,j-1)\\ \end{aligned} (A,i,j)i+ji→A win(A,i,j)i+jj→(B,i,j−1)
Represents (A, i, j) (A, i, j)(A,i,j)有 i i + j \frac{i}{i+j} i+jiThe probability of entering directly into the winning state, ji + j \frac{j}{i+j}i+jjThe probability of entering (B, i, j − 1) (B,i,j-1)(B,i,j−1 ) This state. So
f (A, i, j) = ii + j ∗ 1 + ji + j ∗ f (B, i, j − 1) f(A,i,j)=\frac(i)(i+j) *1+\frac{j}{i+j}*f(B,i,j-1)f(A,i,j)=i+ji∗1+i+jj∗f(B,i,j−1 )
Similarity analysis(B, i, j) (B, i, j)(B,i,j ) .
You can directly memorize the search, pay attention to the analysis of the initial value.
#include<bits/stdc++.h>
using namespace std;
const int N=1009;
int n,m;
double dp[2][N][N];
double dfs(int x,int y,int z)
{
if(!y)
return 0;
if(!z&&x)
return 1;
if(!z)
return 0;
if(z<0&&x)
return 0;
if(dp[x][y][z]!=0)
return dp[x][y][z];
if(x)
dp[x][y][z]+=(double)z/(y+z)*dfs(0,y,z-1)+(double)y/(y+z);
else
dp[x][y][z]+=(double)z/(y+z)*(((double)z-1)/(y+z-1)*dfs(1,y,z-2)+(double)y/(y+z-1)*dfs(1,y-1,z-1));
return dp[x][y][z];
}
int main()
{
scanf("%d%d",&n,&m);
printf("%.9lf",dfs(1,n,m));
return 0;
}