Official

C - Grid Coloring 3 Editorial by evima


Consider the time sequence in reverse order and decide the operations starting from the last one. By doing so, the operations in the statement can be reinterpreted as follows:

Choose a cell as the center. For all uncolored cells in the same row or column as the center (including the center itself), color them in any single color.

The first operation amounts to coloring a cross-shaped region (the center’s row and column) with one color. From the second operation onward, by choosing a cell within the region colored in the first operation as the center, we can in effect perform exactly the following two types of operations, and we only need to consider these:

  • Choose a row that still has uncolored cells, and color all uncolored cells in that row with any single color.
  • Choose a column that still has uncolored cells, and color all uncolored cells in that column with any single color.

When counting the combinations of colors of the cells in a fully colored grid that can be achieved by these operations (we call those combinations pictures), simply counting the number of possible sequences of operations would overcount, since multiple different sequences might yield the same picture. For example, if two “row coloring” operations occur consecutively in the sequence, swapping their order (or considering them as happening simultaneously) does not change the resulting coloring.

To handle this, for each picture \(P\), define the following standard procedure for producing \(P\):

  • Repeat the following row simultaneous operation and column simultaneous operation alternately. That is, you perform \(R_1\) (the first row simultaneous operation), then \(C_1\) (the first column simultaneous operation), then \(R_2\), then \(C_2\), and so on, until \(P\) is formed.
    • Row simultaneous operation \(R_i\): choose one or more rows and color each of them in some color (colors can differ from row to row) at the same time;
    • Column simultaneous operation \(C_i\): choose one or more columns and color each of them in some color (colors can differ from column to column) at the same time.
  • In each \(R_i, C_i\), choose and color as many rows (or columns) as possible at that point to produce \(P\).

Each possible picture admits exactly one standard procedure. Therefore, to solve this problem, it suffices to count the valid standard procedures.

To do so, we need to count all combinations of the sets of rows and columns chosen in each \(R_i, C_i\) along with the colors used, subject to the following conditions:

  • First, to reflect that “the first operation colors a cross-shaped region consisting of one row and one column with a single color,” we require:
    • In \(R_1\), all chosen rows must be colored with the same color.
    • Among the columns colored in \(C_1\), at least one must be colored with the color used in \(R_1\).
  • Moreover, to reflect that we “choose as many rows (or columns) as possible at that point,” for any \(i \geq 1\), we require:
    • If in \(C_i\) all columns are colored with the same color, then \(R_{i+1}\) does not use that color. (If \(R_{i+1}\) were to color any row in that same color, that row could have been colored at the time of \(R_i\), causing a contradiction.)
    • If in \(R_{i+1}\) all rows are colored with the same color, then \(C_{i+1}\) does not use that color.
    • Except for \(R_1\), if a single simultaneous operation colors all remaining cells and finishes the picture, it must not use a single color for all of them. (If it could use a single color and complete the picture, then the previous operation could have done so as well, causing a contradiction.)

We can count the number of standard procedures satisfying the above conditions by dynamic programming. Concretely, let \(N \coloneqq \max \{H, W\}\). We construct a DP table with \(O(N^2)\) states keyed by, for example, the number of uncolored rows and columns remaining and whether there is a restriction on the color choice in the next operation. From each state, there are \(O(N)\) possible transitions (the number of rows or columns to color). This solves the problem in \(O(N^3)\) time overall.

posted:
last update: