Test instructions
There are w white ball in the bag B black Ball, now two people take a ball each time (do not put back), the first to take the victory of the white ball, when the hands take away a ball, the ball in the bag will randomly miss one, ask the probability of winning.
Analysis:
DP[I][J] Represents the I white ball in the bag J Black Ball, the initiator to take the probability of winning.
There are four different cases
The initiator takes the white ball, the probability of winning 1.0*i/(I+J);
Take the white ball, the initiator loses
The first two were taken to the black ball, missing a black ball, transferred to Dp[i][j-3]
The first two were taken to the black ball, missing a white ball, transferred to Dp[i-1][j-2]
#include <map>#include<Set>#include<list>#include<cmath>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<string>#include<cctype>#include<complex>#include<cassert>#include<utility>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespaceStd;typedef pair<int,int>Pii;typedefLong Longll;#defineLson l,m,rt<<1#definePi ACOs (-1.0)#defineRson m+1,r,rt<<11#defineAll 1,n,1#defineRead Freopen ("In.txt", "R", stdin)Constll infll =0x3f3f3f3f3f3f3f3fll;Const intinf=0x7ffffff;Const intMoD =1000000007;intw,b;Doubledp[1001][1001];voidsolve () {memset (DP,0,sizeof(DP)); for(intI=1; i<=w;++i) dp[i][0]=1;//only white balls left. for(intI=1; i<=w;++i) for(intj=1; j<=b;++j) {Dp[i][j]=1.0*i/(i+j); if(j>=3) Dp[i][j]+=dp[i][j-3]*1.0*j/(I+J) * (J-1)/(i+j-1) * (J-2)/(i+j-2); if(i>=1&&j>=2) Dp[i][j]+=dp[i-1][j-2]*1.0*j/(I+J) * (J-1)/(i+j-1) *i/(i+j-2); } printf ("%.9lf\n", Dp[w][b]);}intMain () { while(~SCANF ("%d%d",&w,&b)) {Solve ();}return 0;}
Codeforces 148d-bag Of Mice (probability DP)