Guess the colour or die

Logic Level 2

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 k N k \in \mathbb N ( k 2 k \geq 2 ) hat colours and n N n \in \mathbb N ( n 2 n \geq 2 ) prisoners. The prisoners know the number of colours k k 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 n \mathbf n prisoners and k \mathbf k colours? Just to have a numerical answer: how much would it be for 2020 2020 prisoners and 5 5 colours?


The answer is 2019.

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.

1 solution

Maria Paszkiewicz
May 10, 2020

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 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 k-1 . The first person adds up all the numbers he sees and gets the sum S 1 S_1 and says the result of S 1 m o d k S_1 \mod k operation . Denote the colour of i i th hat with a i a_i .  The algorithm is the following:

  1. First prisoner calculates S 1 m o d k = m 1 S_1 \mod k  = m_1 and says the colour corresponding to m 1 m_1 .
  2. The second prisoner sees the sum of all hats equal to S 2 S_2 . He must subtract the colour a 1 a_1 of the first prisoner S 2 = S 2 a 1 S_2' = S_2 - a_1 . Then, he calculates: S 2 m o d k = m 2 S_2' \mod k  = m_2 . The colour a 2 a_2 of his hat is the result of: ( m 1 m 2 ) m o d k (m_1 - m_2) \mod k . He says the colour corresponding to a 2 a_2 .
  3. The third prisoner sees the sum of all hats is equal to S 3 S_3 . He must subtract the colour of the first prisoner S 3 = S 3 a 1 S_3' = S_3 - a_1 . Then, he calculates: S 3 m o d k = m 3 S_3' \mod k  = m_3 . The color of his hat a 3 a_3 is the result of: ( m 1 m 3 ) m o d k (m_1 - m_3) \mod k . He says the colour corresponding to a 3 a_3 .

It continues so as long as all prisoners say the colours. In this way n 1 n-1 prisoners will survive. It makes 2019 2019 prisoners the right answer to the question for n = 2020 n = 2020 .

Derivation

Assume the sum of all the colours is S = a 1 + a 2 + + a n S = a_1 + a_2 + \ldots + a_n . The first prisoner sees the sum S 1 = a 2 + a 3 + + a n = S a 1 S_1 = a_2 + a_3 + \ldots + a_n = S - a_1 . The i i th prisoner sees the sum S i = S a i S_i = S - a_i . Note that S i = S a 1 + a 1 a i = S 1 + a 1 a i S_i = S - a_1 + a_1 - a_i = S_1 + a_1 - a_i . He wants to guess a i a_i . He can calculate it from a i = S 1 S i + a 1 = S 1 ( S i a 1 ) 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 a_i = a_i \mod 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 a_i = S_1 \mod k - (S_i - a_1) \mod k . This is exactly what happens in the listed algorithm steps.

Example

Let's check it for k = 3 k=3 colours (blue = 0, black = 1, purple = 2) and n = 10 n=10 prisoners. With the numbers assigned we would have e.g. the hats: 2 , 0 , 1 , 0 , 0 , 2 , 2 , 1 , 2 , 1 2,\; 0,\; 1,\; 0,\; 0,\; 2,\; 2,\; 1,\; 2,\; 1 . Let's start from the left:

  1. First prisoner (with purple a 1 = 2 a_1 = 2 hat) sees the sum S 1 = 0 + 1 + 0 + 0 + 2 + 2 + 1 + 2 + 1 = 9 S_1 = 0+1+0+0+2+2+1+2+1 = 9 and says "blue" ( 0 0 ) because m 1 = ( 0 + 1 + 0 + 0 + 2 + 2 + 1 + 2 + 1 ) m o d 3 = 9 m o d 3 = 0 m_1 = (0+1+0+0+2+2+1+2+1) \mod 3 = 9 \mod 3 = 0
  2. Second prisoner (with blue a 2 = 0 a_2 = 0 hat) sees the sum S 2 = 2 + 1 + 0 + 0 + 2 + 2 + 1 + 2 + 1 = 11 S_2 = 2+1+0+0+2+2+1+2+1 = 11 . He subtracts the colour of the first prisoner 11 2 = 9 11 - 2 = 9 . He calculates m 2 = 9 m o d 3 = 0 m_2 = 9 \mod 3 = 0 . Now, he needs to calculate ( m 1 m 2 ) m o d 3 = ( 0 0 ) m o d 3 = 0 (m_1 - m_2) \mod 3 = (0 - 0) \mod 3 = 0 . This corresponds to the hat on his head. The colour is blue.
  3. Third prisoner (with black a 3 = 1 a_3= 1 hat) uses the information from the first one: m 1 = 0 m_1 = 0 . He sees the sum S 3 = 2 + 0 + 0 + 0 + 2 + 2 + 1 + 2 + 1 = 10 S_3 = 2+0+0+0+2+2+1+2+1 = 10 . He subtracts the color of the first prisoner 10 2 = 8 10 - 2 = 8 . He calculates m 2 = 8 m o d 3 = 2 m_2 = 8 \mod 3 = 2 . Now, he needs to calculate ( m 1 m 2 ) m o d 3 = ( 0 2 ) m o d 3 = 1 (m_1 - m_2) \mod 3 = (0 - 2) \mod 3 = 1 .

And so does each next prisoner until all prisoners say their colours.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...