Skip to content

Commit 36e565a

Browse files
committed
F2 determinant
1 parent c830874 commit 36e565a

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

linear_algebra_matrix/linalg_bitset.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,33 @@ template <int Wmax> int rank_gauss_jordan(int W, const std::vector<std::bitset<W
4343
return 0;
4444
}
4545

46+
// determinant of F2 matrix.
47+
// Return 0 if the matrix is singular, otherwise return 1.
48+
// Complexity: O(W^3 / 64)
49+
template <int Wmax> int f2_determinant(const std::vector<std::bitset<Wmax>> &M) {
50+
const int H = M.size();
51+
if (H > Wmax) return 0;
52+
53+
auto tmp = M;
54+
for (int h = 0; h < H; ++h) {
55+
int piv = -1;
56+
for (int j = h; j < H; ++j) {
57+
if (tmp.at(j).test(h)) {
58+
piv = j;
59+
break;
60+
}
61+
}
62+
if (piv == -1) return 0; // singular
63+
64+
if (piv != h) std::swap(tmp.at(piv), tmp.at(h));
65+
for (int hh = h + 1; hh < H; ++hh) {
66+
if (tmp.at(hh).test(h)) tmp.at(hh) ^= tmp.at(h);
67+
}
68+
}
69+
70+
return 1; // nonsingular
71+
}
72+
4673
template <int W1, int W2>
4774
std::vector<std::bitset<W2>>
4875
matmul(const std::vector<std::bitset<W1>> &A, const std::vector<std::bitset<W2>> &B) {

linear_algebra_matrix/linalg_bitset.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ vector<bool> b;
1616

1717
// Solve Ax = b (x: F_2^W)
1818
auto [feasible, x0, freedoms] = system_of_linear_equations<Wmax, vector<bool>>(A, b, W);
19+
20+
// Calc determinant (or check whether A is regular)
21+
int det = f2_determinant<dim>(mat);
1922
```
2023

2124
## 問題例
2225

2326
- [AOJ 2624: Graph Automata Player](https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2624)
2427
- [No.1421 国勢調査 (Hard) - yukicoder](https://yukicoder.me/problems/no/1421)
2528
- [AtCoder Beginner Contest 276 Ex - Construct a Matrix](https://atcoder.jp/contests/abc276/tasks/abc276_h) $2000 \times 8000$ 行列の線型方程式を解く.
29+
- [Library Checker: Determinant of Matrix (Mod 2)](https://judge.yosupo.jp/problem/matrix_det_mod_2)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#define PROBLEM "https://judge.yosupo.jp/problem/matrix_det_mod_2"
2+
#include "../linalg_bitset.hpp"
3+
4+
#include <bitset>
5+
#include <iostream>
6+
#include <string>
7+
#include <vector>
8+
9+
using namespace std;
10+
11+
int main() {
12+
cin.tie(nullptr);
13+
ios::sync_with_stdio(false);
14+
15+
constexpr int dim = 1 << 12;
16+
using BS = bitset<dim>;
17+
18+
int N;
19+
cin >> N;
20+
vector<BS> mat(N);
21+
for (auto &v : mat) {
22+
string s;
23+
cin >> s;
24+
v = BS(s);
25+
}
26+
27+
cout << f2_determinant<dim>(mat) << '\n';
28+
}

0 commit comments

Comments
 (0)