Lightoj-1050-marbles (DP)

Source: Internet
Author: User

1050-marbles
PDF (中文版) statisticsforum
Time Limit:2 second (s) Memory limit:32 MB
Your friend Jim has challenged a game. He has a bag containing red and blue marbles. There'll be a odd number of marbles in the bag, and you go first. On your turn, your reach into the bag and remove a random marble from the bag; Each marble is selected with equal probability. After your turn was over, Jim would reach into the bag and remove a blue marble; If there is no blue marble for Jim to remove and then he wins. If the final marble removed from the bag was blue (by your or Jim), you'll win. Otherwise, Jim wins.

Given the number of the red and blue marbles in the bag, determine the probability so you win the game.

Input
Input starts with an integer T (≤10000), denoting the number of test cases.

Each case begins with integers R and B denoting the number of red and blue marbles respectively. You can assume this 0≤r, b≤500 and R+b is odd.

Output
For each case of input, the case number and your winning probability are in print. Errors less than 10-6 'll be ignored.

Sample Input
Output for Sample Input
5
1 2
2 3
2 5
11 6
4 11
Case 1:0.3333333333
Case 2:0.13333333
Case 3:0.2285714286
Case 4:0
Case 5:0.1218337218

Problem-solving ideas: Take dp[r][b] as the state equation, through observation found, but r>=b, Dp[r][b] must be 0

When r<b we can find dp[r][b] = r/(r+b) *dp[r-1][b-1] + b/(r+b) *dp[r][b-2];

The DP equation can be pretreated according to these two states.

#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>using namespacestd;Doubledp[ -][ -];voidinit () { for(intI=1;i<510; i++) dp[0][i] =1;  for(intI=1;i<510; i++){         for(intj=i+1;j<510; j + +) {Dp[i][j]= (Double) i*1.0/(I+J) *dp[i-1][j-1] + (Double) j*1.0/(I+J) *dp[i][j-2]; }    }    return ; }intMain () {init (); intt,r,b; scanf ("%d",&T);  for(intt=1; t<=t;t++) {scanf ("%d%d",&r,&b); printf ("Case %d:%.6lf\n", T,dp[r][b]); }    }

Lightoj-1050-marbles (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.