The codeforces-148D-Bag of mice-probability DP

Source: Internet
Author: User

DP [x] [Y]: Now there are X white mice, y black mice, and the probability that the princess will win.

So:

Assume that the princess gets the white mouse directly. The probability is X/(x + y), and the princess wins.

Assuming that the princess gets the black mouse, the probability is Y/(x + y), then the princess assumes that to win, the dragon must get the black mouse, the probability is (Y-1)/(x + Y-1 );

Then the probability of the escaped mouse being a black mouse is (y-2)/(x + y-2), the probability of a white mouse being (X)/(x + y-2 );

So DP [x] [Y] = x/(x + y) + Y/(x + y) * (Y-1)/(x + Y-1) * (y-2) /(x + y-2) * DP [x] [Y-3] + (X)/(x + y-2) * DP [x-1] [y-2]);

You can also perform deep-search with memory, and perform recursive DP directly.

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<stdlib.h>#include<vector>#include<cmath>#include<queue>#include<set>#include<stack>using namespace std;#define N 100010#define LL long long#define INF 0xfffffff#define maxn 1100double dp[maxn][maxn];double dos(int x,int y){    if(x<0||y<0)return 0;    if(x==0)return 0;    if(y==0)return 1;    if(dp[x][y]>-0.5)return dp[x][y];    double a,b;    a=1.0*x;    b=1.0*y;    dp[x][y]=a/(a+b);    if(x+y>=3)    {        dp[x][y]+=(b*(b-1)/((a+b)*(a+b-1)))*        (dos(x-1,y-2)*a/(a+b-2)+dos(x,y-3)*(b-2)/(a+b-2));    }  //  cout<<x<<" "<<y<<" "<<dp[x][y]<<endl;    return dp[x][y];}int main(){    int a,b;    while(~scanf("%d%d",&a,&b))    {        memset(dp,-1,sizeof(dp));        printf("%.10lf\n",dos(a,b));    }    return 0;}


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.