CF1391D.505

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

Given a binary matrix aa consisting of nn rows and mm 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.

若一个二进制矩阵的每个偶数阶方子矩阵均包含奇数个 1,则称该矩阵为“好”矩阵。

给定一个由 nn 行 mm 列组成的二进制矩阵 aa,请确定使其变为“好”矩阵所需的最少修改单元格数目;若无法使其变为“好”矩阵,则报告不存在可行方案。

上述所有术语均采用其通常含义——其形式化定义请参见“注释”部分。

输入格式

The first line of input contains two integers nn and mm (1≤n≤m≤1061 \leq n \leq m \leq 10^6 and n⋅m≤106n\cdot m \leq 10^6) — the number of rows and columns in aa, respectively.

The following nn lines each contain mm characters, each of which is one of 0 and 1. If the jj-th character on the ii-th line is 1, then ai,j=1a_{i,j} = 1. Similarly, if the jj-th character on the ii-th line is 0, then ai,j=0a_{i,j} = 0.

输入的第一行包含两个整数 nn 和 mm(1≤n≤m≤1061 \leq n \leq m \leq 10^6 且 n⋅m≤106n\cdot m \leq 10^6),分别表示矩阵 aa 的行数和列数。

接下来的 nn 行,每行包含 mm 个字符,每个字符为 0 或 1。若第 ii 行的第 jj 个字符为 1,则 ai,j=1a_{i,j} = 1;若第 ii 行的第 jj 个字符为 0,则 ai,j=0a_{i,j} = 0。

输出格式

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

输出使 aa 变为“好”的数组所需修改的最少单元格数;如果根本无法实现,则输出 −1-1。

输入输出样例

  • 输入#1

    3 3
    101
    001
    110

    输出#1

    2
  • 输入#2

    7 15
    000100001010010
    100111010110001
    101101111100100
    010000111111010
    111010010100001
    000011001111101
    111111011010011

    输出#2

    -1

说明/提示

In the first case, changing a1,1a_{1,1} to 00 and a2,2a_{2,2} to 11 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 11 or 00.
  • A sub-matrix is described by 44 parameters — r1r_1, r2r_2, c1c_1, and c2c_2; here, 1≤r1≤r2≤n1 \leq r_1 \leq r_2 \leq n and 1≤c1≤c2≤m1 \leq c_1 \leq c_2 \leq m.
  • This sub-matrix contains all elements ai,ja_{i,j} that satisfy both r1≤i≤r2r_1 \leq i \leq r_2 and c1≤j≤c2c_1 \leq j \leq c_2.
  • A sub-matrix is, further, called an even length square if r2−r1=c2−c1r_2-r_1 = c_2-c_1 and r2−r1+1r_2-r_1+1 is divisible by 22.

在第一种情况下,只需将 a1,1a_{1,1} 改为 00、将 a2,2a_{2,2} 改为 11 即可。

可以验证:在第二种情况下,不存在使该矩阵变为“好”矩阵的方法。

一些定义——

  • 二进制矩阵是指其中每个元素均为 11 或 00 的矩阵。
  • 子矩阵由 44 个参数描述:r1r_1、r2r_2、c1c_1 和 c2c_2;其中满足 1≤r1≤r2≤n1 \leq r_1 \leq r_2 \leq n 且 1≤c1≤c2≤m1 \leq c_1 \leq c_2 \leq m。
  • 该子矩阵包含所有满足 r1≤i≤r2r_1 \leq i \leq r_2 且 c1≤j≤c2c_1 \leq j \leq c_2 的元素 ai,ja_{i,j}。
  • 进一步地,若 r2−r1=c2−c1r_2-r_1 = c_2-c_1 且 r2−r1+1r_2-r_1+1 能被 22 整除,则称该子矩阵为偶数长度的正方形子矩阵。

输入解题思路,AI测评打分。不知道怎么写?

首页