I have found this problem at the Gym:

Two counterfeiters each made a coin: one of these coins has the probability of getting heads p%, and another one q%. For some reason the counterfeiters bet that if one chooses the random of these two coins, he gets heads two times in a row. They do so: choose the random coin and throw it. At the 1rst throw they really got heads. Now they want to know: what is the probability to getting heads at the second throw of the chosen coin?

Sample Test:

Input: 50 50 Output: 0.500000000000000

Input: 33 66 Output: 0.550000000000000

Input: 80 40 Output: 0.666666666666667

Does anyone could help me with this problem??

How can I find solutions ans codes for problems from the gym????

This problem looks simple, you are just think "step by step":

But let us measure probabilities as number from 0 to 1, not in percents. Let the first coin give heads with prob P and second with Q.

At first, one of two coins is chosen (with even probability of 0.5) and thrown. There could be four opportunities:

it was the first coin (0.5) and it yields heads with P — totally it gives 0.5*P chance;

it was the first coin (0.5) and it yields tails (1 — P) — totally it gives 0.5*(1-P);

the same for second with heads — 0.5*Q;

and same for second with tails — 0.5*(1-Q).

It is told that coin gives heads at thirst throw. Therefore it was either 1-st or 3-rd chance, but not 2-nd or 4-th.

Total probability of 1-st or 3-rd chance is 0.5*(P + Q). But if we know not which coin was chosen, what could it be?

It could be first coin with probability 0.5*P / (0.5*(P + Q)) = P / (P+Q) — or the second with probability ... Q / (P+Q).

Nice. Now what is the probability to get the second head?

If it is the first coin (which could happen with prob P/(P+Q)) then we can get coin with prob P — totally it would be P * P/(P+Q). Just the same if it is the second coin — total probability of second head with it is Q * Q/(P+Q).

Now the sum chance of getting second head is just the sum of these two: (P*P + Q*Q) / (P + Q) (When grouped with common denominator)

For example with your 3-rd sample: (0.8*0.8 + 0.4*0.4) / (0.8 + 0.4) = 0.6666...

If it is not easy to comprehend (sorry) just write a simple modelling program which chooses randomly one of two coins, throws it for the first time and if it gives tails does not record the result (and starts the next iteration), but if it gives head — then cast it second time and count, whether you get second head or no.

I can't understand this part:

"Total probability of 1-st or 3-rd chance is 0.5*(P + Q). But if we know not which coin was chosen, what could it be? It could be first coin with probability 0.5P / (0.5(P + Q)) = P / (P+Q) — or the second with probability ... Q / (P+Q)"

Would you please explain it. Thanks in advance

Look, we choose one of two coin at random and then throw it and after seeing it gives heads up, we are wondering what is the probability of this coin being either first or second.

Let us suppose, for example, P=0.3 (heads for first coin) and Q=0.4 (heads for second coin).

Imagine that we do this experiment 100 times. How much different results there would be:

in about 50 cases we will choose 1st coin — and among them we have about 0.3*50=15 heads and 50-15=35 tails;

in about 50 cases we will chose 2nd coin — and among them we have 0.4*50=20 heads and 50-20=30 tails.

Now what is the "probability" we want? It is the ratio:

Presult = (successful cases) / (total cases)

But what are "successful" and "total" cases in our problem?

"Total" are all cases when the randomly chosen coin was tossed heads up. We have 35 such cases (it is whole number of experiments multiplied by Ptotal=0.5*P + 0.5*Q obviously).

"Successful" are all cases among these "Total" in which 1st coin was chosen and thrown (if we are seeking probability for 1st coin, of course).

We have 15 such cases (i.e. whole number of experiments multiplied by Psucc1st=0.5*P).

So the probability that the first coin was chosen if we know was tossed heads up is 15/35, or more strictly: Psucc1st / Ptotal = 0.5*P/(0.5*P+0.5*Q) = P/(P+Q).

The same for second coin: Psucc2nd / Ptotal = 0.5*Q/(0.5*P+0.5*Q) = Q/(P+Q).

I fear I could not explain it better. I fear also my English is not good enough to make myself clear. ;-)

UPD: Hey, I remember there is very old puzzle of this kind, but somewhat simpler. Suppose we know that The President of Ruritania have two children, but do not know their sex. One day paparazzi make shots of President's son visiting night club. Your colleague tells you "let's have a bet that other President's child is a girl" — is such a bet fair (i.e. are the probabilities to win or lose equal?)

You see, there were 4 variants in which two children could be born in order — boy and boy, boy and girl, girl and boy, girl and girl. However you now know that last variant is out of consideration. So we have 3 variants and in 2 of them your colleague win your money!

If you will not think of it, you may wrongly suppose that probability is 1/2 (other children is either girl or boy)...

Thanks a lot!!