Hdu 1204 candy war (expectation of Markov Chains)

Source: Internet
Author: User

Hdu 1204 candy war (expectation of Markov Chains)

Candy wars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 2336 Accepted Submission (s): 797


Problem Description on the evening of the end of the birthday Party, there were some sweets left, And Gandon wanted to take all of them away. Speakless said, "Yes, but we can play around for 24 o'clock, didn't you get some candy? In this way, if anyone wins a game, they will take the sugar from the other party until all the sugar from the other party is obtained ." If no one can calculate and the other party cannot calculate, no one will win, but if both parties can calculate or cannot, even if the draw is made, there will be no gains or losses of candy.
Speakless is a person who prefers to think about problems in advance. Since he initiated the candy war, he naturally wants to win (or else he will be able to win -_-). Now he needs your help to give you the probability of winning each game and the probability of winning each game by Gardon. Please give him the probability of winning this war.

Each Input row has four numbers, the number of sweets on the Speakless hand N, and the number of sweets on the Gardon hand M (0 <= N, M <= 50) probability p that can be answered by a single Speakless, and the probability q That Gardon can answer (0 <= p, q <= 1 ).

One number per line in Output indicates the probability that Speakless can win (expressed in percentage, which is retained to the second digit after the decimal point ).

Sample Input
50 50 0.5 0.510 10 0.51 0.550 50 0.51 0.5

Sample Output
0.500.600.88


F [I] indicates the probability that Speakless will win when I have a candy, then f [I] = p (1-q) * f [I + 1] + q (1-p) * f [I-1] + (1-p * (1-q)-q * (1-p) * f [I]; this equation indicates that the expected result of the current State is equal to the sum of the expected results of all States that the current State can reach. Simplified, (f [I + 1]-f [I-1]) * p * (1-q) = f [I]-f [I-1] * (q * (1-p ));
K = (q * (1-p)/(p * (1-q ));
Set f [I + 1]-f [I] = (f [I]-f [I-1]) * k;

F [0] = 0; f [n + m] = 1; (mandatory and mandatory)

F [1]-f [0] = f [1]; f [2]-f [1] = (f [1]) * k; f [3]-f [2] = (f [2]-f [1]) * k = f [1] * k ^ 2 ;... f [n]-f [n-1] = (f [n-1]-f [n-1]) * k = f [1] * k ^ (n-1); summation of the series, f [n] = (1-k ^ n)/(1-k); f [n + m] = (1-k ^ (n + m)/(1-k) = 1;
Then f [n] = f [n]/f [n + m] = (1-k ^ n)/(1-k ^ (n + m ));

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
Using namespace std; # define ll long # define N 25 const int M = 256; const double eps = 1e-4; int main () {int n, m, n1, n2, i; double k, s, p, q; while (~ Scanf ("% d % lf", & n, & m, & p, & q) {if (m = 0) s = 1; else if (n = 0 | p = 0 | q = 1) s = 0; else {k = q * (1-p)/p/(1-q ); if (fabs (k-1.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.