505
Contest: Codeforces Round 663 (Div. 2)
Difficulty rating: 2000
Tags: bitmasks, brute force, constructive algorithms, dp, greedy, implementation

A binary matrix is called good if every even length square sub-matrix has an odd number of ones.

Given a binary matrix $$$a$$$ consisting of $$$n$$$ rows and $$$m$$$ columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all.

All the terms above have their usual meanings — refer to the Notes section for their formal definitions.

Input format:
The first line of input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq m \leq 10^6$$$ and $$$n\cdot m \leq 10^6$$$)  — the number of rows and columns in $$$a$$$, respectively.

The following $$$n$$$ lines each contain $$$m$$$ characters, each of which is one of 0 and 1. If the $$$j$$$-th character on the $$$i$$$-th line is 1, then $$$a_{i,j} = 1$$$. Similarly, if the $$$j$$$-th character on the $$$i$$$-th line is 0, then $$$a_{i,j} = 0$$$.

Output format:
Output the minimum number of cells you need to change to make $$$a$$$ good, or output $$$-1$$$ if it's not possible at all.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first case, changing $$$a_{1,1}$$$ to $$$0$$$ and $$$a_{2,2}$$$ to $$$1$$$ is enough.

You can verify that there is no way to make the matrix in the second case good.

Some definitions —

- A binary matrix is one in which every element is either $$$1$$$ or $$$0$$$.
- A sub-matrix is described by $$$4$$$ parameters — $$$r_1$$$, $$$r_2$$$, $$$c_1$$$, and $$$c_2$$$; here, $$$1 \leq r_1 \leq r_2 \leq n$$$ and $$$1 \leq c_1 \leq c_2 \leq m$$$.
- This sub-matrix contains all elements $$$a_{i,j}$$$ that satisfy both $$$r_1 \leq i \leq r_2$$$ and $$$c_1 \leq j \leq c_2$$$.
- A sub-matrix is, further, called an even length square if $$$r_2-r_1 = c_2-c_1$$$ and $$$r_2-r_1+1$$$ is divisible by $$$2$$$.

Example input:
3 3
101
001
110

Example output:
2

Example input:
7 15
000100001010010
100111010110001
101101111100100
010000111111010
111010010100001
000011001111101
111111011010011

Example output:
-1

Editorial:
Firstly, if $$$min(n,m) > 3$$$, then, no solution exists because this means the grid contains at least one $$$4\times4$$$ sub-matrix, which can further be decomposed into four $$$2\times2$$$ sub-matrices. Since all four of these $$$2\times2$$$ sub-matrices are supposed to have an odd number of ones, the union of them will have an even number of ones.
The problem, now, reduces to changing the least number of cells such that every $$$2\times2$$$ sub-matrix has an odd number of ones — this is possible to achieve for every valid grid. For example, for every even-indexed row, alternate the cells, and for every odd-indexed row, make all cells equal to $$$1$$$.
We will solve this reduction using dynamic programming.
We represent the $$$i^{th}$$$ column as a $$$n$$$-bit integer — $$$a_i$$$; let $$$dp(i,mask)$$$ be the minimum cells we have to flip to make the first $$$i$$$ columns valid, and the $$$i^{th}$$$ column is represented by $$$mask$$$. The transition is quite simple: $$$$$$dp(i,cmask) = min( dp(i-1,pmask)+bitcount(cmask \oplus a[i]), dp(i,cmask)).$$$$$$
The term $$$bitcount(cmask \oplus a[i])$$$ is equal to the number of positions where these two masks differ.
Please note that we only consider those pairs, $$$\{pmask,cmask\}$$$, that when put adjacent do not form a $$$2\times2$$$ sub-matrix with an even number of ones. To speed up the transition, they can be pre-calculated.
Time Complexity: $$$\mathcal{O}(m*2^{2n} + n*m)$$$