Code forces 148d bag of mice (probability DP)

Source: Internet
Author: User

Time limit per Test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

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


A bag contains W white mice and B black mice. Wang Yu and the dragon are taken in sequence. Wang Yu gets the white mouse first.

The winner. When the Dragon gets a mouse, a random mouse will be taken out and the mouse's

The probability of each mouse is the same, and the probability of the mouse running is the same, so that you can calculate the probability of Wang's victory.


Idea: DP [I] [J] indicates the probability that Princess Wang wins when white mouse is I and black mouse is J,

There are four statuses:

(1) Wang got the white mouse. DP [I] [J] + = I/(I + J );

(2) Wang got the rat and the dragon got the mouse. DP [I] [J] + = 0.0;

(3) Wang got the chinanager, the dragon got the chinanager, and ran out a black rat. DP [I] [J] + = J/(I + J) * (J-1) */(I + J-1) * (J-2) */(I + J-2) * DP [I] [J-3];

(4) Wang got the rat, and the dragon got the rat, and ran out a white mouse. DP [I] [J] + = J */(I + J) * (J-1) */(I + J-1) * I */(I + J-2) * DP [I-1] [J-2];


#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn=1100;double dp[maxn][maxn];int n,m;int main(){    while(scanf("%d %d",&n,&m)!=EOF)    {        memset(dp,0,sizeof(dp));        for(int i=1; i<=n; i++)  dp[i][0]=1.0;        for(int i=1; i<=n; i++)            for(int j=1; j<=m; j++)            {                dp[i][j]+=(i*1.0)/(i+j);                if(j>=3)   dp[i][j]+=j*1.0/(i+j) * (j-1)*1.0/(i+j-1) * (j-2)*1.0/(i+j-2) * dp[i][j-3];                if(j>=2)   dp[i][j]+=j*1.0/(i+j) * (j-1)*1.0/(i+j-1) * i*1.0/(i+j-2) * dp[i-1][j-2];            }        printf("%.9lf\n",dp[n][m]);    }    return 0;}


Code forces 148d bag of mice (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.