Coderforce 148d-bag Of Mice (probability DP seeking probability)

Source: Internet
Author: User

The theme: Beauty and the Beast are playing the game of painting pigeons. Pigeons in the cage covered with black cloth, white w only, Black has B, each time to take out a painting, who first painted white pigeons who will win. Beauty first draw, because the beast is too ugly, it every time the painting will scare away a pigeon, all the pigeons out of the cage are not in. Ask beauty to win probability. (Set if no one draws the white dove, count the Beast to win).

Problem Analysis: This problem is not difficult, it is obvious that the probability of DP. This is my first time to write a probability dp, to commemorate ...

The code is as follows:

# include<iostream># include<cstdio># include<vector># include<list># include<queue># include<cstring># include<set># include<map># include <string># include<cmath># include<algorithm>using namespace std;double dp[1005][1005];d ouble DP (    int W,int b) {if (dp[w][b]!=-1.0) return dp[w][b];    if (w<=0) return dp[w][b]=0.0;    if (b<=0) return dp[w][b]=1.0;    Double res1= (Double) w/(double) (w+b);    Double res2= (double) b/(double) (w+b)) * ((double) (b-1)/(double) (w+b-1));    Double t=0.0;    if (b-2>0) t+= ((double) (b-2)/(double) (w+b-2)) *DP (w,b-3);    if (w-1>0) t+= ((double) (w)/(double) (w+b-2)) *DP (w-1,b-2);    res2*=t; return dp[w][b]=res1+res2;}    int main () {int w,b;                while (~SCANF ("%d%d", &w,&b)) {for (Int. i=0;i<=w;++i) for (int j=0;j<=b;++j)        dp[i][j]=-1.0;    printf ("%.9lf\n", DP (w,b)); } return 0;} 

  

Coderforce 148d-bag Of Mice (probability DP seeking probability)

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.