5. From $n^{2}$ bulbs, an $n \times n$ display was assembled. Each bulb has two states - on and off. When pressing any bulb, its state remains unchanged, while all bulbs in the same row or column change their state to the opposite. Initially, all bulbs on the display are off. Petya sequentially pressed several bulbs, as a result of which the display did not turn off completely. What is the smallest number of bulbs that can be on the display?
Answer: $2 n-2$.

Solution. Let's call the reversal of a set of bulbs the change of the state of all bulbs in this set to the opposite. Note two simple facts.

1) Pressing a bulb is equivalent to the reversal of the row and column in which this bulb is located. Indeed, with such reversals, the pressed bulb changes its state twice, i.e., does not change it, while the other bulbs in the same row or the same column change their state once.
2) When pressing several bulbs sequentially, the corresponding reversals can be performed in any order. Indeed, for any bulb, the number of state changes is equal to the total number of reversals of the rows and columns to which it belongs.

Let $k$ be the number of presses on the bulbs. We append to the $i$-th row and the $i$-th column the numbers $p_{i}$ and $q_{i}$, respectively, denoting the number of their reversals. Then

$$
\sum_{i=1}^{n} p_{i}+\sum_{j=1}^{n} q_{j}=2 k
$$

We can exclude even $p_{i}$ and $q_{j}$ from the left side, since an even number of reversals of rows or columns does not change their state. After this, the left side remains even. On the other hand, the sums on the left side will then contain only odd terms. Therefore, the number of terms in the first sum (denote it by $a$) has the same parity as the number of terms in the second sum (denote it by $b$). Thus, we have reversed $a$ different rows and $b$ different columns, where $a$ and $b$ have the same parity. In this case, the bulbs that will change their state compared to the initial state (i.e., turn on) are exactly those that are in a reversed row and an unreversed column or vice versa. The first type of bulbs is $a(n-b)$, and the second type is $b(n-a)$. Therefore, in the end, $a(n-b)+b(n-a)$ bulbs will be lit. We will show that $a(n-b)+b(n-a) \geqslant 2 n-2$. If $b=0$, then $a$ is even and not zero (otherwise all bulbs will be off). Then

$$
a(n-b)+b(n-a)=a n \geqslant 2 n
$$

If $b=n$, then $n-a$ is even and not zero (otherwise all bulbs will be off), from which

$$
a(n-b)+b(n-a)=n(n-a) \geqslant 2 n
$$

Similarly, the cases $a=0$ and $a=n$ are considered. For further consideration, note that $x y \geqslant x+y-1$ for $x, y \geqslant 1$. Therefore, for $1 \leqslant a, b \leqslant n-1$

$$
a(n-b)+b(n-a) \geqslant(a+(n-b)-1)+(b+(n-a)-1)=2 n-2
$$

It remains to show that exactly $2 n-2$ bulbs can be lit. For this, it is sufficient to press once on any bulb on the extinguished board.