<p>100 (or some even number 2N :-) ) prisoners are in a room A. They are numbered from 1 to 100.</p>

<p>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!).</p>

<p>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.</p>

<p>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!).</p>

<p>Is there an algorithm that maximizes the probability that all prisoners survive? What probability does that algorithm achieve?</p>

<p>Notes:</p>

<ul>
<li><p>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!</p></li>
<li><p>The prisoners are not allowed to reorder the boxes!</p></li>
<li><p>All prisoners are killed the first time a prisoner fails to find his number. <em>And</em> no communication is possible.</p></li>
<li><p><b>Hint</b>: one can save more than 30 prisoners <em>on average</em>, which is much more that (50/100) * (50/99) * [...] * 1</p></li>
</ul>

[{'answer_id': 34355, 'author': 'Niyaz', 'author_id': 184, 'author_profile': 'https://Stackoverflow.com/users/184', 'pm_score': 1, 'selected': False, 'text': '<p>Implement a sorting algorithm and sort the boxes according to the numbers inside them.</p>\n\n<p>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)</p>\n\n<p>After the 2nd prisoner, all of the boxes will be in a sorted order !!!</p>\n\n<p>Everybody else can open the boxes containing their numbers easily then.</p>\n'}, {'answer_id': 34383, 'author': 'Re0sless', 'author_id': 2098, 'author_profile': 'https://Stackoverflow.com/users/2098', 'pm_score': 2, 'selected': False, 'text': "<p>I find a low tech solution to this type of problem is always the best way to go.</p>\n\n<p>first we make some assumptions about the situation</p>\n\n<ul>\n<li>The prisoners are not all programmers or mathematicians</li>\n<li>They don't want to die</li>\n<li>The guards are well armed</li>\n</ul>\n\n<p>So with a 0.005% chance that they will see tomorrow, there is a very simple and low tech solution to this problem. <strong>RIOT</strong> </p>\n\n<p>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.</p>\n"}, {'answer_id': 34384, 'author': 'Sam Hasler', 'author_id': 2541, 'author_profile': 'https://Stackoverflow.com/users/2541', 'pm_score': 0, 'selected': False, 'text': "<p>Maybe I'm not reading it right, but the question seems to be badly constructed or missing information.</p>\n\n<blockquote>\n  <p>If he finds the number that was\n  assigned to him in one of these 50\n  boxes, the prisoner gets to walk into\n  a room C and all boxes are closed\n  again before the next one walks into\n  room B from room A. Otherwise, all\n  prisoners (in rooms A, B and C) gets\n  killed.</p>\n</blockquote>\n\n<p>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?</p>\n\n<p>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.</p>\n\n<p>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).</p>\n\n<p>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.</p>\n"}, {'answer_id': 34392, 'author': 'Baltimark', 'author_id': 1179, 'author_profile': 'https://Stackoverflow.com/users/1179', 'pm_score': 0, 'selected': False, 'text': "<p>The prisoners could agree that prisoner 1 open boxes 1-50. </p>\n\n<p>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 <em>given that P1 survived</em> are now 50/99. You want to eliminate opening a box when you know that the previous guy found his. </p>\n\n<p>I don't know if that's optimal, but it's lot better than random. </p>\n\n<p>The probability of surviving that looks like </p>\n\n<p>50/100 * 50/99 * 50/98 *. . .50/51 * 1</p>\n"}, {'answer_id': 34429, 'author': 'Jorge Córdoba', 'author_id': 2695, 'author_profile': 'https://Stackoverflow.com/users/2695', 'pm_score': 1, 'selected': False, 'text': "<p>I don't know if this is allowed but the best approximation I can find is:</p>\n\n<p>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.</p>\n\n<p>Find the first 50 primes, let's asume we hold them in an array called primes.</p>\n\n<ul>\n<li>The first prissioner enters room B and opens the first box and finds the number m.</li>\n<li>Wait primes[1]^m (that would be 3^m)</li>\n<li>Open box 2 and read the number --> n</li>\n<li>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</li>\n</ul>\n\n<p>Repeat. After the first prisioner the total time for him would be:\n3^m * 5^n * 7^p ... = X</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>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)</p>\n\n<p>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.</p>\n\n<p>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%.</p>\n\n<p>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.</p>\n"}, {'answer_id': 34892, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 0, 'selected': False, 'text': "<p>I think since no communication is possible, the best strategy would involve</p>\n\n<blockquote>\n  <p>distributing the probability of each prisoners as evenly as possible</p>\n</blockquote>\n\n<p>Am I on the right path or not?</p>\n\n<p><strong>Information available for each prisoner:</strong></p>\n\n<blockquote>\n  <ul>\n  <li>The number of survivied prisoners, so if you have an efficient box picking system that  utilizes the order any prisoner enters room B, then <strong>a strategy is definitely possible</strong></li>\n  <li>Which boxes the earlier prisoners picked. Of course, no communication is possible <em>during</em> the run and it wouldn't be possible to remember any 100s box picking permutation. <strong>But you could use this information to compute in a system <em>before</em> the run starts.</strong></li>\n  </ul>\n</blockquote>\n\n<p><strong>My take:</strong></p>\n\n<blockquote>\n  <ol>\n  <li>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.  </li>\n  <li>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.</li>\n  <li>When a prisoner is moved to room B he/she opens whatever boxes he has marked on.</li>\n  </ol>\n</blockquote>\n"}, {'answer_id': 34908, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 0, 'selected': False, 'text': "<p>Same concept.</p>\n\n<p><strong>Aonther take:</strong></p>\n\n<blockquote>\n  <ol>\n  <li>Write down a list of the first 100 binary numbers which has fifty 1s and fifty 0s.</li>\n  <li>Sort them from lowest to highest.</li>\n  <li>Prisoner #1 gets the first number, prisoner #2 gets the second, prisoner #3 gets the third and so on...</li>\n  <li>Each prisoner remembers his/her binary number.</li>\n  <li>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.</li>\n  <li>He/she opens whatever boxes matched with 1 and leave closed boxes matched with 0.</li>\n  </ol>\n</blockquote>\n\n<p>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.</p>\n\n<p>I haven't thought out the exact figures and rationale though, but this is one quick solution I can think of at the moment.</p>\n"}, {'answer_id': 34956, 'author': 'Jeremy', 'author_id': 3657, 'author_profile': 'https://Stackoverflow.com/users/3657', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 34966, 'author': 'Jeremy', 'author_id': 3657, 'author_profile': 'https://Stackoverflow.com/users/3657', 'pm_score': 4, 'selected': True, 'text': '<p>This puzzle is explained at <a href="https://web.archive.org/web/20090616153244/http://www.math.princeton.edu/~wwong/blog/blog200608191813.shtml" rel="nofollow noreferrer">http://www.math.princeton.edu/~wwong/blog/blog200608191813.shtml</a> and that person does a much better job of explaining the problem.</p>\n\n<p>The "all prisoners are killed" statement is wrong.\nThe "you can save 30+ on average" is also wrong, the <a href="https://web.archive.org/web/20090616153244/http://www.math.princeton.edu/~wwong/blog/blog200608191813.shtml" rel="nofollow noreferrer">article</a> says that 30% of the time you can save 100% of the prisoners.</p>\n'}, {'answer_id': 35075, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>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\nFirst prisoners odds on losing are 50/100\nGive that the first prisoner survived then the second prisoners chance of winning are 50/99\nSo answer appears to be ((50 ^51)*49!)/100!\nwhich according to google makes 2.89*10^-9\nwhich is pretty much nil\nSo even if the prisoners knew the boxes the previously lucky ones found their number in there'd be no hope</p>\n"}]
['https://Stackoverflow.com/questions/34345', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2638/']