Browse · MathNet
PrintIMO 2019 Shortlisted Problems
2019 counting and probability
Problem
Let be a positive integer. Harry has coins lined up on his desk, each showing heads or tails. He repeatedly does the following operation: if there are coins showing heads and , then he flips the coin over; otherwise he stops the process. (For example, the process starting with would be , which takes three steps.)
Letting denote the initial configuration (a sequence of 's and 's), write for the number of steps needed before all coins show . Show that this number is finite, and determine its average value over all possible initial configurations .

Letting denote the initial configuration (a sequence of 's and 's), write for the number of steps needed before all coins show . Show that this number is finite, and determine its average value over all possible initial configurations .
Solution
We represent the problem using a directed graph whose vertices are the length- strings of 's and 's. The graph features an edge from each string to its successor (except for , which has no successor). We will also write and .
The graph consists of a single vertex: the empty string. The main claim is that can be described explicitly in terms of : - We take two copies, and , of . - In , we take each string of coins and just append a to it. In symbols, we replace with . - In , we take each string of coins, flip every coin, reverse the order, and append an to it. In symbols, we replace with . - Finally, we add one new edge from to , namely .
We depict below, in a way which indicates this recursive construction:
We prove the claim inductively. Firstly, is correct as a subgraph of , as the operation on coins is unchanged by an extra at the end: if is sent to , then is sent to .
Next, is also correct as a subgraph of , as if has occurrences of , then has occurrences of , and thus (provided that ), if is sent to , then is sent to .
Finally, the one edge from to is correct, as the operation does send to .
To finish, note that the sequences in take an average of steps to terminate, whereas the sequences in take an average of steps to reach and then an additional steps to terminate. Therefore, we have We have from our description of . Thus, by induction, we have , which in particular is finite.
---
Alternative solution.
We consider what happens with configurations depending on the coins they start and end with. - If a configuration starts with , the last coins follow the given rules, as if they were all the coins, until they are all , then the first coin is turned over. - If a configuration ends with , the last coin will never be turned over, and the first coins follow the given rules, as if they were all the coins. - If a configuration starts with and ends with , the middle coins follow the given rules, as if they were all the coins, until they are all . After that, there are more steps: first coins are turned over in that order, then coins are turned over in that order.
As this covers all configurations, and the number of steps is clearly finite for 0 or 1 coins, it follows by induction on that the number of steps is always finite.
We define , where and are each one of or , to be the average number of steps over configurations of length restricted to those that start with , if is not , and that end with , if is not (so represents "either or "). The above observations tell us that, for : - . - . - (by using both the observations for and for ). - .
Now , so . Similarly, . So We have and , so by induction on we have .
---
Alternative solution.
Let be the number of heads in positions to inclusive (so is the total number of heads), and let be if the coin is a head, otherwise. Consider the function We claim that is the total number of times coin is turned over (which implies that the process terminates). Certainly when all coins are tails, and is always a nonnegative integer, so it suffices to show that when the coin is turned over (where ), goes down by and all the other are unchanged. We show this by splitting into cases: - If , and are unchanged, and both before and after the coin flip, so is unchanged. - If , both before and after the coin flip, and both and change by the same amount, so is unchanged. - If and the coin is heads, goes down by , as do both and ; so goes down by . - If and the coin is tails, goes up by , is unchanged and goes up by ; so goes down by .
We now need to compute the average value of The average value of the first term is , and that of the third term is . To compute the second term, we sum over choices for the total number of heads, and then over the possible values of , getting Now, in terms of trinomial coefficients, \sum_{j=0}^{n} j\binom{n}{j}=\sum_{j=1}^{n}\binom{n}{n-j, j-1,1\}=n \sum_{j=0}^{n-1}\binom{n-1}{j}=2^{n-1} n and \sum_{j=0}^{n}\binom{j}{2}\binom{n}{j}=\sum_{j=2}^{n}\binom{n}{n-j, j-2,2\}=\binom{n}{2} \sum_{j=0}^{n-2}\binom{n-2}{j}=2^{n-2}\binom{n}{2} . So the second term above is and the required average is
---
Alternative solution.
Harry has built a Turing machine to flip the coins for him. The machine is initially positioned at the coin, where there are heads (and the position before the first coin is considered to be the coin). The machine then moves according to the following rules, stopping when it reaches the position before the first coin: if the coin at its current position is , it flips the coin and moves to the previous coin, while if the coin at its current position is , it flips the coin and moves to the next position.
Consider the maximal sequences of consecutive moves in the same direction. Suppose the machine has consecutive moves to the next coin, before a move to the previous coin. After those moves, the coins flipped in those moves are all heads, as is the coin the machine is now at, so at least the next moves will all be moves to the previous coin. Similarly, consecutive moves to the previous coin are followed by at least consecutive moves to the next coin. There cannot be more than consecutive moves in the same direction, so this proves that the process terminates (with a move from the first coin to the position before the first coin).
Thus we have a (possibly empty) sequence giving the lengths of maximal sequences of consecutive moves in the same direction, where the final moves must be moves to the previous coin, ending before the first coin. We claim there is a bijection between initial configurations of the coins and such sequences. This gives as required, since each with will appear in half of the sequences, and will contribute to the number of moves when it does.
To see the bijection, consider following the sequence of moves backwards, starting with the machine just before the first coin and all coins showing tails. This certainly determines a unique configuration of coins that could possibly correspond to the given sequence. Furthermore, every coin flipped as part of the consecutive moves is also flipped as part of all subsequent sequences of consecutive moves, for all , meaning that, as we follow the moves backwards, each coin is always in the correct state when flipped to result in a move in the required direction. (Alternatively, since there are possible configurations of coins and possible such ascending sequences, the fact that the sequence of moves determines at most one configuration of coins, and thus that there is an injection from configurations of coins to such ascending sequences, is sufficient for it to be a bijection, without needing to show that coins are in the right state as we move backwards.)
---
Alternative solution.
We explicitly describe what happens with an arbitrary sequence of coins. Suppose that contain heads at positions .
Let be the minimal index such that . Then the first few steps will consist of turning over the coins in this order. After that we get a configuration with heads at the same positions as in the initial one, except for . This part of the process takes steps.
After that, the process acts similarly; by induction on the number of heads we deduce that the process ends. Moreover, if the disappear in order , the whole process takes steps.
Now let us find the total value of over all configurations with exactly heads. To sum up the above expression over those, notice that each number appears as exactly times. Thus Therefore, the total value of over all configurations is Hence the required average is .
The graph consists of a single vertex: the empty string. The main claim is that can be described explicitly in terms of : - We take two copies, and , of . - In , we take each string of coins and just append a to it. In symbols, we replace with . - In , we take each string of coins, flip every coin, reverse the order, and append an to it. In symbols, we replace with . - Finally, we add one new edge from to , namely .
We depict below, in a way which indicates this recursive construction:
We prove the claim inductively. Firstly, is correct as a subgraph of , as the operation on coins is unchanged by an extra at the end: if is sent to , then is sent to .
Next, is also correct as a subgraph of , as if has occurrences of , then has occurrences of , and thus (provided that ), if is sent to , then is sent to .
Finally, the one edge from to is correct, as the operation does send to .
To finish, note that the sequences in take an average of steps to terminate, whereas the sequences in take an average of steps to reach and then an additional steps to terminate. Therefore, we have We have from our description of . Thus, by induction, we have , which in particular is finite.
---
Alternative solution.
We consider what happens with configurations depending on the coins they start and end with. - If a configuration starts with , the last coins follow the given rules, as if they were all the coins, until they are all , then the first coin is turned over. - If a configuration ends with , the last coin will never be turned over, and the first coins follow the given rules, as if they were all the coins. - If a configuration starts with and ends with , the middle coins follow the given rules, as if they were all the coins, until they are all . After that, there are more steps: first coins are turned over in that order, then coins are turned over in that order.
As this covers all configurations, and the number of steps is clearly finite for 0 or 1 coins, it follows by induction on that the number of steps is always finite.
We define , where and are each one of or , to be the average number of steps over configurations of length restricted to those that start with , if is not , and that end with , if is not (so represents "either or "). The above observations tell us that, for : - . - . - (by using both the observations for and for ). - .
Now , so . Similarly, . So We have and , so by induction on we have .
---
Alternative solution.
Let be the number of heads in positions to inclusive (so is the total number of heads), and let be if the coin is a head, otherwise. Consider the function We claim that is the total number of times coin is turned over (which implies that the process terminates). Certainly when all coins are tails, and is always a nonnegative integer, so it suffices to show that when the coin is turned over (where ), goes down by and all the other are unchanged. We show this by splitting into cases: - If , and are unchanged, and both before and after the coin flip, so is unchanged. - If , both before and after the coin flip, and both and change by the same amount, so is unchanged. - If and the coin is heads, goes down by , as do both and ; so goes down by . - If and the coin is tails, goes up by , is unchanged and goes up by ; so goes down by .
We now need to compute the average value of The average value of the first term is , and that of the third term is . To compute the second term, we sum over choices for the total number of heads, and then over the possible values of , getting Now, in terms of trinomial coefficients, \sum_{j=0}^{n} j\binom{n}{j}=\sum_{j=1}^{n}\binom{n}{n-j, j-1,1\}=n \sum_{j=0}^{n-1}\binom{n-1}{j}=2^{n-1} n and \sum_{j=0}^{n}\binom{j}{2}\binom{n}{j}=\sum_{j=2}^{n}\binom{n}{n-j, j-2,2\}=\binom{n}{2} \sum_{j=0}^{n-2}\binom{n-2}{j}=2^{n-2}\binom{n}{2} . So the second term above is and the required average is
---
Alternative solution.
Harry has built a Turing machine to flip the coins for him. The machine is initially positioned at the coin, where there are heads (and the position before the first coin is considered to be the coin). The machine then moves according to the following rules, stopping when it reaches the position before the first coin: if the coin at its current position is , it flips the coin and moves to the previous coin, while if the coin at its current position is , it flips the coin and moves to the next position.
Consider the maximal sequences of consecutive moves in the same direction. Suppose the machine has consecutive moves to the next coin, before a move to the previous coin. After those moves, the coins flipped in those moves are all heads, as is the coin the machine is now at, so at least the next moves will all be moves to the previous coin. Similarly, consecutive moves to the previous coin are followed by at least consecutive moves to the next coin. There cannot be more than consecutive moves in the same direction, so this proves that the process terminates (with a move from the first coin to the position before the first coin).
Thus we have a (possibly empty) sequence giving the lengths of maximal sequences of consecutive moves in the same direction, where the final moves must be moves to the previous coin, ending before the first coin. We claim there is a bijection between initial configurations of the coins and such sequences. This gives as required, since each with will appear in half of the sequences, and will contribute to the number of moves when it does.
To see the bijection, consider following the sequence of moves backwards, starting with the machine just before the first coin and all coins showing tails. This certainly determines a unique configuration of coins that could possibly correspond to the given sequence. Furthermore, every coin flipped as part of the consecutive moves is also flipped as part of all subsequent sequences of consecutive moves, for all , meaning that, as we follow the moves backwards, each coin is always in the correct state when flipped to result in a move in the required direction. (Alternatively, since there are possible configurations of coins and possible such ascending sequences, the fact that the sequence of moves determines at most one configuration of coins, and thus that there is an injection from configurations of coins to such ascending sequences, is sufficient for it to be a bijection, without needing to show that coins are in the right state as we move backwards.)
---
Alternative solution.
We explicitly describe what happens with an arbitrary sequence of coins. Suppose that contain heads at positions .
Let be the minimal index such that . Then the first few steps will consist of turning over the coins in this order. After that we get a configuration with heads at the same positions as in the initial one, except for . This part of the process takes steps.
After that, the process acts similarly; by induction on the number of heads we deduce that the process ends. Moreover, if the disappear in order , the whole process takes steps.
Now let us find the total value of over all configurations with exactly heads. To sum up the above expression over those, notice that each number appears as exactly times. Thus Therefore, the total value of over all configurations is Hence the required average is .
Final answer
n(n+1)/4
Techniques
Expected valuesInvariants / monovariantsRecursion, bijectionInduction / smoothingAlgebraic properties of binomial coefficientsAlgorithms