CodeForces 405C Unusual Product【思维】

来源:互联网 发布:php图片木马下载 编辑:程序博客网 时间:2024/05/17 21:57

Description

Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix.

The dot product of two integer number vectors x and y of size n is the sum of the products of the corresponding components of the vectors. The unusual square of an n × n square matrix A is defined as the sum of n dot products. The i-th of them is the dot product of the i-th row vector and the i-th column vector in the matrix A.

Fortunately for Chris, he has to work only in GF(2)! This means that all operations (addition, multiplication) are calculated modulo 2. In fact, the matrix A is binary: each element of A is either 0 or 1. For example, consider the following matrix A:

The unusual square of A is equal to (1·1 + 1·0 + 1·1) + (0·1 + 1·1 + 1·0) + (1·1 + 0·1 + 0·0) = 0 + 1 + 1 = 0.

However, there is much more to the homework. Chris has to process q queries; each query can be one of the following:

  1. given a row index i, flip all the values in the i-th row in A;
  2. given a column index i, flip all the values in the i-th column in A;
  3. find the unusual square of A.

To flip a bit value w means to change it to 1 - w, i.e., 1 changes to 0 and 0 changes to 1.

Given the initial matrix A, output the answers for each query of the third type! Can you solve Chris's homework?

Input

The first line of input contains an integer n (1 ≤ n ≤ 1000), the number of rows and the number of columns in the matrix A. The next n lines describe the matrix: the i-th line contains n space-separated bits and describes the i-th row of A. The j-th number of the i-th line aij (0 ≤ aij ≤ 1) is the element on the intersection of thei-th row and the j-th column of A.

The next line of input contains an integer q (1 ≤ q ≤ 106), the number of queries. Each of the next q lines describes a single query, which can be one of the following:

  • i — flip the values of the i-th row;
  • i — flip the values of the i-th column;
  • 3 — output the unusual square of A.

Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.

Output

Let the number of the 3rd type queries in the input be m. Output a single string s of length m, where the i-th symbol of s is the value of the unusual square of A for the i-th query of the 3rd type as it appears in the input.

Sample Input

Input
31 1 10 1 11 0 01232 332 22 21 3331 22 11 13
Output
01001


/*    题意:给你一个矩阵,要求你计算矩阵乘法,规则和线代一样,行乘列求和,但是这里对结果模以2,有q次操作          每次操作有三种情况:  (q<=1e6)          1 i - 改变第i行(0->1;1->0)          2 i - 改变第i列(0->1;1->0)          3   - 查询矩阵乘法的值    类型:思维    分析:由A11*A11 + A12*A21 + A13*A31 +            A21*A12 + A22*A22 + A23*A32 +            A31*A13 + A32*A23 + A33*A33 =            A11*A11 + A22*A22 + A33*A33 + 2*A12*A21 + 2*A13*A31 + 2*A23*A32 ;            因为对结果mod2,所以 假设 A12*A21 = 1,那么*2之后mod2=0;假设 A12*A21 = 0,结果依然是0其他两个同理            所以最终结果转化为求解 ( A11*A11 + A22*A22 + A33*A33 ) mod 2的值            操作1和2每次会改变A11*A11、A22*A22、A33*A33中的某个值,每次我们把当前和的0/1的状态去异或1就是改变以后            和的状态,因为假设当前和为1,由1或3形成,那么改变以后的和显然是偶数,mod2以后就是0,1^1=0,满足要求            假设当前和为0,由0或2形成,那么改变以后的和显然是奇数,mod2以后就是1,1^0=1,满足要求            所以上面的做法得证.*/#include<cstdio>#include<iostream>#include<algorithm>#include<vector>#include<set>#include<cstring>using namespace std;typedef long long ll;const int maxn=1002;int a[maxn][maxn];int main(){    int n;    scanf("%d",&n);    int A = 0;    for (int i = 1; i <= n; i++){        for (int j = 1; j <= n; j++){            scanf("%d",&a[i][j]);            if (i==j) A^=a[i][j];        }    }    int q,x;    scanf("%d",&q);    while(q--){        scanf("%d",&x);        if (x==3)            printf("%d",A);        else{            scanf("%d",&x);            A^=1;        }    }    return 0;}




0 0
原创粉丝点击