Problem ACodeForces 148D probability dp
W white rats and B black rats are in the bag. The dragon and Princess caught the mouse in the bag in turn. Whoever catches the white teacher first wins. Each time the princess captures a mouse, each time the dragon catches a mouse, a mouse will run out. Every time a mouse is caught and the mouse that runs out is random. If neither of them caught a white mouse, the dragon wins. The princess first captured it. Ask the princess about the probability of winning.
I have done so many probability dp questions, and I have answered them almost, but the result is still not ......
The following describes the ideas of others.
Win [I] [j] = I * 1.0/(I + j); // when I am a white mouse, j is a black mouse, the Princess chooses a white mouse.
Win [I] [j] + = lost [I] [J-1] * j * 1.0/(I + j ); // I only white mouse j only black mouse when the princess chooses black mouse, but after the princess chooses the black mouse, the Dragon still loses
Lost [I] [j] = j * 1.0/(I + j) * win [I-1] [J-1] * (I * 1.0/(I + j-1 )); // when I only have a white mouse, j only has a black mouse, the dragon chooses a black mouse.
Lost [I] [j] + = j * 1.0/(I + j) * win [I] [J-2] * (j-1) * 1.0/(I + j-1); // when I am a white mouse, j is a black mouse, the dragon selects the black mouse. After the selection, the mouse jumps out and only the black mouse
Description
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 shoshould 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 containsWWhite andBBlack 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 integersWAndB(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 exceed10? -? 9.
Sample Input
Input
1 3
Output
0.500000000
Input
5 5
Output
0.658730159
Hint
Let's go through the first sample. the probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. the probability of the dragon drawing a black mouse and not winning on his first turn is 3/4*2/3 = 1/2. after this there are two mice left in the bag-one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. if the princess 'mouse is white, she wins (probability is 1/2*1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
# Include
# Include
# Include
Using namespace std; double win [1100] [1100], lost [1100] [1100]; int main () {int w, B; while (~ Scanf ("% d", & w, & B) {memset (win, 0, sizeof (win); memset (lost, 0, sizeof (lost); for (int I = 1; I <= w; ++ I) win [I] [0] = 1.0; for (int I = 1; I <= w; I ++) for (int j = 1; j <= B; j ++) {win [I] [j] = I * 1.0/(I + j) + lost [I] [J-1] * j * 1.0/(I + j ); // This is his current win or this time does not win, but the next time the dragon loses, he will surely win the lost [I] [j] = j * 1.0/(I + j) * win [I-1] [J-1] * I * 1.0/(I + J-1); // This time the dragon lost must have chosen black, therefore, we need to win the next time the princess wins, and the probability of running white or black lost [I] [j] + = j * 1.0/(I + j) * win [I] [J-2] * (J-1) * 1.0/(I + J-1);} printf ("%. 9lf \ n ", win [w] [B]);} return 0 ;}