505 (Codeforces 1391D)
Tags: Greedy, bitmasks

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: 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: Output the minimum number of cells you need to change to make $$$a$$$ good, or output $$$-1$$$ if it's not possible at all.

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$$$. 

Sample Inputs:
3 3
101
001
110

7 15
000100001010010
100111010110001
101101111100100
010000111111010
111010010100001
000011001111101
111111011010011

Sample Outputs:
2

-1

Solution (Python):
def f(l, a):
    (i, count) = (0, 0)
    for x in l:
        count += x not in a[i]
        i = (i + 1) % 2
    return count

def g(l, mx):
    return min([f(l, mx[i]) for i in range(len(mx))])

def two():
    l = [x[0] + x[1] for x in zip(input(), input())]
    mx = ([['00', '11'], ['10', '01']], [['10', '01'], ['00', '11']])
    return g(l, mx)

def three():
    l = [x[0] + x[1] + x[2] for x in zip(input(), input(), input())]
    mx = ([['100', '011'], ['110', '001']], [['110', '001'], ['100', '011']], [['111', '000'], ['010', '101']], [['010', '101'], ['000', '111']])
    return g(l, mx)
nm = input()
(n, m) = [int(x) for x in nm.split()]
if n == 1:
    print(0)
    exit()
if n > 3:
    print(-1)
    exit()
if n == 2:
    print(two())
    exit()
if n == 3:
    print(three())
    exit()

Solution (Python):
def solve_two(A, n, m):
    B = [(A[0][i] + A[1][i]) % 2 for i in range(m)]
    odd = 0
    even = 0
    for i in range(m):
        if B[i] == i % 2:
            odd += 1
        else:
            even += 1
    return min(odd, even)

def solve_three(A, n, m):
    BU = [(A[0][i] + A[1][i]) % 2 for i in range(m)]
    BD = [(A[1][i] + A[2][i]) % 2 for i in range(m)]
    oddodd = 0
    oddeven = 0
    evenodd = 0
    eveneven = 0
    for i in range(m):
        if BU[i] == i % 2:
            oddeven += 1
            oddodd += 1
        else:
            eveneven += 1
            evenodd += 1
        if BD[i] == i % 2:
            if BU[i] == i % 2:
                evenodd += 1
            else:
                oddodd += 1
        elif BU[i] == i % 2:
            eveneven += 1
        else:
            oddeven += 1
    return min([oddodd, oddeven, evenodd, eveneven])

def solve():
    (n, m) = map(int, input().split())
    A = [list(map(int, list(input()))) for _ in range(n)]
    if n >= 4 and m >= 4:
        return -1
    if n == 1:
        return 0
    if n > m:
        (n, m) = (m, n)
        A = list(zip(*A))
    ans = 0
    if n == 2:
        return solve_two(A, n, m)
    if n == 3:
        return solve_three(A, n, m)

def main():
    print(solve())
if __name__ == '__main__':
    main()

Solution (Python):
raw = input()
(n, m) = map(int, raw.split(' '))
mx = []
for ri in range(n):
    mx += [input()]
if n >= 4:
    print(-1)
if n == 1:
    print(0)
if n == 2:
    p = m // 2
    s = 'AB' * (p + 2)
    s1 = s[:m]
    s2 = s[1:m + 1]
    (r1, r2) = (0, 0)
    for i in range(m):
        o1 = int(mx[0][i] != mx[1][i])
        letter = 'AB'[o1]
        if letter != s1[i]:
            r1 += 1
        if letter != s2[i]:
            r2 += 1
    rez = min(r1, r2)
    print(rez)
if n == 3:
    p = m // 2
    s = 'AD' * (p + 2)
    s1 = s[:m]
    s2 = s[1:m + 1]
    s = 'BC' * (p + 2)
    s3 = s[:m]
    s4 = s[1:m + 1]
    (r1, r2, r3, r4) = (0, 0, 0, 0)
    for i in range(m):
        o1 = int(mx[0][i] != mx[1][i])
        o2 = int(mx[1][i] != mx[2][i])
        letter = 'ABCD'[o1 * 2 + o2]
        if letter != s1[i]:
            r1 += 1
        if letter != s2[i]:
            r2 += 1
        if letter != s3[i]:
            r3 += 1
        if letter != s4[i]:
            r4 += 1
    rez = min(min(r1, r2), min(r3, r4))
    print(rez)