CodeForces 405C Unusual Product

                                                                    Unusual Product

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

CodeForces 405C Unusual Product_第1张图片

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 nlines 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 lineaij (0 ≤ aij ≤ 1) is the element on the intersection of the i-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 test(s)
input
3
1 1 1
0 1 1
1 0 0
12
3
2 3
3
2 2
2 2
1 3
3
3
1 2
2 1
1 1
3
output
01001

比赛时虽然想到了异或,但是没想到直接异或1,没发现规律。

题意:给出一个nn列的矩阵,有3中操作:

1 i 表示把第i行全部取反;

2 i表示把第i列全部取反;

输出这个矩阵乘以自身的乘积。

分析:因为这里的矩阵相乘是第一行乘以第一列,第二行乘以第二列……所以只有a[i][i] 乘了自己一次,其他都是互相乘了两次,既然乘了两次,那么只有两种情况,一种是(0*1 + 1*0)%2=0,另 一种是(1*1+1*1%2=0,这两种模2后都等于0,所以这些都不需要算了……既然是模2,那么直接用 异或 来算都可以了……当把每行或者每列的每个值都由01或者由10后,那么主对角线的肯定有一个值变了,所以结果值再异或1即可。

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int a[1005][1005];
int n;
int get_ans()
{
    int ans = 0;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            ans ^= a[i][j] * a[j][i];
    return ans;
}
int main()
{
    int i, j, q, t, k;
    while(~scanf("%d",&n))
    {
        for(i = 1; i <= n; i++)
            for(j = 1; j <= n; j++)
                scanf("%d",&a[i][j]);
        int ans = get_ans();
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d",&k);
            if(k == 3)
                printf("%d",ans);
            else
            {
                scanf("%d",&t);
                ans ^= 1;
            }
        }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(CodeForces 405C Unusual Product)