Probability dp dpd p exercises
Foreword: Too bad, I didn't learn this knowledge point, let's make up, hope not to coo.
1.D. Bag of mice
Idea: Board question, find the probability of the princess winning, the state is set to dp [i] [j] dp[i][j]dp[i][j]有 i i i white mouse,jjWith j black mouse, the princess will win the probability first.
First initialize:dp [i] [0] = 1, dp [0] [j] = 0, i ∈ [1, w], j ∈ [0, b] dp[i][0]=1,dp[ 0][j]=0,i\in[1,w],j\in[0,b]dp[i][0]=1,dp[0][j]=0,i∈[1,w],j∈[0,b ]
Then state transition:
There are four situations:
1. The princess touches the white mouse,dp [i] [j] + = ii + j dp[i][j]+=\dfrac{i}{i+j}dp[i][j]+=i+ji
2. The princess touches the black mouse, and the dragon touches the white mouse.
3. The princess touches the black mouse, the dragon touches the black mouse, and a black mouse runs out.
dp [i] [j] + = ji + j × j − 1 i + j − 1 × j − 2 i + j − 2 × dp [i] [j − 3] dp[i][j]+=\ dfrac{j}{i+j}\times \dfrac{j-1}{i+j-1} \times \dfrac{j-2}{i+j-2}\times dp[i][j- 3]dp[i][j]+=i+jj×i+j−1j−1×i+j−2j−2×dp[i][j−3 ]
4. The princess touched the black mouse, the dragon touched the black mouse, and ran out a white mouse.
dp [i] [j] + = ji + j × j − 1 i + j − 1 × ii + j − 2 × dp [i − 1] [j − 2] dp[i][j]+=\dfrac {j}{i+j}\times \dfrac{j-1}{i+j-1} \times \dfrac{i}{i+j-2}\times dp[i-1][j-2 ]dp[i][j]+=i+jj×i+j−1j−1×i+j−2i×dp[i−1][j−2]
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e3+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
#define mst(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define fi first
#define se second
#define pb push_back
int w,b;
double dp[N][N];
int main(){
scanf("%d%d",&w,&b);
for(int i=0;i<=w;i++) dp[i][0]=1;
for(int i=0;i<=b;i++) dp[0][i]=0;
for(int i=1;i<=w;i++)
for(int j=1;j<=b;j++){
dp[i][j]+=(double)i/(i+j);
if(j>=3){
dp[i][j]+=(double)j/(i+j)*(j-1)/(i+j-1)*(j-2)/(i+j-2)*dp[i][j-3];
}
if(j>=2){
dp[i][j]+=(double)j/(i+j)*(j-1)/(i+j-1)*i/(i+j-2)*dp[i-1][j-2];
}
}
printf("%.9f\n",dp[w][b]);
return 0;
}