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?
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:
- 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++.
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 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
01001
题意:
给出 N(1 ~ 1000),后给出这 N X N 的矩阵,每个数字只可能是 0 和 1 两种状态。后给出 Q(1 ~ 10 ^ 6),代表有 Q 个操作,每个操作可以有以下方式:
1. 当是 1 时,给出 i,说明改变 i 行的状态;
2. 当是 2 时,给出 i,说明改变 i 列的状态;
3. 当是 3 时,输出矩阵 A X A 的结果,结果要 % 2 。
思路:
数学 + 位运算。A X A = a11 * a11 + a22 * a22 + a33 * a33 + 2 * (a12 * a21 + a13 * a31 + a23 * a32)。因为 % 2,所以 A X A = a11 * a11 + a22 * a22 + a33 * a33 ,所以无论改变行还是列,都只会改变一个数的状态而已。除此之外,对于 % 2:1 + 1 = 0,1 + 0 = 1,0 + 1 = 1,0 + 0 = 0。这样的话和异或的运算是一样的,所以直接异或就好,当改变一次状态就取反一次,这样的话就解决所有问题了。
AC:
#include <stdio.h> #define MAX 1005 int n; int num[MAX][MAX]; int main() { int q,k; scanf("%d",&n); for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++) scanf("%d",&num[i][j]); k = num[1][1]; for(int i = 2;i <= n;i++) k = k ^ num[i][i]; scanf("%d",&q); while(q--) { int kind; scanf("%d",&kind); if (kind == 1 || kind == 2) { int ans; scanf("%d",&ans); k = !k; } else printf("%d",k); } printf("\n"); return 0; }
相关推荐
新版闽教版六年级上Unit6 an unusual day练习题精选.doc
氮化反应机理及在材料科学中的应用,孙长庆,郑伟涛,This report presents consistent insight into the mechanism behind the unusual behavior of nitride compounds from the perspective of tetrahedron bond ...
The Unusual SkyBlock:Minecraft配布ワールド「The Unusual SkyBlock」用データパックリポジトリ
这个扩展标志着不同寻常的提交。 这是一个chrome插件,用于识别Github上存储库的提交历史中的异常提交,以及用于将特定提交标记为正常或异常的附加信息。 一旦您单击存储库的提交历史记录上显示的插件图标,它仅在...
1.1 Optical Flow Calculation 1.2 Creating Image Regions 2. Single-Level Hidden Markov Model 2.1 Definitions and Notation 2.2 HMM Parameter Estimation 3. Anomaly Detection
STCunio,即 system on chip unusual i/o,意思是“不一样的 IO”,谐音“STC 有你哦”, 采
简单的OA协同办公系统,简单的实现了用户管理、部门管理、岗位管理以及论坛相关的功能.
画弹出表单提交代码是一款jquery对话框插件制作css3动画
语音识别中提取LPC系数(线性预测系数),实测可用
Unusual Devices File for SanDisk SDDR-09 SmartMedia reader.
语言:English (United States) 此扩展标志着不寻常的提交 这是一个chrome插件,用于识别Github上存储库的提交历史中的异常提交,以及用于将特定提交标记为正常或异常的附加信息。 一旦您单击存储库的提交历史记录上...
语言:Bahasa Melayu,Deutsch,English,Français,Nederlands,Norsk,Tiếng Việt,Türkçe,català,dansk,eesti,español,hrvatski,italiano,latviešu,lietuvių,magyar,polski,português (Brasil),português ...
unusual observations. For undated standards, the latest edition applies. Explanation of the symbols used in the document: > - More than ≥ - More than or equal to ≤ - Less than or equal to Important...
该项目是通过引导的。 您将在下面找到一些有关如何执行常见任务的信息。 您可以在找到本指南的最新版本。目录自动格式化代码更改页面<title>... 使用AJAX请求获取数据与API后端集成节点Ruby on Rails 在开发中...
any unusual, suspicious and abnormal events happening within the observed scene. These may be pedestrians crossing a road in a wrong place, cars running on the red light, abandoned objects, a person ...
不寻常的文件 这是一个非常不寻常的文档生成工具。 为什么? 您将在示例中了解。 第一的。 在您的代码中编写注释。 /*--- @foo =@class Foo(@type string);...@classMethod Bar(@foo,@type string) @type array;...
The author has taken unusual care to motivate concepts and to simplify proofs. For example, the book presents - without having defined determinants - a clean proof that every linear operator on a ...
sdk LCS/Telegraphics Wintab* Interface Specification 1.1: 16- and 32-bit API Reference By Rick Poyner Revised February 11, 2012 This specification was developed in response to a perceived need for a...
针对传统算法中有关时间序列流不协调子序列计算代价比较高的问题,提出了一种快速发现Top-K不协调子序列的算法。该算法通过特殊的数据结构保留计算结果,避免了大量的重复计算,从而达到降低时间复杂度的目的;...
用卷积滤波器matlab代码物联网中异常天气的智能算法调查 1我们在论文中使用的derain算法 演算法 论文链接 项目链接 DDN 德兰网 LPNet 预备网 基于GP的SSL 测试执行时间 $ T_ {total} = ...Rain100L