D. Bag Of Mice
The Dragon and the princess is arguing about-what-does on the New year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the Moonlight and while the Princess thinks they shou LD just go to bed early. They is desperate to come to a amicable agreement, so they decide to leave the to chance.
they take turns drawing a mouse from a bag Which initially Contains w white and b black mice. The person was the first to draw a white mouse wins. After all mouse drawn by the dragon the rest of the mice in the bag panic, and one of the them jumps out of the bag itself (the P Rincess draws she mice carefully and doesn ' t scare other mice). princess draws first . what is the probability of the princess winning?
If There is no more mice in the bag and nobody have drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves is not considered to being drawn (do not define the winner). Once a mouse have 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 integers w and b (0≤ w, b ≤1000 ).
Output
Output the probability of the princess winning. The answer is considered to being correct if its absolute or relative error does not exceed 9.
Sample Test (s)input
1 3
Output
0.500000000
input
5 5
Output
0.658730159
Note
Let ' s go through the first sample. The probability of the princess drawing a white mouse on his first turn and winning right away are. The probability of the dragon drawing a black mouse and not winning on he first turn is 3/4 * 2/3 =. After this there is the mice left in the bag-one black and one white; One of the them jumps out, and the other are drawn by the princess in her second turn. If The princess ' Mouse is white, she wins (probability is a), otherwise nobody gets the white mouse, so Acco Rding to the rule the Dragon wins.
Test instructions
The king and the Princess are making a game,
There was a bag, with a white mouse and a black mouse in the first.
Now 2 people take turns to catch mice from the bag, rule:
1. Princess Initiator
2. Catch the White mouse win first
3. The princess is more gentle when caught, and the king is more violent, so every time the king catches, there will be a mouse frightened and escaped.
4. The mouse will not go back.
5. If until the end, 2 people did not catch microscope, calculate the king win
6. Because the bag is invisible, the probability of each mouse being caught is the same.
Ideas:
The state (I,J) indicates that there is an I white mouse in the bag, the state of the J Black Mouse
DP[I][J] means that the princess can face (i,j) The probability of this situation
As the princess, the Princess will certainly face (w,b) This situation, initialize: dp[w][b]=1.0, the remaining 0.0
(also shows that the princess can not face (w,b-1), (w-1,b) These situations)
For a situation (I,J), not equal to (w,b), the princess can face the conditions of this situation:
1. In the situation (w+1,b+2), the princess caught the B, and then the king, the king caught a B, scared to run W
2. In the situation (w,b+3), the princess caught the B, and then the king, the king caught a B, scared to run B
Then we can get the equation of state transition.
Then, the probability that the princess faces every situation is dp[i][j], the probability of winning in this situation is i/(I+J)
Accumulate all the situation the probability that the princess wins, that is the answer.
1#include <cstdio>2#include <cstring>3 4 Const intmaxn=1010;5 6 DoubleDP[MAXN][MAXN];7 8 intMain ()9 {Ten intw,b; Onescanf"%d%d",&w,&b); AMemset (DP,0,sizeofDP); - -dp[w][b]=1.0; the for(inti=w;i>=0; i--) - { - for(intj=b;j>=0; j--) - { + if(i==w&&j==b) - Continue; +dp[i][j]=dp[i+1][j+2]* (j+2)/(i+j+3) * (j+1)/(i+j+2) * (i+1)/(i+j+1) A+dp[i][j+3]* (j+3)/(i+j+3) * (j+2)/(i+j+2) * (j+1)/(i+j+1); at } - } - Doublecn1=0.0; - for(intI=0; i<=w;i++) - { - for(intj=0; j<=b;j++) in { - if(i==0&&j==0) to Continue; +ans+=dp[i][j]*i/(i+j); - } the } *printf"%.9f\n", ans); $ Panax Notoginseng return 0; -}
View Code
CF 148d BAG Of Mice probability dp good question