hdu-1204-Candy War (heard Markov process! )

Source: Internet
Author: User

Candy WarsTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 2295 Accepted Submission (s): 787


Problem description Birthday party at the end of the night, left some candy, Gandon want to take all of them, Speakless said: "Can be, but we play 24 points, you have already got some sweets?" In this way, if someone wins a game, they take a sugar until they have finished all the sugar. "If anyone can figure out and the other person does not come out, who will win, but if both sides can work out or neither, even if the draw, there will be no loss of candy."
Speakless is a person who likes to think ahead of time, since he launched the Candy War, he naturally wants to win (otherwise it will be stripped of-_-). Now he needs your help, give you the probability of winning each game and Gardon the probability of winning each game, please give him the probability that he might win the battle.

Input each line has four number, speakless hand candy number N, Gardon hand candy number M (0&LT;=N,M&LT;=50), a game speakless can solve the probability p, a question Gardon can answer the probability q (0<=p, Q<=1).

Output is one number per line, indicating the probability that speakless will win (in percent, reserved to 2 digits 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

Authorspeakless
Sourcegardon-dygg Contest 2
recommendjgshining | We have carefully selected several similar problems for you:1239 1730 1579 1207 1050

In fact, this problem is beyond the scope of my problem solving and knowledge system ... At the beginning of the analysis, someone mentioned Markov process. Check it out, it's almost confused, it seems to be a certain nature or theorem about the theory of generality .... Anyway, I didn't read it. But a god Ox uses high school math to analyze the problem ... No, oier study together! Thanks to He Xiu for sharing his problem solving skills!

This is a probability problem, first of all we must understand what we ask for!


Set F (i) to indicate the probability of winning when the speakless has I candy, we ask for f (n)
Then according to test instructions we know that at this time:
1.Speakless the probability of winning this game is P (1-q), i.e. f (i) becomes f (i+1)
2.Speakless the probability of this inning is Q (1-p), i.e. f (i) becomes f (i-1)
3.Speakless the probability of this inning is 1-p (1-Q)-Q (1-p), i.e. f (i) becomes f (i)
So:
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 there is P (1-q) *g (i) = Q (1-p) g (i-1), i.e. g (i) is geometric series,
Set K=q (1-p)/(P (1-q)), 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+m-1)
Add each of 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 beginning of the definition, we know F (0) =0 (said to have lost), F (n+m) =1 (said to have won)
G (1) =f (1)-f (0) =f (1)
So 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 ask for f (n), in the (2) formula, as long as f (1) is unknown, therefore need to be more (1) first to find out F (1). Final f (n) = (1-k^n)/(1-k^ (m+n)) Several places to note: n==0, m==0, p==0, q==0, p== Q Concentration special case!


C + + code:
#include <iostream> #include <iomanip> #include <cmath>using namespace Std;int main () {    int n,m;    Double p,q,rate,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 (q==0| | p==1) {cout<< "1.00" <<endl;continue;}        if (p==q) rate=1.0*n/(m+n);   M,n is not necessarily equal to 0.5        else        {            k = q* (1-p)/(p* (1-Q));            Rate = (1.0-pow (k,n))/(1.0-pow (K,m+n));      "Power operation"        }        cout<<fixed<<setprecision (2) <<rate<<endl;    "Set the number of significant digits for floating-point output"    }    return 0;


Java code:
Import Java.io.*;import java.util.*;p ublic class main{public static void Main (string[] args) {//TODO auto-generated Metho D stubscanner input = new Scanner (system.in), while (Input.hasnext ()) {Double N = input.nextdouble ();//speakless The number of sweets on hand ndo Uble M = input.nextdouble (); Gardon number of candies on hand mdouble p = input.nextdouble (); Speakless can solve the probability pdouble q = input.nextdouble (); Gardon can solve the probability qif (N = = 0) {System.out.println ("0.00"); continue;} if (M = = 0) {System.out.println ("1.00"); continue;} if (p = = 0 && q = = 1) {System.out.println ("0.00"); continue;} if (p = = 1 && q = = 0) {System.out.println ("1.00"); continue;} if (p = = q) {System.out.printf ("%.2f", N/(n + M)); System.out.println ();} else{double k = q * (1-p)/(p * (1-q));d ouble rate = (1-math.pow (k, N))/(1-math.pow (k, n + M)); System.out.printf ("%.2f", rate); System.out.println ();}}}


Attach the author's original address: Http://acm.hdu.edu.cn/discuss/problem/post/reply.php?postid=13123&messageid=1&deep=0

hdu-1204-Candy War (heard Markov process! )

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.