codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1660E.md (3706B)


      1 Source: https://codeforces.com/problemset/problem/1660/E
      2 
      3 E. Matrix and Shifts
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 You are given a binary matrix $A$ of size $n \times n$. Rows are numbered from top to bottom from $1$ to $n$, columns are numbered from left to right from $1$ to $n$. The element located at the intersection of row $i$ and column $j$ is called $A_{ij}$. Consider a set of $4$ operations:
     22 
     23 1. Cyclically shift all rows up. The row with index $i$ will be written in place of the row $i-1$ ($2 \le i \le n$), the row with index $1$ will be written in place of the row $n$.
     24 2. Cyclically shift all rows down. The row with index $i$ will be written in place of the row $i+1$ ($1 \le i \le n - 1$), the row with index $n$ will be written in place of the row $1$.
     25 3. Cyclically shift all columns to the left. The column with index $j$ will be written in place of the column $j-1$ ($2 \le j \le n$), the column with index $1$ will be written in place of the column $n$.
     26 4. Cyclically shift all columns to the right. The column with index $j$ will be written in place of the column $j+1$ ($1 \le j \le n - 1$), the column with index $n$ will be written in place of the column $1$.
     27 
     28  ![](https://espresso.codeforces.com/f3922cf1689c2176413d7f60a8fb3753e52b7e6a.png) The $3 \times 3$ matrix is shown on the left before the $3$-rd operation is applied to it, on the right — after. 
     29 
     30 You can perform an arbitrary (possibly zero) number of operations on the matrix; the operations can be performed in any order.
     31 
     32 After that, you can perform an arbitrary (possibly zero) number of new xor-operations:
     33 
     34 * Select any element $A_{ij}$ and assign it with new value $A_{ij} \oplus 1$. In other words, the value of $(A_{ij} + 1) \bmod 2$ will have to be written into element $A_{ij}$.
     35 
     36 Each application of this xor-operation costs one burl. Note that the $4$ shift operations — are free. These $4$ operations can only be performed before xor-operations are performed.
     37 
     38 Output the minimum number of burles you would have to pay to make the $A$ matrix unitary. A unitary matrix is a matrix with ones on the main diagonal and the rest of its elements are zeros (that is, $A_{ij} = 1$ if $i = j$ and $A_{ij} = 0$ otherwise).
     39 
     40 Input
     41 
     42 The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) —the number of test cases in the test.
     43 
     44 The descriptions of the test cases follow. Before each test case, an empty line is written in the input.
     45 
     46 The first line of each test case contains a single number $n$ ($1 \le n \le 2000$)
     47 
     48 This is followed by $n$ lines, each containing exactly $n$ characters and consisting only of zeros and ones. These lines describe the values in the elements of the matrix.
     49 
     50 It is guaranteed that the sum of $n^2$ values over all test cases does not exceed $4 \cdot 10^6$.
     51 
     52 Output
     53 
     54 For each test case, output the minimum number of burles you would have to pay to make the $A$ matrix unitary. In other words, print the minimum number of xor-operations it will take after applying cyclic shifts to the matrix for the $A$ matrix to become unitary.
     55 
     56 Example
     57 
     58 Input
     59 
     60 ```
     61 4
     62 
     63 3
     64 
     65 010
     66 
     67 011
     68 
     69 100
     70 
     71 5
     72 
     73 00010
     74 
     75 00001
     76 
     77 10000
     78 
     79 01000
     80 
     81 00100
     82 
     83 2
     84 
     85 10
     86 
     87 10
     88 
     89 4
     90 
     91 1111
     92 
     93 1011
     94 
     95 1111
     96 
     97 1111
     98 ```
     99 
    100 Output
    101 
    102 ```
    103 1
    104 0
    105 2
    106 11
    107 ```
    108 
    109 Note
    110 
    111 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.
    112 
    113 In the second test case, you can make a unitary matrix by applying the operation $2$ — cyclic shift of rows upward twice to the matrix.