Title Link: Http://codeforces.com/problemset/problem/148/D
The main topic: a bag of W White mouse, b black Mouse; A and b take the mouse in turn (not put back), who first caught microscope, who win; because B rude, every time a mouse, will run out of a first;
The probability of seeking a win;
Topic Analysis:
This kind of probability DP state is fixed, DP (i, j) indicates the probability of the current state awin;
The probability of 1:dp[i][0],a win is 1;dp[0][j] probability is 0;
2:DP[I][J] The following four types of transfers:
If a takes white, the probability is i/(i+j),
If a takes black, B takes white, the probability is 0,
If a takes black, B takes black, runs out black probability 1.0*j/(i+j) * (j-1.0)/(i+j-1.0) * (j-2.0)/(i+j-2) *dp[i][j-3]; Premise: Black >=3
If a takes black, B takes black, runs out white probability 1.0*j/(i+j) * (j-1.0)/(i+j-1.0) *i/(i+j-2) *dp[i-1][j-2]; Premise: White >=1; black >=2
Code:
author:acsorry//result:yes#include<iostream> #include <cstdio> #include <cstring> #include < cmath> #include <algorithm> #include <vector> #include <string> #include <queue> #include <deque> #include <stack> #include <map> #include <set> #define INF 1<<29#define maxInt 0x7fffffff#define SUP 0x80000000#define Mem (A, B) memset (A,b,sizeof (a)) using namespace Std;typedef long long ll;const int N=1007;double Dp[n][n];int Main () {int w,b; while (cin>>w>>b) {dp[0][0]=0; for (int i=1;i<=w;i++) dp[i][0]=1.0;//only the white mouse for (int i=1;i<=b;i++) dp[0][i]=0.0;//only black rat for (int i=1;i<= w;i++) {for (int j=1;j<=b;j++) {dp[i][j]=1.0*i/(i+j); if (j>=3) dp[i][j]+=1.0*j/(i+j) * (j-1.0)/(i+j-1.0) * (j-2.0)/(i+j-2) *dp[i][j-3]; if (j>=2) dp[i][j]+=1.0*j/(i+j) * (j-1.0)/(i+j-1.0) *i/(i+j-2) *dp[i-1][j-2]; } } printf ("%.9f\n", Dp[w][b]); } return 0;}
CF 148D. Bag of mice[probability DP]