100 (or some even number 2N :-) ) prisoners are in a room A. They are numbered from 1 to 100.
One by one (from prisoner #1 to prisoner #100, in order), they will be let into a room B in which 100 boxes (numbered from 1 to 100) await them. Inside the (closed) boxes are numbers from 1 to 100 (the numbers inside the boxes are randomly permuted!).
Once inside room B, each prisoner gets to open 50 boxes (he chooses which one he opens). If he finds the number that was assigned to him in one of these 50 boxes, the prisoner gets to walk into a room C and all boxes are closed again before the next one walks into room B from room A. Otherwise, all prisoners (in rooms A, B and C) gets killed.
Before entering room B, the prisoners can agree on a strategy (algorithm). There is no way to communicate between rooms (and no message can be left in room B!).
Is there an algorithm that maximizes the probability that all prisoners survive? What probability does that algorithm achieve?
Notes:
Doing things randomly (what you call 'no strategy') indeed gives a probability of 1/2 for each prisoner, but then the probability of all of them surviving is 1/2^100 (which is quite low). One can do much better!
The prisoners are not allowed to reorder the boxes!
All prisoners are killed the first time a prisoner fails to find his number. And no communication is possible.
Hint: one can save more than 30 prisoners on average, which is much more that (50/100) * (50/99) * [...] * 1
This puzzle is explained at http://www.math.princeton.edu/~wwong/blog/blog200608191813.shtml and that person does a much better job of explaining the problem.
The "all prisoners are killed" statement is wrong.
The "you can save 30+ on average" is also wrong, the article says that 30% of the time you can save 100% of the prisoners.
I find a low tech solution to this type of problem is always the best way to go.
first we make some assumptions about the situation
The prisoners are not all programmers or mathematicians
They don't want to die
The guards are well armed
So with a 0.005% chance that they will see tomorrow, there is a very simple and low tech solution to this problem. RIOT
its all about losses v potential gain, the chances are the prisoners far out number the guards, and using each other as human shields, as they are all dead men anyway if they don't, they can increase the chances they will over power a guard, once they have his weapon there chance goes up, helping them over power more guards to get more fire power to further increase there survival rate. once the guards realise what's happening, they will probably run for the hills and lock down the prison, this will give the media a heads up and then its a human rights issue.
Implement a sorting algorithm and sort the boxes according to the numbers inside them.
First prisoner sorts 50 boxes, and the second prisoner sorts the other 50 and merges with the first one. (Note that the second prisoner can guess the values inside the first 50 boxes)
After the 2nd prisoner, all of the boxes will be in a sorted order !!!
Everybody else can open the boxes containing their numbers easily then.
I don't know if this is allowed but the best approximation I can find is:
EDIT: Ok, I think this makes it. Of course I'm treating this as a computing problem, I don't think any prisioner will be able to perform this, although is pretty straight forward if you don't.
Find the first 50 primes, let's asume we hold them in an array called primes.
The first prissioner enters room B and opens the first box and finds the number m.
Wait primes[1]^m (that would be 3^m)
Open box 2 and read the number --> n
Wait (primes[2]^n - 1) * primes[1]^m, that would be (5^n - 1) * 3^m and the total time he has been waiting would be 3^n * 5^n
Repeat. After the first prisioner the total time for him would be:
3^m * 5^n * 7^p ... = X
Before the second prisioner enters the room factorize X. You know beforehand the prime numbers that have been used so the factorization is trivial. Doing so you obtain m, n, p, etc so the second prisioner knows every box/number combination the previous prisioner used.
The probability of the first one getting everybody killed is 1/2, the second one will have a 50 / (100 - n) (being n the numbers of attemps of the first one) the third one will have 50 / (100 - n - m) (if n + m = 100 then all positions are known) and so on.
Obviously the next prissioner must skip the already known boxes (except for the last choice if the box which contains his number is already known)
I don't know what's the exact possibility as it dependes on how many choices they have to do but I'd say it's pretty high.
EDIT: Rereading, if the prissioner does not have to stop when he obtains his number then the probability for the whole group is vastly improved, exactly 50%.
EDIT2: #OysterD see it this way. If the first prisioner can open 50 boxes then the second one know if its number is in any of that boxes. If it is, then he can open other 49 (and by doing so learning the box/number comination of the 100 boxes) and finally open his one. So if the first prissioner succeds then everyone succeds. Remember that each prisioner provides a way for the other to know exactly the boxes/number combination for every box he opens.
Maybe I'm not reading it right, but the question seems to be badly constructed or missing information.
If he finds the number that was
assigned to him in one of these 50
boxes, the prisoner gets to walk into
a room C and all boxes are closed
again before the next one walks into
room B from room A. Otherwise, all
prisoners (in rooms A, B and C) gets
killed.
Does the last sentence there mean that all prisoners are killed the first time a prisoner fails to find their number? If not, what happens to prisoners that don't find their number?
If no communication is possible, and whenever a prisoner enters room B it is always in an identical state then there is no possibility for a strategy.
The prisoners could could say before they leave room A which number box they are going to open. But without subsequent prisoners knowing whether they were successful or not (assuming failure for one isn't failure for all) when the next prisoner enters room B they still have the same odds of picking their number as the previous prisoner (always 1:100).
If failure for one is failure for all, then by knowing which box the previous prisoners opened, and by dint of the fact that they haven't all been killed, each successive prisoner could reduce the odds of picking the wrong box by one box. i.e. 1:100 for the first prisoner, 1:99 for the second, down to 1:1 for the last.
The prisoners could agree that prisoner 1 open boxes 1-50.
If they're all still alive, they agree that the next prisoner opens boxes 2-51. (the 2 is arbitrary, but simple to remember this rule) His odds of surviving given that P1 survived are now 50/99. You want to eliminate opening a box when you know that the previous guy found his.
I don't know if that's optimal, but it's lot better than random.
The probability of surviving that looks like
50/100 * 50/99 * 50/98 *. . .50/51 * 1
I think since no communication is possible, the best strategy would involve
distributing the probability of each prisoners as evenly as possible
Am I on the right path or not?
Information available for each prisoner:
The number of survivied prisoners, so if you have an efficient box picking system that utilizes the order any prisoner enters room B, then a strategy is definitely possible
Which boxes the earlier prisoners picked. Of course, no communication is possible during the run and it wouldn't be possible to remember any 100s box picking permutation. But you could use this information to compute in a system before the run starts.
My take:
Draw a table of numbers with 2 columns, the first column contains the box number (from box #1 to box#100). Each prisoner then gets to pick 50 boxes and whatever box they pick, they should put 1 mark on the corresponding row in the second column.
All prisoners are however required to never pick any box twice. And no box may be marked more than 50. Some prisoners may get less options than others since some box may get filled to 50 marks first.
When a prisoner is moved to room B he/she opens whatever boxes he has marked on.
If all prisoners are killed when someone fails to find their number then you either save 100 or 0. There is no way to save 30 people.
Same concept.
Aonther take:
Write down a list of the first 100 binary numbers which has fifty 1s and fifty 0s.
Sort them from lowest to highest.
Prisoner #1 gets the first number, prisoner #2 gets the second, prisoner #3 gets the third and so on...
Each prisoner remembers his/her binary number.
When any prisoner is moved to room B, he/she then match the binary digits of the number he remembered with each of the box, the highest bit is matched with the leftmost box, the next highest bit is matched with the second leftmost box ... the lowest bit is matched with the rightmost box.
He/she opens whatever boxes matched with 1 and leave closed boxes matched with 0.
This would minimizes the probability because early prisoners will get digits that are different from the later prisoners and prisoners which has number close together would get digits close together. This doesn't guarantee survivability but if the early prisoners do survive, chances are the later prisoners would have a higher probability of surviving as well.
I haven't thought out the exact figures and rationale though, but this is one quick solution I can think of at the moment.
There aren't any time limits in the question so I suggest that prisoners should decide to take 1 hour per box and open them in the order presented. If the second prisoner is allowed into the room after 2 hours then he knows that the first prisoner found his own number in box 2. Therefore he knows to skip box 2 in his sequence and opens boxes 1, 3, 4...51
First prisoners odds on losing are 50/100
Give that the first prisoner survived then the second prisoners chance of winning are 50/99
So answer appears to be ((50 ^51)*49!)/100!
which according to google makes 2.89*10^-9
which is pretty much nil
So even if the prisoners knew the boxes the previously lucky ones found their number in there'd be no hope
Related
There are N stones arranged in a circle. A frog sits on the first stone. The frog keeps jumping in clock-wise direction from stone to stone. It skips the stones in increments of 1 for each jump. ie, for the first jump it will skip 0 stones, it skips one stone for the 2nd jump, 2 stones for the 3rd jump and so on.
The task is, given the number of stones N, find out if the frog will reach all the stones.
Eg:
N=1 ---> true
N=2 ---> true --> jumps=(1,2)
N=3 ---> false --> jumps=(1,2,1,1,2,1,1,2,1.....)
N=4 ---> true --> jumps=(1,2,4,3)
I have tried out for different values for N. All I could see was, if N is a power of 2 then all stones can be reached. Also if the any stone is visited more than once before visiting all the stones, then the answer is false. I could not find a reasoning for the observations and hence could not prove it.
I think you're thinking about this the wrong way. Don't get me wrong, it's cool to do mathematical analysis and come out with a simple result, like, "All stones can be reached if and only if N is a power of 2." (And in fact, I believe that that result is correct.) But you don't need to find that sort of result in order to write an algorithm that can answer the question.
Instead, your algorithm can just simulate the frog's jumps (which you've obviously already figured out how to do), and see if it hits every stone. The only tricky part is that the frog plans to continue jumping forever, and obviously you want your algorithm to finish in a finite amount of time. The key insight is that the frog's sequence of jumps will always end up in a loop, so you only need to continue simulating the sequence long enough to be sure that you've entered the loop and completed one pass through it.
How do we know this?
To see why, imagine that we know that the frog has just jumped from stone #i to stone #j. Is that enough to tell us where the frog will jump to next? The answer is "yes"; we don't know whether the frog thinks it skipped j−i−1 stones vs. N+j−i−1 stones vs. 2N+j−i−1 stones or whatnot, but it doesn't matter, because the next stone it lands on doesn't depend on whether the next jump skips j−i stones vs. N+j−i stones vs. 2N+j−i stones — these are all equivalent. So if we know its last jump then we know its next jump, and if we know its next jump then we know the jump after that, and so on: so this one jump is enough to determine the whole rest of the sequence of stones.
This means that there at most N2 possible "states" that the sequence can be in, always determined by the prior stone and the next stone, where each state fully determines all subsequent states. So if you simulate the sequence through at least N2 states, then either you've visited every state exactly once or you've visited some state at least twice (by the pigeonhole principle), so you've necessarily hit all the states you're going to, which means you've necessarily landed on all the stones that you're going to.
That gives you an algorithm in O(N2) time and O(N) space.
You can potentially improve this by
tracking which states you've already visited, and terminating as soon as you find that you've returned to a state.
tracking how many stones you've already landed on, and terminating as soon as you find that you've landed on all of them.
which would complete in O(N) time if you're right that "if the any stone is visited more than once before visiting all the stones, then the answer is false", while still behaving correctly (and still within O(N2) time) if that theory is wrong.
So I had a programming competition ,I was stuck in a question and I trying to solve it these few days. because I want to know the answer so much, but I'm not able to do it. Any explanation or answer will be appreciated. Here is the question:
There’s a concert happening at a Hall. The hall is an infinitely long 1D number line, where each of the N students are standing at a certain point in the number line. Since they are all scattered around different positions in the number line, its getting harder to manage them. So the organizer Kyle wants to move them in the hall in such a way so that they all are in consecutive positions, for example, they are in positions 2,3,4,5 instead of being scattered around like 1,3,5,6. In simpler words, there is no gap between them. At one time Kyle can pick one person who is on the most outer positions on any side. For example in 1,3,5,6 the most outer positions are 1 and 6. He can pick either of them and move them to any unoccupied position so that they are not in the most outer position anymore. This makes them come closer and closer with each move until they are all in consecutive positions. Find the maximum and minimum number of moves that can accomplish this task.
The maximum is the count of gaps, since the most inefficient move will reduce the count of gaps between the outermost pair by one.
For the minimum, given k people, in linear time (using a sliding window) find the stretch of k consecutive positions with the most people, and call this count c. Then the minimum is k-c, where each move puts someone from outside this window to inside it.
I have some idea for you but I'm not totally sure I got it right..
So:
If number 1 is currently the most outer than he is then jumping to the highest number
and number 2 is now the outer and it goes infinitely and same goes for the other end the outer on the other side is moving to the next becoming last and so on.. make sense?
I've been practicing some probabilities for last few weeks around 10/week usually did the job, however as the topics got harder I've started struggling and now I'm completely stuck. I've been looking online and i did find similar examples but nothing to touch my case in particular. I will continue looking for an answer so even if u can't answer my specific cases links to online literature will be appreciated.
answers are welcome, however i would rather be more interested in explanation of how a problem works.
urn contains m white and k black balls. two players are drawing the balls one after another without putting them back into the urn. the winner is the first person to draw white ball. what is the probability that second player will be the winner? (k=4, m=4)
women tend to vote with probability of a, men tend to do the same with probability of b. the probability c tells us that if we take a couple one of them will not go voting. what are the chances that at least one of them will vote? (a=0.49, b=0.61, c=0.75)
we are sending a message of n bytes. to obtain higher chance of sending entire message without ruining it we use k different wires. What is the probability of sending an entire message through one of the wires without ruining it, if the probability of ruining any of the bytes at any of the wires is p. (p=0.06, n=7, k=6)
the basketball game finals play to N wins. after m + n games the result is m : n. what is the probability of winning the finals for the lagging(the team that's behind) team if it is known that the leading team wins each match with a probability of p? (m=3 n=2 N=5 p=0.36)
sorry for my English and any help will be appreciated
Just A
So Given m = k and both = 4. And each player takes a ball out per turn. Given the worst case scenario 4 good balls will be picked (K) there will be 2 turns of both players picking the black ball then the first player will be guaranteed the white ball next turn. Therefore you need to work out the probability for the first two turns.
Turn 1
- P1 turn 1 Chance for black= 4/8
- P2 turn 2 Change for white = 4/7
Therefore turn 1 chance = (4/8)*(4/7). = 28.57%
Turn 2
- P1 turn 1 Chance for black= 2/6
- P2 turn 2 Change for white = 4/5
Therefore turn 2 chance = (4/6)*(4/5). = 26.664% but to get to turn 2 is a 28.57% chance
Therefore its the first probability add the second probability given the first case occurs. So 28.57% + (26.44%*28.57%)= 36.12%
The third case is a always lose. Top tip then be the first player!
Turn 3 Instance win for player 2 therefore a loss. = 0%
Every now and then I read all those conspiracy theories about Lotto-based games being controlled and a computer browsing through the combinations chosen by the players and determining the non-used subset. It got me thinking - how would such algorithm have to work in order to determine such subsets really efficiently? Finding non-used numbers is definitely crossed out as is finding the least used because it's not necesserily providing us with a solution. Also, going deeper, how could an algorithm efficiently choose such a subset that it was used some k times by the players? Saying more formally:
We are given a set of 50 numbers 1 to 50. In the draw 6 numbers are picked.
INPUT: m subsets each consisting of 6 distinct numbers 1 to 50 each,
integer k (0<=k) being the maximum players having all of their 6
numbers correct.
OUTPUT: Subsets which make not more than k players win the jackpot ('winning' means all the numbers they chose were picked in the draw).
Is there any efficient algorithm which could calculate this without using a terrabyte HDD to store all the encounters of every possible 50!/(44!*6!) in the pessimistic case? Honestly, I can't think of any.
If I wanted to run such a conspirancy I would first of all acquire the list of submissions by players. Then I would generate random lottery selections and see how many winners would be produced by each such selection. Then just choose the random lottery selection most attractive to me. There is little point doing anything more sophisticated, because that is probably already powerful enough to be noticed by staticians.
If you want to corrupt the lottery it would probably be easier and safer to select a few competitors you favour and have them win the lottery. In (the book) "1984" I think the state simply announced imaginary lottery winners, with the announcement in each area announcing somebody outside the area. One of the ideas in "The Beckoning Lady" by Margery Allingham is of a gang who attempt to set up a racecourse so they can rig races to allow them to disguise bribes as winnings.
First of all, the total number of combinations (choosing 6 from 50) is not very large. It is about 16 million which can be easily handled.
For each combination keep a count of number of people who played it. While declaring a winner choose the combination that has less than k plays.
If the number within each subset are sorted, then you can treat your subsets as strings - sort them in lexicographical order, then it is easy to count how many players selected each subset (and which subsets were not selected at all). So the time is proportional to the number of players and not the number of numbers in the lottery.
I can not understand package-merge algorithm.
Can anyone explain package-merge algorithm step by step please?
How we package and how we merge?
Is there any other optimal algorithm for solving coin collector's problem?
This is a code-free answer: I'm going to try and explain the method using some example situations, and hope it can be intuited from them as I generalise it. Sorry it's quite long, but if you read it through methodically it should all make sense.
Let's say a coin collector has two sets of coins: a set of dollar coins and a set of half dollar coins. However, the coin collector values each coin differently based on, say, the date it was minted. Some are very rare and so are more valuable.
Now, the situation is: he has no ordinary money except for his valuable coins, and he needs to use them to buy something at a store, where the clerk at the counter will only take them at their standard value. But as he values his collection, he wants to use the least valuable set of coins he can to make the payment.
So let's say he has 7 dollar coins and 5 half dollar coins, and he wants to buy something worth 4 dollars. A simplistic solution would be to take just his dollar coins and rank them by value, select the 4 least valuable, and hand them over, pocketing the remaining 3 dollar coins, which are the most valuable ones.
But then he realises that if he takes two half dollars and "packages" them together, he can treat the package as a new dollar-sized coin. To the clerk at the counter it's worth a dollar, but to the collector it's worth the combined value of the two individual half dollars he used.
A better strategy then is to look at his half-dollars, take the cheapest two, package them together as a new dollar "coin", and add ("merge") it into his set of dollars.
So now he has 8 (7+1) dollar-sized coins/packages and 3 (5-2) half dollars.
This step can be repeated once more, and now he has 9 dollar-sized coins/packages, and 1 remaining half dollar that cannot be packaged with anything else. Since this is his most valuable half dollar, he realises that he should not use it and pockets it again, effectively removing it from the problem.
Now he just has a set of 9 dollar-sized coins/packages. He sorts them by value, selects the 4 that are least valuable, and hands them over to the clerk, pocketing the remaining (most valuable) coins.
To generalise slightly: imagine he also has some quarters with him. Before packaging the half dollar coins, the he must first package the quarters into half-dollar packages (selecting the cheapest pairs in turn, discarding the most valuable remainder, if there is one), and then merge them into the set of half dollar coins.
Hypothetically there could be lesser denominations: 1/8th dollars, 1/16th dollars, etc. As long as they are negative powers of two the strategy can be generalised: sort the coins of least denomination, package them, merge the packages into the set of the next smallest denomination, and continue until you have only dollar-sized coins/packages.
(There's one other case that's worth considering: if the asking price is not a round dollar figure, e.g. 7 1/4 dollars. In this case, before packaging the quarter-sized coins/packages, you first select the cheapest, hand it over and subtract it from the asking price, then package the remaining ones.
This also can be generalised: whenever the price requires a particular denomination (i.e. it is an odd multiple of that denomination); while packaging that denomination you remove the cheapest one first, subtracting it from the price. By the time you get to the dollar stage, the price will then be a whole number of dollars.)
I find the coin analogy unhelpful. You might too. More intuitive for me is to consider a set of rods of various lengths in powers of two: 1/4, 1/2, 1, 2, 4, 8, .... Each rod has a different price. You need to spend the least money on rods and yet span some exact distance X.
With any combination of these rods, you can only /exactly/ span a distance which is diadic, that is "which has factors only of two in its denominator", ie has a terminating binary representation after the point. You can't span 1/5 or 4 3/7 because no finite number of rods matches such distances exactly, there will always be a little left over.
So assuming you have do cover a diadic X, what rods do you use? Think about the smallest binary fraction used in your distance X. If X is 13 3/128 then that's 1/128. That's the level of accuracy you need.
a. If all your rods are bigger than 1/128 then you're stuffed. None of your rods are fine enough to make that last 1/128th. Bad luck.
b. If 1/128 is the smallest kind of rod you have, you're going to have to use at least one of them. There's no choice to go that extra tiny distance: you're going to use one. So may as well buy the cheapest 1/128 rod now. Then you can solve the remaining slightly smaller problem (13 1/64) with the remaining rods.
c. If your smallest rod is tinier than 1/128 (say 1/512) then it's possible that you will use them, but only an even number of them. If you use an odd number, you'll have some tiny amount (here 1/512) left over and whatever you do with the bigger rods can't get rid of that extra little bit and you are stuffed. So you're either gonna use none of them or an even number. If you end up using two, you'll end up using the cheapest two. If you end up using four, you'll end up using the cheapest four, and so on.
So you'll use them in pairs. If you take the cheapest two -- say they cost 5p and 7p -- then if you use them, then you'll use them, effectively, as if they're a single 1/256 piece costing 12p. So take them out of the 1/512 pile and consider them as if a 12p 1/256 piece along with all the other 1/256 pieces. The leftover third 1/512 piece, the most expensive one, is useless, it won't be used for the reason above, it is too fine and will leave over that tiny fraction.
Now we're done with the 1/512 pile, you can now consider the 1/256 pile, which is now the smallest and maybe has a proper 1/256 (say costing 8p) in it, and also our "package" of two 1/512s (costing say 12p). You can then package these up in a pair using the arguments above. It has a cost of 20p and equivalent to a 1/128 piece.
You've not committed to using any of these packages yet, you just know that if you do, you'll use the whole package. And you carry on, up the sizes, making the problem smaller each time, either by reducing the total number of pieces by packaging, or by reducing the target.
So eventually, you will reach your target, or prove that you can't.
The most important thing is to remember that when you build a package you're not, at that point, committing to use it, you're just saying /if/ you do, then you'll use the rods inside as a unit.
It doesn't lend itself to a very short explanation; have you read the Wikipedia page?