Codeforces 406A Unusual Product【暴力找规律】

来源:互联网 发布:网络ssid没有wlan 编辑:程序博客网 时间:2024/06/07 03:35

A. 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 theunusual square of a square matrix.

The dot product of two integer number vectorsx and y of sizen is the sum of the products of the corresponding components of the vectors. Theunusual square of an n × n square matrix A is defined as the sum ofn dot products. The i-th of them is the dot product of the i-th row vector and thei-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 matrixA 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 thei-th row in A;
  2. given a column index i, flip all the values in thei-th column in A;
  3. find the unusual square of A.

To flip a bit value w means to change it to1 - 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 matrixA. The next n lines describe the matrix: thei-th line contains n space-separated bits and describes the i-th row ofA. The j-th number of thei-th line aij (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 nextq lines describes a single query, which can be one of the following:

  • 1 i — flip the values of the i-th row;
  • 2 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 lengthm, where the i-th symbol ofs is the value of the unusual square of A for the i-th query of the 3rd type as it appears in the input.

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

题目大意:

给你一个N*N大的一个矩阵,其矩阵的值为第一行*第一列+第二行*第二列+.......................的值。

给你三种操作:

1、翻转一行的数。

2、翻转一列的数。

3、查询矩阵的值。


思路:


1、读完题就觉得是XJB搞的题,XJB推了半天也是没有什么思路,仔细观察了一下样例输入输出,感觉翻转一次,无论如何翻转,其值就会置返、


2、然后按照观察得到的结论写了一发暴力代码,跑了很多数据,发现是没差的,然后就没有然后了。


Ac代码:

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











0 0
原创粉丝点击