Binary Blocks (Codeforces 838A)
Tags: Others, brute force

You are given an image, that can be represented with a 2-d n by m grid of pixels. Each pixel of the image is either on or off, denoted by the characters "0" or "1", respectively. You would like to compress this image. You want to choose an integer k > 1 and split the image into k by k blocks. If n and m are not divisible by k, the image is padded with only zeros on the right and bottom so that they are divisible by k. Each pixel in each individual block must have the same value. The given image may not be compressible in its current state. Find the minimum number of pixels you need to toggle (after padding) in order for the image to be compressible for some k. More specifically, the steps are to first choose k, then the image is padded with zeros, then, we can toggle the pixels so it is compressible for this k. The image must be compressible in that state.

Input: The first line of input will contain two integers n, m (2 ≤ n, m ≤ 2 500), the dimensions of the image.The next n lines of input will contain a binary string with exactly m characters, representing the image.

Output: Print a single integer, the minimum number of pixels needed to toggle to make the image compressible.

Note: We first choose k = 2.The image is padded as follows: 001000101100110010000000We can toggle the image to look as follows: 001100001100000000000000We can see that this image is compressible for k = 2.

Sample Inputs:
3 5
00100
10110
11001

Sample Outputs:
5

Solution (Python):
from sys import stdin

def run(n, m, pixels):
    ans = 1 << 30
    acc = [[0] * (m + 1) for _ in range(n + 1)]
    for i in range(n):
        for j in range(m):
            acc[i + 1][j + 1] = acc[i + 1][j] + int(pixels[i][j])
        for j in range(m):
            acc[i + 1][j + 1] += acc[i][j + 1]
    for k in range(2, max(n, m) + 1):
        (r, c) = ((n + k - 1) // k, (m + k - 1) // k)
        res = 0
        for i in range(r):
            for j in range(c):
                (x, y) = (i * k, j * k)
                (x2, y2) = (min(x + k - 1, n - 1), min(y + k - 1, m - 1))
                zero = acc[x2 + 1][y2 + 1] - acc[x][y2 + 1] - acc[x2 + 1][y] + acc[x][y]
                res += min(zero, k * k - zero)
        ans = min(ans, res)
    print(ans)

def main():
    (n, m) = [int(x) for x in stdin.readline().split()]
    pixels = []
    for i in range(n):
        pixels.append(stdin.readline().strip())
    run(n, m, pixels)
if __name__ == '__main__':
    import os
    if os.path.exists('tmp.in'):
        stdin = open('tmp.in')
    main()

Solution (Python):
def debug(*a):
    return
    print('[DEBUG]', *a)
is_prime = [False, False] + [True] * (2600 - 2)
for i in range(2, 2600):
    if is_prime[i]:
        for j in range(i + i, 2600, i):
            is_prime[j] = False
(n, m) = map(int, input().split())
a = [None] * n
for i in range(n):
    a[i] = input()
b = [None] * (n + 1)
b[n] = [0] * (m + 1)
for i in range(n - 1, -1, -1):
    b[i] = [0] * (m + 1)
    for j in range(m - 1, -1, -1):
        b[i][j] = b[i + 1][j] + b[i][j + 1] - b[i + 1][j + 1] + int(a[i][j])
ans = None
for k in range(2, max(n, m) + 1):
    if is_prime[k]:
        kk = k * k
        aa = 0
        for i in range(0, n, k):
            for j in range(0, m, k):
                bb = b[i][j]
                if i + k < n:
                    bb -= b[i + k][j]
                if j + k < m:
                    bb -= b[i][j + k]
                if i + k < n and j + k < m:
                    bb += b[i + k][j + k]
                aa += min(bb, kk - bb)
        if ans == None or aa < ans:
            ans = aa
print(ans)

Solution (Python):
def debug(*a):
    return
    print('[DEBUG]', *a)
is_prime = [False, False] + [True] * (2600 - 2)
for i in range(2, 2600):
    if is_prime[i]:
        for j in range(i + i, 2600, i):
            is_prime[j] = False
(n, m) = map(int, input().split())
a = [None] * n
for i in range(n):
    a[i] = input()
b = [None] * (n + 1)
b[n] = [0] * (m + 1)
for i in range(n - 1, -1, -1):
    b[i] = [0] * (m + 1)
    for j in range(m - 1, -1, -1):
        b[i][j] = b[i + 1][j] + b[i][j + 1] - b[i + 1][j + 1] + int(a[i][j])
ans = None
for k in range(2, max(n, m) + 1):
    kk = k * k
    aa = 0
    for i in range(0, n, k):
        for j in range(0, m, k):
            bb = b[i][j]
            if i + k < n:
                bb -= b[i + k][j]
            if j + k < m:
                bb -= b[i][j + k]
            if i + k < n and j + k < m:
                bb += b[i + k][j + k]
            aa += min(bb, kk - bb)
    if ans == None or aa < ans:
        ans = aa
print(ans)