http://codeforces.com/problemset/problem/405/C

题意:

给出一个n*n的矩阵,有q个操作,输入3时,输出A ,A等于第i行乘以第i列的对应元素的和(mod2),输入1 x,表示将第x行的元素翻转(即0变成1,1变成0),输入2 x,表示将第x列的元素翻转.

思路:

根据A的计算方式可知A的最终结果只由左对角线上的元素将决定,如果左对角线上的元素为1的个数有奇数个(可通过异或计算),则A=1,否则A=0。翻转的时候每翻转一行或一列,都会改变对角线的元素,故结果应异或上1.

 #include <stdio.h>
#include <string.h>
const int N=;
int a[N][N]; int main()
{
int n;
scanf("%d",&n);
int A = ;
for (int i = ; i <= n; i++)
{
for (int j = ; 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==)
printf("%d",A);
else
{
scanf("%d",&x);
A^=;
}
}
return ;
}
05-11 12:59
查看更多