题解归档 - cf406A

题解归档 - cf406A

本文由 cf-code 本地题解库自动归档;公开内容以本地 AC/验证版本为准。

思路

cf406A - Unusual Product

sum_i dot(row_i, col_i) = sum_i sum_j a[i][j] a[j][i] in GF(2).

For i != j, pair (i,j) and (j,i) contributes twice the same product, so it cancels modulo 2. For i == j, contribution is a[i][i]^2 = a[i][i]. Therefore the answer is exactly xor/parity of the main diagonal.

Row flip i and column flip i both flip only one diagonal bit a[i][i] among the surviving terms, so the maintained answer toggles for every query of type 1 or 2.

代码

来源:problems/cf406A/solution.cpp

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1, x; j <= n; j++) {
            cin >> x;
            if (i == j) ans ^= x;
        }
    }
    int q;
    cin >> q;
    string out;
    out.reserve(q);
    while (q--) {
        int op;
        cin >> op;
        if (op == 3) {
            out.push_back(char('0' + ans));
        } else {
            int x;
            cin >> x;
            ans ^= 1;
        }
    }
    cout << out << '\n';
    return 0;
}
~  ~  The   End  ~  ~


 赏 
感谢您的支持,我会继续努力哒!
支付宝收款码
tips
文章二维码 分类标签:归档TypechoAutoUpload
文章标题:题解归档 - cf406A
文章链接:https://www.fangshaonian.cn/archives/336/
最后编辑:2026 年 6 月 28 日 19:07 By 方少年
许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
(*) 2 + 3 =
快来做第一个评论的人吧~