CF 148D. Bag of mice[probability DP]

Source: Internet
Author: User

Title Link: Http://codeforces.com/problemset/problem/148/D

The main topic: a bag of W White mouse, b black Mouse; A and b take the mouse in turn (not put back), who first caught microscope, who win; because B rude, every time a mouse, will run out of a first;

The probability of seeking a win;

Topic Analysis:

This kind of probability DP state is fixed, DP (i, j) indicates the probability of the current state awin;

The probability of 1:dp[i][0],a win is 1;dp[0][j] probability is 0;

2:DP[I][J] The following four types of transfers:

If a takes white, the probability is i/(i+j),

If a takes black, B takes white, the probability is 0,

If a takes black, B takes black, runs out black probability 1.0*j/(i+j) * (j-1.0)/(i+j-1.0) * (j-2.0)/(i+j-2) *dp[i][j-3]; Premise: Black >=3

If a takes black, B takes black, runs out white probability 1.0*j/(i+j) * (j-1.0)/(i+j-1.0) *i/(i+j-2) *dp[i-1][j-2]; Premise: White >=1; black >=2

Code:

author:acsorry//result:yes#include<iostream> #include <cstdio> #include <cstring> #include < cmath> #include <algorithm> #include <vector> #include <string> #include <queue> #include <deque> #include <stack> #include <map> #include <set> #define INF 1<<29#define maxInt 0x7fffffff#define SUP 0x80000000#define Mem (A, B) memset (A,b,sizeof (a)) using namespace Std;typedef long long ll;const int    N=1007;double Dp[n][n];int Main () {int w,b;        while (cin>>w>>b) {dp[0][0]=0; for (int i=1;i<=w;i++) dp[i][0]=1.0;//only the white mouse for (int i=1;i<=b;i++) dp[0][i]=0.0;//only black rat 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]+=1.0*j/(i+j) * (j-1.0)/(i+j-1.0) * (j-2.0)/(i+j-2) *dp[i][j-3];            if (j>=2) dp[i][j]+=1.0*j/(i+j) * (j-1.0)/(i+j-1.0) *i/(i+j-2) *dp[i-1][j-2];   }     } printf ("%.9f\n", Dp[w][b]); } return 0;}



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