5. (7 points) The king decided to test his hundred sages and announced that the next day he would line them up with their eyes blindfolded and put a black or white hat on each of them. After their eyes are uncovered, each, starting from the last in line, will name the supposed color of their hat. If he fails to guess correctly, he will be executed. The sages still have time to agree on how they will act tomorrow. How many sages can definitely be saved?
Solution. Let's describe the strategy that the sages should adhere to. The last in line looks ahead, counts the number of black hats, and says "black" if this number is even. In doing so, he cannot save himself for sure. However, the 99th, 98th, ..., 1st in line receive very important information. Thus, the 99th counts the number of black hats on the sages standing ahead, and if the parity of this number has changed, then he has a black hat, and he says "black." Then the 98th counts the number of black hats on those standing ahead and, knowing the parity of the number of black hats on those standing ahead without him and together with him, can uniquely determine the color of his hat, and so on. The sages from the 99th to the 1st will survive.

Answer. 99 sages will definitely manage to save themselves.