## Editorial for DMOPC '20 Contest 4 P2 - Beautiful Grids

**only**when stuck, and

**not to copy-paste code from it**. Please be respectful to the problem author and editorialist.

**Submitting an official solution before solving the problem yourself is a bannable offence.**

Author:

Define an odd row as a row with an odd sum, and an odd column is a column with odd sum.

For 10%, note that we need only output any valid sequence. Although many sequences can work, the simplest is to simply output , followed by the cells containing as specified in the input.

For 20%, we need only output the correct answer . If we count the number of odd columns and odd rows , our answer is . We will discover why later.

#### Subtask 1

Let us consider only the odd rows and odd columns. Note that any flip we perform flips the parity of the relevant row and column. We can iterate through the odd rows, and choose an arbitrary odd column to flip at the same time. However, what if there are no more odd columns? Then we can simply flip an arbitrary column. We can prove that is always even, so that our end result will be beautiful. Such a proof is left as an exercise to the reader.

Note that we may have some odd columns left over as well, which we can process by flipping an arbitrary row.

**Time Complexity:**

#### Subtask 2

We can optimize the solution to with a map. Implementation details are left as an exercise to the reader.

**Time Complexity:**

## Comments