Stumped by programming competition training problem

Status
Not open for further replies.

lensam69

Ars Scholae Palatinae
746
I came across this problem in a competition training list, but I truly don't understand the expected answer.

http://www.codeforces.com/problemset/problem/98/B

To summarize for the TL;DR crowd, one person among n must be chosen using a coin toss, in such a way that everyone has the same probability of being chosen.

What's the expected number of coin tosses (As an irreducible fraction) needed to choose the winner.

The provided samples are:

Input: 2 Output: 1/1

Input: 3 Output: 8/3

Input: 4 Output: 2/1


How is this solved for the n=3 case? Don't understand the 8/3 answer here...
Statistics > Me

TIA
 
To choose one out of 3 with equal probability you'll have 'rounds' of 2 coin tosses each. The outcome of a round will be to choose the first, second or third option, or to go onto another round. In each round the 3 choices have an equal probability of 1/4, and overall they each have a probability of 1/3. The expected number of coin tosses is the sum of the number of coin tosses in each round multiplied by the probability of reaching that round.

So with 3 people you have a 100% chance of two coin tosses in the first round. You have a 1/4 chance in reaching the second round and adding another two coin tosses, a 1/16 chance of reaching the third round, etc. This is sum 2/4^i, i=0 to infinity which is 8/3.
 
Yes. To choose with equal probability using coin tosses you have to 'pad' N up to the nearest power of 2, where the added outcomes are "this round doesn't count." Take another example, N=5. Pad N up to 8 so in each round every participant has a 1/8 chance of being picked and there's a 3/8 chance of going onto another round.

Also note that for N=5 the number of coin tosses in a round varies, so you have to calculate the expected number of tosses per round. For optimal coin tossing you have 3/4 chance of 3 tosses + 1/4 chance of two tosses, which yields an expected number of tosses per round of 11/4. The total expected number of tosses is then sum (11/4)*(3/8)^i, i=0 to infinity.
 

lensam69

Ars Scholae Palatinae
746
Ok, I think I'm getting this.

So in the case of N=5, I would have 5 people + 3 blank spots

1,2,3,4,5,0,0,0

Choosing a winner in this round would mean picking a position. which could be done as a binary search of sorts (Separate in two halves, discard one half with each coin toss?) Meaning that it would take log2(8)=3 coin tosses to pick a winner, but there is a 3/8 chance that said winner could be a blank, meaning that the game needs to be played again.

How did you get to 3/4 P * 3 tosses + 1/4 P * 2 tosses?
 
Say on the first toss you rule out 1,2,3, and 4 for this round. You're left with 5,0,0,0. Then on the next toss you rule out 5, and the first zero, and are left with the last two zeros. Since no matter which way the third toss comes out you get the same result, you don't need to actually make the third toss for this round. The round is over after two tosses and you can move directly to the next round. There's a 1/4 chance that you get to that point, and a 3/4 chance that you don't and have to go ahead an make a third toss.

Take as another example N=9. There are 7 'play again' outcomes per round. The first toss might rule out 1 through 8, and another toss might rule out 9 and three zeros, leaving you with 4 zeros. So there's a 1/4 chance that you're done after 2 tosses. It might also be that the second toss rules out those 4 zeros and leaves you with the 9 and three zeros. Then the third toss might rule out the 9 and a zero, leaving you with 2 zeros. So there's a 1/8 chance you're done after three tosses. In all other cases you need 4 tosses. So the expected number of tosses is 1/4 * 2 + 1/8 * 3 + 10/16 * 4 = 27/8.
 

mboza

Ars Tribunus Militum
2,679
Subscriptor++
Also, note that if you fail, you are starting again from the beginning, and the expected number of tosses from this position is the same as from the starting position. So instead of summing the infinite series you can solve the total number of tosses as:

(3 people case)

E = 3/4 * 2 (success) + 1/4 * (2+E) (fail, start again with 2 "wasted" tosses)
4E = 3*2 + (2+E)
E = 8/3

(9 people case)

E = 1/4 * (2+E) + 1/8 * (3 + E) + 10/16 * 4
E = 27/5
 
That's a much easier way to do it.

I think your method, for N=9, would actually be:

E = 1/4 * (2+E) + 1/8 * (3 + E) + 1/16 * (4 + E) + 9/16 * 4

because there's a 1/16 chance you make 4 tosses and then have to go again. This can be rearranged:

E = E * (7/16) + 2*1/4 + 3*1/8 + 4*10/16

E = (16/9) * (2*1/4 + 3*1/8 + 4*10/16)

And it works in general:
E = (2⌈log2N⌉ / N) * (expected tosses in one round)

You can also derive this by simplifying the infinite sum. In our cases "sum (odds of going again)^i, i=0 to infinity" is equal to (2⌈log2N⌉ / N)
 
Status
Not open for further replies.