1119C.md (3311B)
1 Source: https://codeforces.com/problemset/problem/1119/C 2 3 C. Ramesses and Corner Inversion 4 5 time limit per test 6 7 1 second 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 Ramesses came to university to algorithms practice, and his professor, who is a fairly known programmer, gave him the following task. 22 23 You are given two matrices $A$ and $B$ of size $n \times m$, each of which consists of $0$ and $1$ only. You can apply the following operation to the matrix $A$ arbitrary number of times: take any submatrix of the matrix $A$ that has at least two rows and two columns, and invert the values in its corners (i.e. all corners of the submatrix that contain $0$, will be replaced by $1$, and all corners of the submatrix that contain $1$, will be replaced by $0$). You have to answer whether you can obtain the matrix $B$ from the matrix $A$. 24 25  An example of the operation. The chosen submatrix is shown in blue and yellow, its corners are shown in yellow. 26 27 Ramesses don't want to perform these operations by himself, so he asks you to answer this question. 28 29 A submatrix of matrix $M$ is a matrix which consist of all elements which come from one of the rows with indices $x_1, x_1+1, \ldots, x_2$ of matrix $M$ and one of the columns with indices $y_1, y_1+1, \ldots, y_2$ of matrix $M$, where $x_1, x_2, y_1, y_2$ are the edge rows and columns of the submatrix. In other words, a submatrix is a set of elements of source matrix which form a solid rectangle (i.e. without holes) with sides parallel to the sides of the original matrix. The corners of the submatrix are cells $(x_1, y_1)$, $(x_1, y_2)$, $(x_2, y_1)$, $(x_2, y_2)$, where the cell $(i,j)$ denotes the cell on the intersection of the $i$-th row and the $j$-th column. 30 31 Input 32 33 The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 500$) — the number of rows and the number of columns in matrices $A$ and $B$. 34 35 Each of the next $n$ lines contain $m$ integers: the $j$-th integer in the $i$-th line is the $j$-th element of the $i$-th row of the matrix $A$ ($0 \leq A_{ij} \leq 1$). 36 37 Each of the next $n$ lines contain $m$ integers: the $j$-th integer in the $i$-th line is the $j$-th element of the $i$-th row of the matrix $B$ ($0 \leq B_{ij} \leq 1$). 38 39 Output 40 41 Print "Yes" (without quotes) if it is possible to transform the matrix $A$ to the matrix $B$ using the operations described above, and "No" (without quotes), if it is not possible. You can print each letter in any case (upper or lower). 42 43 Examples 44 45 Input 46 47 ``` 48 3 3 49 0 1 0 50 0 1 0 51 1 0 0 52 1 0 0 53 1 0 0 54 1 0 0 55 ``` 56 57 Output 58 59 ``` 60 Yes 61 ``` 62 63 Input 64 65 ``` 66 6 7 67 0 0 1 1 0 0 1 68 0 1 0 0 1 0 1 69 0 0 0 1 0 0 1 70 1 0 1 0 1 0 0 71 0 1 0 0 1 0 1 72 0 1 0 1 0 0 1 73 1 1 0 1 0 1 1 74 0 1 1 0 1 0 0 75 1 1 0 1 0 0 1 76 1 0 1 0 0 1 0 77 0 1 1 0 1 0 0 78 0 1 1 1 1 0 1 79 ``` 80 81 Output 82 83 ``` 84 Yes 85 ``` 86 87 Input 88 89 ``` 90 3 4 91 0 1 0 1 92 1 0 1 0 93 0 1 0 1 94 1 1 1 1 95 1 1 1 1 96 1 1 1 1 97 ``` 98 99 Output 100 101 ``` 102 No 103 ``` 104 105 Note 106 107 The examples are explained below. 108 109  Example 1.  Example 2.  Example 3.