Once upon a time there was a prison full of prisoners. In order to be capable of accepting the new ones some of the old ones must be executed. An executioner came up with the idea that he put a hat on the head of each prisoner. If the prisoner guessed the colour of the hat on his head he would survive if not - he would be executed.
Imagine the situation with ( ) hat colours and ( ) prisoners. The prisoners know the number of colours and know what the colours are. Every prisoner can see everyone's else hat but he is unable to see his own hat. He can also hear the answers of the others. Each prisoner is allowed to say only the name of the colour. The prisoners appeared to be clever enough to come up with an algorithm to save as much of them as possible. What is the maximum guaranteed number of prisoners saved in case of prisoners and colours? Just to have a numerical answer: how much would it be for prisoners and colours?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The solution
Only the first prisoner to answer is devoted to die because his mission is to give the essential information to the others. In this way at least n − 1 prisoners will survive regardless the number of colours. It makes 2019 prisoners the right answer to the question.
Explanation
Let's assign the numbers to the colours, e.g. blue = 0, black = 1, purple = 2 and so on up to k − 1 . The first person adds up all the numbers he sees and gets the sum S 1 and says the result of S 1 m o d k operation . Denote the colour of i th hat with a i . The algorithm is the following:
It continues so as long as all prisoners say the colours. In this way n − 1 prisoners will survive. It makes 2 0 1 9 prisoners the right answer to the question for n = 2 0 2 0 .
Derivation
Assume the sum of all the colours is S = a 1 + a 2 + … + a n . The first prisoner sees the sum S 1 = a 2 + a 3 + … + a n = S − a 1 . The i th prisoner sees the sum S i = S − a i . Note that S i = S − a 1 + a 1 − a i = S 1 + a 1 − a i . He wants to guess a i . He can calculate it from a i = S 1 − S i + a 1 = S 1 − ( S i − a 1 ) . The prisoners are allowed only to say colors not the sums. One can notice that a i = a i m o d k for each colour. We can use the modulo operation to extract the important information for us: a i = S 1 m o d k − ( S i − a 1 ) m o d k . This is exactly what happens in the listed algorithm steps.
Example
Let's check it for k = 3 colours (blue = 0, black = 1, purple = 2) and n = 1 0 prisoners. With the numbers assigned we would have e.g. the hats: 2 , 0 , 1 , 0 , 0 , 2 , 2 , 1 , 2 , 1 . Let's start from the left:
And so does each next prisoner until all prisoners say their colours.