CF1391D.505
普及/提高-
通过率:0%
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 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.
输入格式
The first line of input contains two integers n and m ( 1≤n≤m≤106 and n⋅m≤106 ) — 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 ai,j=1 . Similarly, if the j -th character on the i -th line is 0, then ai,j=0 .
输出格式
Output the minimum number of cells you need to change to make a good, or output −1 if it's not possible at all.
输入输出样例
输入#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,1 to 0 and a2,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 — r1 , r2 , c1 , and c2 ; here, 1≤r1≤r2≤n and 1≤c1≤c2≤m .
- This sub-matrix contains all elements ai,j that satisfy both r1≤i≤r2 and c1≤j≤c2 .
- A sub-matrix is, further, called an even length square if r2−r1=c2−c1 and r2−r1+1 is divisible by 2 .