### otero1991's blog

By otero1991, 12 years ago,

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

• 0

 » 12 years ago, # | ← Rev. 3 →   +6 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.
•  » » 12 years ago, # ^ |   0 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