HDU1204 candy wars (probability theory)

Source: Internet
Author: User

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

Click to open the original question link

Question Analysis: make f (I) the probability of the I-th candy of Speakless;

P (1-q) indicates that the probability of winning Speakless is f (I + 1 );

Q (1-p) indicates that the probability of Speakless input is f (I-1 );

1-p (1-q)-q (1-p) indicates that the probability of a draw is f (I );

Therefore:


F (I) = p (1-q) * f (I + 1) + q (1-p) * f (I-1) + (1-p (1-q)-q (1-p )) * f (I)
Slightly deformed:
P (1-q) * (f (I + 1)-f (I) = q (1-p) * (f (I)-f (I-1) Order g (I) = f (I)-f (I-1 ),
Then p (1-q) * g (I) = q (1-p) g (I-1), that is, g (I) is an proportional series,
Set k = q (1-p)/(p (1-q), then g (I) = k * g (I-1) g (1) = f (1) -f (0)
G (2) = f (1)-f (0)
...
G (n) = f (n)-f (n-1)
...
G (n + m) = f (n + m)-f (n + s-1)
Add the above equations: g (1) + g (2) +... + g (n + m) = f (n + m)-f (0) = 1
G (1) + g (2) +... + g (n + m) = g (1) * (1-k ^ (n + m)/(1-k)
G (1) + g (2) +... + g (n) = g (1) * (1-k ^ (n)/(1-k)
Back to the definition, we know that f (0) = 0 (indicating that it has lost), f (n + m) = 1 (indicating that it has won)
G (1) = f (1)-f (0) = f (1)
Therefore, g (1) + g (2) +... + g (n + m) = f (1) * (1-k ^ (n + m)/(1-k) = 1 ...................................... ...... (1)
G (1) + g (2) +... + g (n) = f (1) * (1-k ^ (n)/(1-k) = f (n )................................... ................ (2)

We require f (n). In formula (2), as long as f (1) is unknown, We need to (1) first find f (1 ). final f (n) = (1-k ^ n)/(1-k ^ (m + n ))

Note the following special cases: N = 0, M = 0, p = 0, q = 0, p = q!


 

# Include <iostream> # include <iomanip> # include <cmath> using namespace std; int main () {int n, m; double p, q, ans, k; while (cin> n> m> p> q) {if (n = 0) {cout <"0.00" <endl; continue ;} if (m = 0) {cout <"1.00" <endl; continue;} if (p = 0 | q = 1) {cout <"0.00" <endl; continue;} if (p = 1 | q = 0) {cout <"1.00" <endl; continue;} if (p = q) ans = 1.0 * n/(n + m); else {k = q * (1-p)/(p * (1-q )); ans = (1.0-pow (k, n)/(1.0-pow (k, n + m);} cout <fixed <setprecision (2) <ans <endl;/<SPAN style = "FONT-SIZE: 10px"> * setprecision </SPAN> controls the number of floating point numbers displayed in the output stream, if it is used with fixed, you can control the number of digits on the right of the decimal point header file to # include <iomanip> */} return 0 ;} # include <iostream> # include <iomanip> # include <cmath> using namespace std; int main () {int n, m; double p, q, ans, k; while (cin> n> m> p> q) {if (n = 0) {cout <"0.00" <endl; continue ;} if (m = 0) {cout <"1.00" <endl; continue;} if (p = 0 | q = 1) {cout <"0.00" <endl; continue;} if (p = 1 | q = 0) {cout <"1.00" <endl; continue;} if (p = q) ans = 1.0 * n/(n + m); else {k = q * (1-p)/(p * (1-q )); ans = (1.0-pow (k, n)/(1.0-pow (k, n + m);} cout <fixed <setprecision (2) <ans <endl;/* setprecision controls the number of floating point numbers in the output stream. If it is used with fixed, you can control the number of digits on the right of the decimal point. The header file is # include <iomanip> */} return 0 ;}


 

Related Article

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.