The dragon and the princess are arguing about what to do on the New Year‘s Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance.
They take turns drawing a mouse from a bag which initially contains w white and bblack mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn‘t scare other mice).Princess draws first. What is the probability of the princess winning?
If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one.
Input
The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000).
Output
Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9.
Example
Input
1 3
Output
0.500000000
Input
5 5
Output
0.658730159
题意: 公主和龙玩一个抓老鼠的游戏。袋子里,有两种老鼠,W只白老鼠,b只黑老鼠。一次抓出一只老鼠,公主先抓,龙后抓,龙抓出一只老鼠后,剩下的老鼠中会逃跑掉任意一只(跑掉的这只不算任何人抓的)。先抓到白老鼠的获胜(公主除抓到白老鼠获胜外,其余情况都算输),求公主获胜的概率。
题解:
思考: 对于 w 只白老鼠,b 只黑老鼠,公主要赢的情况
(一) 直接抓到一只白老鼠,概率为 p1 = w/(w+b)
(二) 抓到一只黑老鼠,但是龙也抓住一只黑老鼠,概率为
p2 = (1-p1)*(b-1)/(w+b-1) 然后跑掉一只老鼠,再分两种
跑掉一只白的 p3=w/(w+b-2) 变为 w-1 , b-2 的状态
跑掉一只黑的 p4=(b-2)/(w+b-2) 变为 w , b-3 的状态
dp[i][j] 代表 i 只白老鼠, j 只黑老鼠公主获胜的概率
dp[i][j]=p1 + p2*p3*dp[i-1][j-2] + p2*p3*dp[i][j-3];
1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 #define MAXN 1005 5 double dp[MAXN][MAXN]; 6 7 void Init() 8 { 9 for (int i=1;i<MAXN;i++) 10 { 11 for (int j=0;j<MAXN;j++) 12 { 13 double p1=0,p2=0; 14 if (i>=1) 15 p1 = (i*1.0)/(i+j); //公主赢 16 if (j>=2) 17 p2 = (1-p1)*(j-1.0)/(i+j-1); //龙抓黑 18 19 double p3 = 0,p4 = 0; 20 if (i>=1&&j>=2) p3 = (i*1.0)/(i+j-2); 21 if (j>=3) p4 =(j-2.0)/(i+j-2); 22 23 dp[i][j]= p1; 24 if (j>=2) dp[i][j]+=p2*p3*dp[i-1][j-2]; 25 if (j>=3) dp[i][j]+=p2*p4*dp[i][j-3]; 26 } 27 } 28 } 29 30 int main() 31 { 32 Init(); 33 int w,b; 34 scanf("%d%d",&w,&b); 35 printf("%.12lf\n",dp[w][b]); 36 return 0; 37 }