unusual product

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:

given a row index i, flip all the values in the i-th row in A;
given a column index i, flip all the values in the i-th column in A;
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 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:

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 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.

    Examples

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
题意:
给出一个n*n的矩阵,矩阵中每个数字只可能是0或1,然后再给出一个数字p,接下来给出p次操作,每次输入数字1、2、3:
1.输入数字i,翻转第i行。
2.输入数字j,翻转第j列。
3.输出矩阵结果,对2取模。

思路:
这个题做了一个小时还是没做出来,主要是翻译没翻译好,再加上对数学本能的恐惧orz
矩阵算法为第一行乘以第一列加上第二行乘以第二列再加上第三行乘以第三列……直到最后一行乘以最后一列,即a11a11+a22a22……+2*(a12a21+a13a31+a23a32……),可以看出来除了对角线上的点其他每个点都乘了两遍,乘两遍时有三种情况,200,201,21*1,不管哪种最后对二取模都为0,所以可以直接异或,每翻转一次就取反一次。
还要注意的就是输入数据比较多所以不要用c++输入,会超时。

代码:
#include <stdio.h>

int s[1005][1005];
char ans[1000005]
int main
{
int n,q,k;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
scanf("%d",&s[i][j];
}
int sum = 0,tot=0;
memset(ans,0,sizeof(ans));
for(int i = 1; i <= n; i++)//求未进行操作时矩阵的值
{
sum += s[i][i]*s[i][i];
sum %= 2;
}
scanf("%d", &q);
for(int i = 0;i < q; i++)
{
scanf("%d", &k);
if(k == 1 || k == 2)
{
scanf("%d", &k);
sum = !sum;//进行异或取反
}
else ans[tot++]=sum+‘0’;
}
printf("%s\n",ans);
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值