Main topic:
The black box has w white mouse and B black mouse, Dragon and Princess take a mouse in turn from the box, the first to take the white mouse to win, but the dragon every time will be more run out of a mouse.
Thinking Analysis:
DP[I][J] means that when I have a white mouse and a J black mouse in the box, the probability of Princess winning first.
Then there are 4 of transfer equations.
respectively:
Take it straight to the white mouse.
Taking the Black mouse, Dragon also got the Black mouse, ran a black mouse.
Take the Black mouse, Dragon also got the Black mouse, ran a white mouse.
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm>using namespace Std;double Dp[1005][1005];int Main () {int w,b;cin>>w>>b;for (int i=1;i<=w;i++) {dp[i][0]=1.0;} for (int i=1;i<=b;i++) {dp[0][i]=0.0;} for (int i=1;i<=w;i++) {for (int j=1;j<=b;j++) {dp[i][j]=0.0;dp[i][j]+=1.0*i/(i+j); if (j>=3) dp[i][j]+=1.0*j/ (i+j) *1.0* (j-1)/(i+j-1) *1.0* (j-2)/(i+j-2) *dp[i][j-3];if (j>=2) dp[i][j]+=1.0*j/(i+j) *1.0* (j-1)/(i+j-1) *1.0* (i )/(I+j-2) *dp[i-1][j-2];}} printf ("%.9lf\n", Dp[w][b]); return 0;}
Codeforces 148D Bag Of Mice (probabilistic DP)