[ICPC2020济南A] Matrix Equation - 线性基
Description
给一个 A 矩阵 一个 B 矩阵(矩阵元素为 0 或 1),求有多少个 C 矩阵 满足 A X C = B . C (叉乘 和 直积)。(n le 200)
Solution
C 的每一列是相互无关的,分开考虑,转化为解 (n) 个线性方程组的个数,实际上就是求系数矩阵的秩,用 bitset 线性基即可
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
int a[205][205], b[205][205];
int qpow(int p, int q)
{
return (q & 1 ? p : 1) * (q ? qpow(p * p % mod, q / 2) : 1) % mod;
}
struct LinearBase
{
// bitset<205> a[205];
vector<bitset<205>> a;
LinearBase()
{
a.resize(205);
}
int ans = 0;
void insert(bitset<205> k)
{
for (int j = 204; j >= 0; j--)
{
if ((k >> j)[0])
{
if (a[j] == 0)
{
a[j] = k;
ans++;
break;
}
else
{
k ^= a[j];
}
}
}
}
};
signed main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> b[i][j];
int ans = 0;
for (int c = 1; c <= n; c++)
{
LinearBase lb;
for (int i = 1; i <= n; i++)
{
bitset<205> tmp;
for (int j = 1; j <= n; j++)
{
tmp[j] = a[i][j];
if (i == j && b[i][c])
tmp[j] = 1 - tmp[j];
}
lb.insert(tmp);
}
ans += n - lb.ans;
}
cout << qpow(2, ans) << endl;
}