Cf d. Bag of mice (probability dp)
Here there are w white rats and B black rats. Dragon and Princess Wang take turns to capture the rats from the bag. Each time they capture one, the first white rat wins. When a dragon catches a mouse, the bag will run away from a mouse. The mouse that runs away is equal to probability. Ask Wang's probability of victory.
The probability of Princess Wang's victory in the presence of j black rats is dp [I] [j], which can be obtained from the following three States:
Wang Yu won a white rat for the first time with the probability of I/(I + j );
Princess did not get the white mouse, the probability of taking the black rat is j/(I + j), if the princess to win, the next time the dragon must take the black mouse, the probability is (J-1) /(I + J-1) while escaping is a black rat, probability is (J-2)/(I + J-2), status is transferred to dp [I] [J-3];
Princess did not get the white mouse, the probability of taking the black rat is j/(I + j), if the princess to win, the next time the dragon must take the black mouse, the probability is (J-1) /(I + J-1), also run away is the white mouse, probability is I/(I + J-2), the State is transferred to dp [I-1] [J-2];
In summary, dp [I] [j] = I/(I + j) + j/(I + j) * (J-1)/(I + J-1) * (J-2) /(I + J-2) * dp [I] [J-3] + j/(I + j) * (J-1)/(I + J-1) * I/(I + J-2) * dp [I-1] [J-2].
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include //#define LL __int64#define LL long long#define eps 1e-12#define PI acos(-1.0)using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 4010;double dp[1010][1010];int main(){int w,b;while(~scanf(%d %d,&w,&b)){for(int i = 1; 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] = i*1.0/(i+j);if(j >= 2)dp[i][j] += j*1.0/(i+j) * (j-1)*1.0/(i+j-1) * (i*1.0)/(i+j-2) * dp[i-1][j-2];if(j >= 3)dp[i][j] += j*1.0/(i+j) * (j-1)*1.0/(i+j-1) * (j-2)*1.0/(i+j-2) * dp[i][j-3];}}printf(%.9lf,dp[w][b]);}return 0;}