题解:CF2082A Binary Matrix

· · 题解

UPD 2026.2.10:我怎么贴错代码了。。。

考虑从给出的矩阵出发。假设原矩阵有 a 行的异或值不为零,有 b 列异或值不为零,则答案为 \max(a,b)。

简单证明一下:首先,你可以在异或值不为零的行、列交界处改掉,这样这一行、列异或值都变为零。随后,如果 a>b,就在异或值仍为一的所有行中,把随便一列全部改掉。反之亦然。

AC Code

#include<bits/stdc++.h>
using namespace std ;
const int MAXN = 105 ;
char str[MAXN][MAXN] ; 
signed main() {
    ios::sync_with_stdio(0) ;
    cin.tie(0) ;
    cout.tie(0) ;

    int t ;
    cin >> t ;
    while (t--) {
        int n , m ;
        cin >> n >> m ;
        for (int i = 1 ; i <= n ; i ++) {
            cin >> str[i] + 1 ;
        }

        int ans1 = 0 , ans2 = 0 ;
        for (int i = 1 ; i <= n ; i ++) {
            int cnt = 0 ;
            for (int j = 1 ; j <= m ; j ++) {
                cnt ^= (str[i][j] == '1') ;
            }
            ans1 += cnt ;
        }

        for (int j = 1 ; j <= m ; j ++) {
            int cnt = 0 ;
            for (int i = 1 ; i <=a n ; i ++) {
                cnt ^= (str[i][j] == '1') ;
            }
            ans2 += cnt ;
        }
        cout << max(ans1 , ans2) << "\n" ;
    }

    return 0 ;
}