Official

A - Hat Puzzle Editorial by evima


Let’s establish a strategy for sufficiently intelligent players.

We represent the colors of the hats of the \(N\) players as a string of RB.

Consider the set \(S\) of RB strings that do not contradict the information you have given so far. Since the players see different numbers of hats, it seems that \(S\) would be different for each player. However, \(S\) is simply a set of RB strings that do not contradict “the information you have given.” In other words, \(S\) is the set of possible RB strings for player \(1\).

For each player \(i\), an RB string \(x\) is uncontradictory for that player if and only if the following two conditions are satisfied.

  • \(x \in S\).
  • The colors of the hats of all players \(j\) (\(j<i\)) matches \(x\).

Let \(R\) and \(B\) be the number of players wearing red and blue hats, respectively. At the start of round \(1\), all the strings consisting of \(R\) Rs and \(B\) Bs are contained in \(S\).

Let’s carefully observe what happens in each round. First, the player who is asked by you thinks as follows:

  • Player \(i\)’s thought: Determine the set \(T\) of uncontradictory RB strings for player \(i\) from the colors of the hats of all players \(j\) (\(j<i\)) and \(S\). If the color of player \(i\)’s hat is the same in all RB strings in \(T\), player \(i\) can be sure that their hat is that color. Otherwise, they cannot yet determine the color of their hat.

Next, after you announce the players who answered Yes, \(S\) changes as follows:

  • Consider each \(x \in S\). Assume that the colors of the hats of the \(N\) players are \(x\), and consider what the response to your question in this round would be for all players. If this contradicts the actual responses (which can be known from your announcement), remove \(x\) from \(S\).

Now we know how the game progresses. However, if we implement the above strategy as it is, the complexity will be at least proportional to the size of \(S\), which is far from enough for the constraints of this problem.

So, let’s consider the characteristics of the RB strings contained in \(S\).

We will consider the RB string as a path \((0,0)\to (R,B)\) on a grid, reading R as right and B as up.

Then, all updates to \(S\) can be written in this form: remove from \(S\) all paths passing through a certain point \((x,y)\). Let’s confirm this.

First, if we rephrase this property, \(S\) is characterized by a set of forbidden points on the grid. We prove this by induction on the number of rounds.

It is clear that \(S\) can be written in this form at the start of round \(1\).

Now, suppose that \(S\) can be described in the above form at the start of a round. Perform a DP on the grid to make the set of forbidden points maximal. More specifically, for example, if \((10,11),(11,10)\) are forbidden, also forbid \((10,10),(11,11)\). By doing this, it is guaranteed that the point \((x,y)\) is not a forbidden point \(\iff\) there is a legitimate path passing through \((x,y)\).

Let’s trace the thoughts of each player. If the numbers of red and blue hats visible from the player are \(r\) and \(b\), it is confirmed that the path passes through a point \((r,b)\). If \((r+1,b)\) is forbidden, it is confirmed that the path will go to \((r,b+1)\). In other words, it is confirmed that the color of their hat is blue. The same discussion can be made for red. And if both \((r+1,b)\) and \((r,b+1)\) are available, they cannot determine the color of their hat. In this way, by representing \(S\) as a set of forbidden points, we can know each player’s movement.

Next, let’s see how \(S\) changes after your announcement. Suppose the path passed through the coordinates \((x,y)\). Here, refer to how player \(x+y+1\) responded to the previous question. If the response was Yes, exactly one of \((x+1,y)\) and \((x,y+1)\) should be forbidden. Otherwise, it is found that the path cannot pass through \((x,y)\). In other words, \((x,y)\) is added as a forbidden point. The same discussion can be made if the player’s response was No.

From the above, we found that by characterizing \(S\) as a set of forbidden points on the grid, we can simulate the progress of one round. Now, repeat it a sufficient number of times and terminate when it is found that the set of forbidden points no longer changes.

Let’s estimate the necessary number of rounds. The conclusion is that \(N\) rounds is sufficient. We prove this by induction. First, the case \(N=1\) is clear.

Consider when \(N>1\). First, all of the \(N-1\) players \(2,3,\cdots,N\) can see player \(1\)’s hat, so they can be considered to be playing a game with \(N-1\) players excluding player \(1\). By the induction hypothesis, their movements converge in \(N-1\) rounds, and there is no information at all in the subsequent movements. In other words, since the last time player \(1\) can get information is round \(N-1\), the earliest round in which player \(1\)’s movement can change is round \(N\). This completes the proof.

Implementing the above discussion as it is yields an \(O(N^3)\) solution.

Sample Implementation

posted:
last update: