Yahoo Programming Contest 2019 E - Odd Subrectangles
思路:
对于行方案固定的情况下,假设和为奇数的列为a个,和为偶数的列为b个,a+b = m
那么从奇数里面选奇数个,即C(a, 1) + C(a, 3) + C(a, 5) + ... = 2^(a-1)
从偶数里面随便选,即2^b
那么在存在奇数的情况下,列方案数为 2^(a+b-1) = 2^(m-1)
如果不存在奇数呢?那么问题就变为寻找使得所有列和都为偶数的行方案,即行向量异或起来为0
这种行方案数为2^(n-r), 其中 r 为矩阵的秩,求矩阵的秩用线性基
所以最后答案为 (2^n - 2^(n-r)) * 2^(m-1)
代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using namespace std; #define y1 y11 #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair<LL, LL> #define pli pair<LL, int> #define pii pair<int, int> #define piii pair<pii, int> #define pdd pair<double, double> #define mem(a, b) memset(a, b, sizeof(a)) #define debug(x) cerr << #x << " = " << x << "\n"; #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); //head const int N = 305; const int MOD = 998244353; bitset<N> a[N]; vector<bitset<N>> base; LL q_pow(LL n, LL k) { LL res = 1; while(k) { if(k&1) res = (res*n) % MOD; k >>= 1; n = (n*n) % MOD; } return res; } int main() { int n, m, t; scanf("%d %d", &n, &m); for (int i = 0; i < n; ++i) { a[i].reset(); for (int j = m-1; j >= 0; --j) { scanf("%d", &t); if(t) a[i].flip(j); } } for (int i = 0; i < n; i++) { for (bitset<N> b : base) { bitset<N> bb = b^a[i]; for (int j = m-1; j >= 0; j--) { if(bb[j] < a[i][j]) { a[i] = bb; break; } else if(bb[j] > a[i][j]) break; } } if(a[i].any()) base.pb(a[i]); } int cnt = (int)base.size(); printf("%lld\n", ((q_pow(2, n+m-1) - q_pow(2, n+m-1-cnt)) % MOD+ MOD) % MOD); return 0; }