Counting using symmetry of expectations

Revision en1, by MEDAAA, 2026-06-02 04:04:13

Hello Codeforces! Today I want to share some thoughts on the magic of symmetry and linearity of expectation. We have all encountered counting problems that seem completely impossible to calculate directly. But there is a beautiful trick: if we use symmetry to convert a pure counting problem into an expected value problem, we unlock the ability to bypass massive formulas and solve the problem elegantly.

Before we begin, make sure you are familiar with the basics of:

Expected values (Obviously)

Indicator variables: An indicator variable is a simple binary variable that equals 1 if a specific event occurs, and 0 if it does not. The most useful property of an indicator variable is that its expected value is exactly equal to the probability of the event happening ($$$E[I] = P(I=1)$$$). This makes them the perfect atomic building blocks for counting complex occurrences.

Linearity of expectation: the expected value of a sum of random variables is always equal to the sum of their individual expected values, $$$E[X + Y] = E[X] + E[Y]$$$, this property is that it holds perfectly true even if the variables are dependent on each other.

Let us jump right into the topic by looking at the following example.

Counting Inversions in all possible permutations

Given a positive integer $$$n$$$, define $$$f(p)$$$ to be the count of inversions in the permutation $$$p$$$. Count the sum of $$$f(p)$$$ over all possible permutations of size $$$n$$$, $$$\sum_{p \in S_n} f(p)$$$.

Let us discuss the direct solution first. If we try to tackle this purely through combinatorics, looking at entire permutations all at once is too complex. Instead, we use the contribution to the sum technique. We focus on a specific pair of positions $$$(i, j)$$$ where $$$i \lt j$$$, and ask in how many permutations $$$p_i \gt p_j$$$. First, we need to choose the two actual numbers that will go into positions $$$i$$$ and $$$j$$$. There are $$$\binom{n}{2}$$$ ways to pick these two values from the set of numbers from 1 to $$$n$$$. Once we pick these two numbers, there is only one valid way to place them to form an inversion where the larger number must go to position $$$i$$$, and the smaller number must go to position $$$j$$$. We still have $$$n-2$$$ remaining numbers, and they can be arranged in the remaining $$$n-2$$$ positions in exactly $$$(n-2)!$$$ ways. So, for any fixed pair of positions $$$(i, j)$$$, exactly $$$\binom{n}{2} \times (n-2)! = \frac{n!}{2}$$$ permutations have an inversion at these two positions. Now, how many pairs of positions $$$(i, j)$$$ are there in total? There are $$$\binom{n}{2}$$$ pairs of positions. We multiply the number of position pairs by the number of inversions per pair to get our final sum $$$\frac{n! \cdot n(n-1)}{4}$$$.

Now, let us discuss the other approach, instead of counting the total sum directly, we can find the expected number of inversions in a single, uniformly random permutation, and then just multiply that expectation by the total number of permutations, $$$n!$$$. Note that this is 100% correct since if we try to choose a random permutation then every permutation is equal likely to be our choice, which is the symmetry. Let $$$X$$$ be the total number of inversions. We can break $$$X$$$ down into a sum of tiny indicator variables: let $$$X_{i,j}$$$ be a variable that equals 1 if $$$p_i \gt p_j$$$, and 0 otherwise

$$$X = \sum_{1 \le i \lt j \le n} X_{i,j}$$$

. By Linearity of Expectation, the expected value of the sum is the sum of the expected values:

$$$E[X] = \sum_{1 \le i \lt j \le n} E[X_{i,j}]$$$

Since these are the only two possibilities, the probability of $$$p_i \gt p_j$$$ is exactly $$$\frac{1}{2}$$$. Therefore, the expected value of any single indicator variable is just $$$\frac{1}{2}$$$. Thus, $$$E[X] = \frac{n(n-1)}{2} \times \frac{1}{2} = \frac{n(n-1)}{4}$$$. Therefore, the answer is $$$n! \times \frac{n(n-1)}{4}$$$.

Now let's see another harder example.

1426F - Number of Subsequences

In a nutshell, you are given a string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters $$$a$$$, $$$b$$$ and $$$c$$$ and question marks $$$?$$$. You need to replace every $$$?$$$ with one of the three letters. Your task is to count the total number of subsequences $$$abc$$$ in all resulting strings.

Hint 1
Hint 2
Solution
Code

838D - Airplane Arrangements

You have an airplane with $$$n$$$ seats in a single row and $$$m$$$ passengers $$$m \leq n$$$. Every passenger independently chooses an assigned seat from $$$1$$$ to $$$n$$$ and a walking direction either facing the front or facing the back of the plane. When it is their turn to board, they go straight to their assigned seat. If it is empty, they sit. If it is occupied, they keep walking in their chosen direction to take the very next available seat. Your goal is to find the total number of initial choices assigned seat and direction for all $$$m$$$ passengers where every single person successfully finds a seat without walking off either edge of the airplane.

Try to solve it first without symmetry of expectations, I don't know how but just try. Now, for our solution

Hint 1
Hint 2
Solution

I hope this blog helped in exploring the magic of expected values and symmetry for you. If you know of any other cool problems share it with us in the comments.

Tags expected value, basic math

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English MEDAAA 2026-06-02 04:04:13 13163 Initial revision (published)