Codeforces Round #105 D. Bag of mice probability dp,

Source: Internet
Author: User

Codeforces Round #105 D. Bag of mice probability dp,

Http://codeforces.com/contest/148/problem/D

The question is that dragon and Princess take turns pumping mice from the bag, W white teachers in the bag, and D black teachers. The Princess smokes first, and the first pulls the white mouse to win, the dragon will randomly run out of a mouse each time it is pumped. Give W and D for you to find the probability of the princess's victory.

For dp [w] [d], it indicates that there are w white mice, d black mice, and the probability of the princess's victory, if the princess pulls out the white mouse for the first time, the probability is w/(w + d). If the princess fails to win the princess, and the dragon cannot win the White Rat, rec = d/(w + d) * (D-1)/(w + D-1 ). The mouse that runs out may be white or black. If the mouse is white, then the W-1 is left white mouse D-2 only black mouse, then princess first hand, probability is rec * w/(w + D-2) * dp [W-1] [D-2]. If it is a black rat, then the remaining w white mouse D-3 only black rat, the same princess first hand, the probability is rec * (D-2)/(w + D-2) * dp [w] [D-3];

 

Finally, the dp transfer equation is obtained:

Dp [I] [j] = I/(I + j) + rec * (I/(I + J-2) * dp [I-1] [J-2] + (J-2) /(I + j-2) * dp [I] [J-3]);

Rec = j/(I + j) * (j-1)/(I + j-1 );


/*********************************************** ** problem ID    : cf_#105D.cpp ** create time    : Wed Jul 22 20:22:24 2015 ** auther name    : xuelanghu ** auther blog    : blog.csdn.net/xuelanghu407 **********************************************/#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int n, m;double dp[1010][1010];int main () {        cin >> n >> m;        for (int i=0; i<=n; i++) {        dp[i][0] = 1.0;    }    for (int i=0; i<=m; i++) {        dp[0][i] = 0.0;    }        for (int i=1; i<=n; i++) {        dp[i][1] = (double)i / (i+1);        for (int j=2; j<=m; j++) {            dp[i][j] = (double)i / (i+j);            double tmp = (double)j* (double)(j-1)  / (i+j) / (i+j-1);            if (j>2) dp[i][j] += tmp * (double)(j-2) / (i+j-2) * dp[i][j-3];              dp[i][j] += tmp * (double)i / (i+j-2) * dp[i-1][j-2];        }    }        printf("%.10lf\n", dp[n][m]);        return 0;}code


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.