Source: IMO-2019-P5
Category: Operations and Strategies

Problem:
Let $n$ be a positive integer. Harry has $n$ coins lined up on his desk, which can show either heads or tails. He does the following operation: if there are $k$ coins which show heads and $k > 0$, then he flips the $k$th coin over; otherwise he stops the process. (For example, the process starting with $THT$ would be $THT \to HHT \to HTT \to TTT$, which takes three steps.)

Prove the process will always terminate, and determine the average number of steps this takes over all $2^{n}$ configurations.

Solution:
We will prove that the process always terminates and that the expected number of steps over all $2^n$ initial configurations is
\[
E_n = \frac{1}{2} (1 + 2 + \dots + n) = \frac{1}{4} n(n+1),
\]
which is finite.

We represent the operation by a directed graph $G_n$ whose vertices are the strings in $\{0,1\}^n$, where $1$ corresponds to heads and $0$ corresponds to tails. Each string points to its successor under the operation. For a bit $b \in \{0,1\}$, we let $\overline{b} = 1-b$, and we denote binary strings as a sequence of $n$ symbols.

The main claim is that the graph $G_n$ can be described explicitly in terms of $G_{n-1}$. Specifically, the vertex set of $G_n$ can be partitioned into two halves, which we will call $X$ and $Y$, such that each half is isomorphic to $G_{n-1}$ in its edge structure, with a single connecting edge from $Y$ to $X$. The construction is as follows:
\begin{itemize}
    \item We take two copies $X$ and $Y$ of the graph $G_{n-1}$.
    \item To form the vertices of $X$, we take each string of length $n-1$ from $G_{n-1}$ and append a $0$ to it. In symbols, we apply the mapping $s_1 s_2 \dots s_{n-1} \mapsto s_1 s_2 \dots s_{n-1}0$.
    \item To form the vertices of $Y$, we take each string of length $n-1$ from $G_{n-1}$, toggle every bit, reverse the order of the bits, and then append a $1$ to the end. In symbols, we apply the mapping $s_1 s_2 \dots s_{n-1} \mapsto \overline{s}_{n-1} \overline{s}_{n-2} \dots \overline{s}_1 1$.
    \item Finally, we add one new edge from $Y$ to $X$, specifically from the state $11\dots1$ to the state $11\dots10$.
\end{itemize}

To prove this claim, we need only show that the arrows in this recursively defined directed graph match the actual operation rules. 

First, consider the subgraph $X$. For any string in $X$, the $n$-th bit is $0$. Appending a $0$ to a string $s_1 \dots s_{n-1}$ does not change the total number of $1$s. Thus, if the original string had $k$ ones, the new string also has $k$ ones, and the operation will flip the $k$-th bit. This perfectly matches the transitions in $G_{n-1}$, meaning $X$ is a valid subgraph of $G_n$ and the process operates exactly as it does in $G_{n-1}$.

Next, consider the subgraph $Y$. Suppose a string $s = s_1 \dots s_{n-1}$ in $G_{n-1}$ has exactly $k$ ones. The modified string in $Y$ is given by $\overline{s}_{n-1} \dots \overline{s}_1 1$. The number of ones in the first $n-1$ bits of this new string is $(n-1) - k$, because every bit was toggled. Including the final $1$, the total number of ones in the new string is $(n-1-k) + 1 = n-k$. 
Therefore, the operation on this new string will flip its $(n-k)$-th bit. The $(n-k)$-th bit of the string $\overline{s}_{n-1} \dots \overline{s}_1 1$ corresponds to the bit $\overline{s}_{n - (n-k)} = \overline{s}_k$. Flipping this bit changes it to $s_k$. Thus, the transition in $G_n$ maps the string to:
\[
\overline{s}_{n-1} \dots \overline{s}_{k+1} s_k \overline{s}_{k-1} \dots \overline{s}_1 1.
\]
This is exactly the image we would get by applying the transformation for $Y$ to the successor of $s$ in $G_{n-1}$ (since the successor of $s$ in $G_{n-1}$ is obtained by flipping $s_k$). This confirms that the internal transitions within $Y$ are isomorphic to those in $G_{n-1}$.

The only exception in $Y$ is the string corresponding to the terminal state of $G_{n-1}$. The terminal state of $G_{n-1}$ is $00\dots0$ (which has zero $1$s, so the process stops). Under the mapping for $Y$, the state $00\dots0$ becomes $11\dots1$. In $G_n$, the string $11\dots1$ has exactly $n$ ones, so the operation flips its $n$-th bit, changing it to $11\dots10$. This exactly matches the single new edge from $Y$ to $X$ defined in our construction.

Since $G_{n-1}$ is assumed by induction to terminate at $00\dots0$, all paths in $Y$ eventually reach $11\dots1$, which then transitions to $11\dots10$ in $X$. From there, the process follows the rules of $X$ (which is isomorphic to $G_{n-1}$) and must eventually terminate at $00\dots0$. This proves that the process always terminates for any initial configuration in $G_n$.

To finish the problem, let $E_n$ denote the expected number of steps for a randomly chosen initial configuration in $G_n$. 
The vertices of $G_n$ are split equally between $X$ and $Y$. 
For a string starting in $X$, the expected number of steps to reach the terminal state $00\dots0$ is exactly $E_{n-1}$. 
For a string starting in $Y$, the process first traverses the internal edges of $Y$. Since $Y$ is isomorphic to $G_{n-1}$, the expected number of steps to reach the local sink $11\dots1$ is $E_{n-1}$. Once at $11\dots1$, the process transitions to $11\dots10$, and from there it takes exactly $n-1$ more steps to reach $00\dots0$ (since $11\dots10 \to 11\dots00 \to \dots \to 00\dots0$). Thus, from $11\dots1$, it takes exactly $n$ steps to finish. The expected number of steps for a string in $Y$ is therefore $E_{n-1} + n$.

Averaging over all $2^n$ configurations (half in $X$ and half in $Y$), we obtain the recurrence relation:
\[
E_n = \frac{1}{2} \left[ E_{n-1} + (E_{n-1} + n) \right] = E_{n-1} + \frac{n}{2}.
\]
With the base case $E_0 = 0$, we can solve this recurrence by induction:
\[
E_n = \sum_{i=1}^n \frac{i}{2} = \frac{1}{2} (1 + 2 + \dots + n) = \frac{1}{4} n(n+1).
\]
This completes the proof.

Answer: $E_n = \frac{1}{4}n(n+1)$

Grading guidelines:
(Partial) 1. Proved termination by exhibiting a strictly decreasing invariant (e.g. interpreting the configuration as a binary number / a sum of distinct powers of 2) and checking that one move always decreases it. 2. Set up a correct recursion/partition of the state space into two parts of size 2^{n-1} each which evolve like the (n−1)-coin process (e.g. identifying the two copies corresponding to the last coin being T/H after a suitable transformation). 3. Derived a correct recurrence for the expectation E_n of the form E_n = E_{n-1} + n/2 (or an equivalent averaging argument), even if not fully justified or not fully solved. (Almost) 1. Gave the full decomposition into two copies of G_{n-1} and obtained E_n = E_{n-1} + n/2, but made a localized mistake in verifying the unique connecting transition (typically the behaviour at the all-heads state) or in proving the isomorphism on one half. 2. Solved the recurrence to get E_n = n(n+1)/4 and proved termination, but left a minor gap in the step-count for the special path after reaching 11…1 (e.g. claiming it takes n or n−1 steps without a complete justification of the exact number). 3. Solution is almost complete, but made minor mistakes which are not negligible.