16. Alternating Groups (from 9th grade, 3 points) A sequence consists of $k$ zeros and $m$ ones, arranged in a random order. Let's divide the sequence into alternating groups of zeros and ones (a group is a segment consisting of all identical digits standing consecutively) ${ }^{3}$. The total number of groups is a random variable. Find its expected value
Solution. For each element of the sequence, we define a random variable $I_{j}$ - an indicator that equals 1 if this element is the first in its group. Otherwise, $I_{j}=0$. Clearly, $I_{1}=1$. For all other indicators $I_{j}$, the probability of the event $I_{j}=1$ is the probability that the symbols at the $(j-1)$-th and $j$-th positions are different:

$$
\mathrm{P}\left(I_{j}=1\right)=\frac{k}{k+m} \cdot \frac{m}{k+m-1}+\frac{m}{k+m} \cdot \frac{k}{k+m-1}=\frac{2 k m}{(k+m)(k+m-1)}
$$

Therefore, $\mathrm{E}_{j}=\frac{2 k m}{(k+m)(k+m-1)}$. The total number of groups $X$ is equal to the number of first elements in the groups, that is,

$$
X=I_{1}+I_{2}+\ldots+I_{k+m}
$$[^2]

Let's move to the expectations:

$$
\mathrm{E} X=1+(k+m-1) \cdot \frac{2 k m}{(k+m)(k+m-1)}=1+\frac{2 k m}{k+m}
$$

Answer: $1+\frac{2 k m}{k+m}$.

Note. Interestingly, in this problem, the result is related to the harmonic mean of the number of ones and the number of zeros.