codeforces C. Unusual Product(数学?)

博客围绕二进制 n×n 矩阵的“不寻常平方”问题展开。介绍了“不寻常平方”的定义,给出三种操作,包括翻转行或列元素、输出“不寻常平方”。分析得出只需考虑对角线上元素,且每次翻转操作会改变结果,可通过异或 1 处理,避免超时。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. 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 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. — flip the values of the i-th row;
  2. — 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.

分析

给出一个二进制 n×n 矩阵(矩阵元素只有 1 和 0 ),然后题目又给出了一个 “不寻常平方”,具体操作为 ∑ i , j = 1 n   a i j × a j i ( m o d 2 ) \sum_{i,j=1}^{n}\ a_{ij} × a_{ji}(mod 2) i,j=1n aij×aji(mod2)
我们可以发现,i=a,j=b 与 i=b,j=a时(a≠b)的得数是一样的;相当于 2(aij×aji)(mod 2);那么无论 (aij×aji)等于 0 还是 1 ,乘上 2 再取模 2 它都只能等于 0 了。

也就是说,影响最终结果的就只有 i=j=t 时的情况;即 att × att 的和:
∑ t = 1 n   a t t × a t t ( m o d 2 ) \sum_{t=1}^{n}\ a_{tt} × a_{tt}(mod 2) t=1n att×att(mod2)
然后题目给出了三种操作;
操作1或2是对某行或某列上的元素进行变换,使得 0->1 ,1->0;操作三是输出这个题目定义的 “不寻常平方”;从上面知道我们只需要考虑对角线上的元素就行了。

然而因为询问量非常巨大,我们需要进一步化简;每一次操作1或2都会改变对角线上的一个元素,进而使 “不寻常平方” 取模2的结果由0->1 或1->0,可以直接一开始就算出最终的结果,每有一次操作1或2就使得结果改变;改变可以直接异或1,每有操作3就直接输出结果即可;

虽然不难,但感觉有点吃时间,处理得太差会超时;

代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#define MS(X) memset(X,0,sizeof(X))
#define MSC(X) memset(X,-1,sizeof(X))
typedef long long LL;
using namespace std;
int st[1001][1001];
int save[1000005];
int main(){
    int n,p=0,md;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            scanf("%d",&st[i][j]);
            if(i==j) p^=st[i][j];
        }
    }
    int t,cl,i=0;
    scanf("%d",&t);
    for(int i=0;i<t;i++){
        scanf("%d",&save[i]);
        if(save[i]!=3) scanf("%d",&cl);
    }
    for(int i=0;i<t;i++){
        if(save[i]==3) printf("%d",p);
        else p^=1;
    }
    return 0;
}
### Codeforces Div.2 比赛难度介绍 Codeforces Div.2 比赛主要面向的是具有基础编程技能到中级水平的选手。这类比赛通常吸引了大量来自全球不同背景的参赛者,包括大学生、高中生以及一些专业人士。 #### 参加资格 为了参加 Div.2 比赛,选手的评级应不超过 2099 分[^1]。这意味着该级别的竞赛适合那些已经掌握了一定算法知识并能熟练运用至少一种编程语言的人群参与挑战。 #### 题目设置 每场 Div.2 比赛一般会提供五至七道题目,在某些特殊情况下可能会更多或更少。这些题目按照预计解决难度递增排列: - **简单题(A, B 类型)**: 主要测试基本的数据结构操作和常见算法的应用能力;例如数组处理、字符串匹配等。 - **中等偏难题(C, D 类型)**: 开始涉及较为复杂的逻辑推理能力和特定领域内的高级技巧;比如图论中的最短路径计算或是动态规划入门应用实例。 - **高难度题(E及以上类型)**: 对于这些问题,则更加侧重考察深入理解复杂概念的能力,并能够灵活组合多种方法来解决问题;这往往需要较强的创造力与丰富的实践经验支持。 对于新手来说,建议先专注于理解和练习前几类较容易的问题,随着经验积累和技术提升再逐步尝试更高层次的任务。 ```cpp // 示例代码展示如何判断一个数是否为偶数 #include <iostream> using namespace std; bool is_even(int num){ return num % 2 == 0; } int main(){ int number = 4; // 测试数据 if(is_even(number)){ cout << "The given number is even."; }else{ cout << "The given number is odd."; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值