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 the same, while all bulbs in the same row or column change their state to the opposite. Initially, all bulbs on the display are off. Vasya sequentially pressed several bulbs, none of which lie in the same row or column. What is the maximum number of bulbs Vasya could have lit?
Answer: $\frac{n^{2}}{2}$ for even $n$ and $\frac{n^{2}-1}{2}$ for odd $n$.

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, that is, it 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.

Suppose $k$ presses on bulbs were made. Then we reversed $k$ different rows and $k$ different columns. 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. There are $k(n-k)$ of such bulbs, so in the end, $2 k(n-k)$ bulbs will be lit. We will show that

$$
2 k(n-k) \leqslant \frac{n^{2}}{2} \text { for even } n \text { and } 2 k(n-k) \leqslant \frac{n^{2}-1}{2} \text { for odd } n .
$$

Consider the expression $2 k(n-k)$ as a quadratic trinomial in terms of $k$. Its graph is a parabola with vertex at $k=\frac{n}{2}$. Since the coefficient of $k^{2}$ is negative, the maximum value of the trinomial is achieved at the vertex and is equal to $\frac{n^{2}}{2}$. Then $2 k(n-k) \leqslant \frac{n^{2}}{2}$, and the first inequality $(*)$ is proved. To justify the second, we need to note that the number $2 k(n-k)$ is an integer, so

$$
2 k(n-k) \leqslant\left[\frac{n^{2}}{2}\right]=\frac{n^{2}-1}{2}
$$

It remains to show that equalities can be achieved in the inequalities $(*)$. Press $\left[\frac{n}{2}\right]$ different bulbs on the main diagonal of the board in the initial state. For even $n$, we will get $\frac{n^{2}}{2}$ lit bulbs, and for odd $n$, the number of bulbs will be

$$
2 \cdot \frac{n-1}{2} \cdot\left(n-\frac{n-1}{2}\right)=\frac{n^{2}-1}{2}
$$