Cf d. Bag of mice (probability dp)

Source: Internet
Author: User

Cf d. Bag of mice (probability dp)

 

Here there are w white rats and B black rats. Dragon and Princess Wang take turns to capture the rats from the bag. Each time they capture one, the first white rat wins. When a dragon catches a mouse, the bag will run away from a mouse. The mouse that runs away is equal to probability. Ask Wang's probability of victory.

 

The probability of Princess Wang's victory in the presence of j black rats is dp [I] [j], which can be obtained from the following three States:

Wang Yu won a white rat for the first time with the probability of I/(I + j );

Princess did not get the white mouse, the probability of taking the black rat is j/(I + j), if the princess to win, the next time the dragon must take the black mouse, the probability is (J-1) /(I + J-1) while escaping is a black rat, probability is (J-2)/(I + J-2), status is transferred to dp [I] [J-3];

Princess did not get the white mouse, the probability of taking the black rat is j/(I + j), if the princess to win, the next time the dragon must take the black mouse, the probability is (J-1) /(I + J-1), also run away is the white mouse, probability is I/(I + J-2), the State is transferred to dp [I-1] [J-2];

 

In summary, dp [I] [j] = I/(I + j) + j/(I + j) * (J-1)/(I + J-1) * (J-2) /(I + J-2) * dp [I] [J-3] + j/(I + j) * (J-1)/(I + J-1) * I/(I + J-2) * dp [I-1] [J-2].

 

 

#include 
 
  #include 
  
   #include
   #include 
    
     #include 
     
      #include 
      
       #include 
       
        #include 
        
         #include 
         
          #include 
          
           #include 
           
            #include 
            
             #include //#define LL __int64#define LL long long#define eps 1e-12#define PI acos(-1.0)using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 4010;double dp[1010][1010];int main(){int w,b;while(~scanf(%d %d,&w,&b)){for(int i = 1; i <= w; i++)dp[i][0] = 1;for(int i = 0; i <= b; i++)dp[0][i] = 0;for(int i = 1; i <= w; i++){for(int j = 1; j <= b; j++){dp[i][j] = i*1.0/(i+j);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];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];}}printf(%.9lf,dp[w][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.