Problem acodeforces 148D probability DP

Source: Internet
Author: User

Test instructions: There are w white rats and B black rat in the bag. The Dragon and the princess took turns to catch the mouse from the bag. Whoever catches the white teacher wins first. Every time the princess catches a mouse, the dragon will have a mouse run out after grasping a mouse. Every time I catch a mouse and run out of mice are random. If two people have not caught the white mouse then the dragon wins. The princess catches first. Ask the Princess the probability of winning.

Do so many of the probability of DP problem, the original answer is almost, the result of a do or not ...

Here's a look at other people's ideas.

WIN[I][J] = i * 1.0/(i + j); I white mouse J only black mouse when the princess selected white mouse
WIN[I][J] + = lost[i][j-1] * J * 1.0/(i + j); I white mouse J only black mouse when the princess chose Black Mouse, but the princess chose the Black Mouse after the dragon still lost
LOST[I][J] = J * 1.0/(i + j) * Win[i-1][j-1] * (I * 1.0/(i + j-1)); I white mouse J black Mouse when the dragon selected black mice, after the election to jump out of a white mouse
LOST[I][J] + = J * 1.0/(i + j) * Win[i][j-2] * ((J-1) * 1.0/(i + j-1)); I white mouse J black Mouse when the dragon selected black mice, after the election to jump out of a black mouse

Description

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 the 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 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 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.


#include <cstdio> #include <iostream> #include <algorithm> #include <cstring>using namespace Std;double Win[1100][1100],lost[1100][1100];int Main () {    int w,b;    while (~SCANF ("%d%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 win this time or this time not win but the next dragon lose to the end must win          lost[i][j]=j*1.0/(i+j) *win[i-1][j-1]*i*1.0/(i+j-1);//The Dragon this time lost must be selected black, So to use the next princess to win on the match to run white or black probability          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;}


Problem acodeforces 148D probability DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.