Cf148d--bag of mice+ probability expectation DP

Source: Internet
Author: User

First probability expectation DP:)

In fact, and the general DP is similar, as long as the status of the election is OK.


Definition dp[i][j] means I have only white mouse J Black Mouse when the princess won the probability.

Then: 1. Princess Choose white Mouse, direct win, probability: i/(I+J)

2. The princess chose the black Mouse

1) The Dragon chose the black Mouse, fled the black mouse; probability: j/(i+j) * (j-1)/(i+j-1) * (j-2)/(I+j-2)

2) Dragon Choose Black Mouse, Escape white mouse; probability: j/(i+j) * (j-1)/(i+j-1) *i/(i+j-2)

3) Long Mouse, so the princess will lose, do not consider

Then dp[i][j] equals the sum of the above probabilities.

When initialized, the probability of winning is 1 if only microscope

If the black mouse wins, the odds are 0.



The code is as follows:


#include <iostream> #include <cstdio> #include <cstring>using namespace std;double dp[1100][1100]; int main () {    int w,b;    while (scanf ("%d%d", &w,&b)!=eof)    {for        (int i=1;i<=w;i++)           dp[i][0]=1;        for (int j=1;j<=b;j++)           dp[0][j]=0;        for (int i=1;i<=w;i++) for          (int j=1;j<=b;j++)          {              dp[i][j]=1.0*i/(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[w][b]);    }  return 0;}



Cf148d--bag of mice+ probability expectation 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.