CF1660E.Matrix and Shifts

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

You are given a binary matrix AA of size n×nn \times n . Rows are numbered from top to bottom from 11 to nn , columns are numbered from left to right from 11 to nn . The element located at the intersection of row ii and column jj is called AijA_{ij} . Consider a set of 44 operations:

  1. Cyclically shift all rows up. The row with index ii will be written in place of the row i1i-1 ( 2in2 \le i \le n ), the row with index 11 will be written in place of the row nn .
  2. Cyclically shift all rows down. The row with index ii will be written in place of the row i+1i+1 ( 1in11 \le i \le n - 1 ), the row with index nn will be written in place of the row 11 .
  3. Cyclically shift all columns to the left. The column with index jj will be written in place of the column j1j-1 ( 2jn2 \le j \le n ), the column with index 11 will be written in place of the column nn .
  4. Cyclically shift all columns to the right. The column with index jj will be written in place of the column j+1j+1 ( 1jn11 \le j \le n - 1 ), the column with index nn will be written in place of the column 11 .

The 3×33 \times 3 matrix is shown on the left before the 33 -rd operation is applied to it, on the right — after.You can perform an arbitrary (possibly zero) number of operations on the matrix; the operations can be performed in any order.

After that, you can perform an arbitrary (possibly zero) number of new xor-operations:

  • Select any element AijA_{ij} and assign it with new value Aij1A_{ij} \oplus 1 . In other words, the value of (Aij+1)mod2(A_{ij} + 1) \bmod 2 will have to be written into element AijA_{ij} .

Each application of this xor-operation costs one burl. Note that the 44 shift operations — are free. These 44 operations can only be performed before xor-operations are performed.

Output the minimum number of burles you would have to pay to make the AA matrix unitary. A unitary matrix is a matrix with ones on the main diagonal and the rest of its elements are zeros (that is, Aij=1A_{ij} = 1 if i=ji = j and Aij=0A_{ij} = 0 otherwise).

输入格式

The first line of the input contains an integer tt ( 1t1041 \le t \le 10^4 ) —the number of test cases in the test.

The descriptions of the test cases follow. Before each test case, an empty line is written in the input.

The first line of each test case contains a single number nn ( 1n20001 \le n \le 2000 )

This is followed by nn lines, each containing exactly nn characters and consisting only of zeros and ones. These lines describe the values in the elements of the matrix.

It is guaranteed that the sum of n2n^2 values over all test cases does not exceed 41064 \cdot 10^6 .

输出格式

For each test case, output the minimum number of burles you would have to pay to make the AA matrix unitary. In other words, print the minimum number of xor-operations it will take after applying cyclic shifts to the matrix for the AA matrix to become unitary.

输入输出样例

  • 输入#1

    4
    
    3
    010
    011
    100
    
    5
    00010
    00001
    10000
    01000
    00100
    
    2
    10
    10
    
    4
    1111
    1011
    1111
    1111

    输出#1

    1
    0
    2
    11

说明/提示

In the first test case, you can do the following: first, shift all the rows down cyclically, then the main diagonal of the matrix will contain only "1". Then it will be necessary to apply xor-operation to the only "1" that is not on the main diagonal.

In the second test case, you can make a unitary matrix by applying the operation 22 — cyclic shift of rows upward twice to the matrix.

首页