CF405C.Unusual Product

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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?

小 Chris 非常热爱线性代数。这一次,他被布置了一道关于方阵“非同寻常的平方”(unusual square)的作业题。

两个长度为 nn 的整数向量 xx 和 yy 的点积(dot product),定义为它们对应分量乘积之和。一个 n×nn \times n 方阵 AA 的非同寻常的平方定义为 nn 个点积之和;其中第 ii 项是矩阵 AA 的第 ii 行向量与第 ii 列向量的点积。

幸运的是,Chris 只需在二元域 GF(2)GF(2) 中运算!这意味着所有运算(加法、乘法)均对 22 取模。事实上,矩阵 AA 是二进制的:其每个元素非 00 即 11。例如,考虑如下矩阵 AA:

AA 的非同寻常的平方等于

(1⋅1+1⋅0+1⋅1)+(0⋅1+1⋅1+1⋅0)+(1⋅1+0⋅1+0⋅0)=0+1+1=0.(1\cdot1 + 1\cdot0 + 1\cdot1) + (0\cdot1 + 1\cdot1 + 1\cdot0) + (1\cdot1 + 0\cdot1 + 0\cdot0) = 0 + 1 + 1 = 0.

然而,这份作业远不止于此。Chris 还需要处理 qq 个查询;每个查询属于以下三种类型之一:

  1. 给定行索引 ii,将矩阵 AA 的第 ii 行所有值翻转(flip);
  2. 给定列索引 ii,将矩阵 AA 的第 ii 列所有值翻转(flip);
  3. 求矩阵 AA 的非同寻常的平方。

所谓翻转(flip)一个比特值 ww,即将其变为 1−w1 - w:11 变为 00,00 变为 11。

给定初始矩阵 AA,请输出所有第 3 类查询所对应的答案!你能帮 Chris 完成这份作业吗?

输入格式

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 a__ij (0 ≤ a__ij ≤ 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++.

输入的第一行包含一个整数 nn(1≤n≤10001 \leq n \leq 1000),表示矩阵 AA 的行数与列数。接下来的 nn 行描述该矩阵:第 ii 行包含 nn 个以空格分隔的比特位,表示矩阵 AA 的第 ii 行。第 ii 行中的第 jj 个数 aija_{ij}(0≤aij≤10 \leq a_{ij} \leq 1)是矩阵 AA 中第 ii 行与第 jj 列交点处的元素。

输入的下一行包含一个整数 qq(1≤q≤1061 \leq q \leq 10^6),表示查询的数量。接下来的 qq 行每行描述一个查询,查询类型如下:

  • 1 i — 翻转第 ii 行的所有值;
  • 2 i — 翻转第 ii 列的所有值;
  • 3 — 输出矩阵 AA 的“非同寻常平方”(unusual square)。

注意:由于输入和输出的数据量可能非常大,请勿在程序中使用低效的输入/输出方式。例如,在 C++ 中请勿使用 cin 和 cout。

输出格式

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.

设输入中第 3 类查询的数量为 mm。输出一个长度为 mm 的字符串 ss,其中 ss 的第 ii 个字符为输入中第 ii 个第 3 类查询所对应的矩阵 AA 的“特殊平方”值。

输入输出样例

  • 输入#1

    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

    输出#1

    01001

输入解题思路,AI测评打分。不知道怎么写?

首页