A13578 | 505
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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 \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$ .
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 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 $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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted