Skip to content

Commit a94d97a

Browse files
committed
Update bitpack + inv matrix mod 2 test
1 parent 17222cc commit a94d97a

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

cp-algo/data_structures/bitpack.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ namespace cp_algo::data_structures {
4545
int operator[](size_t i) const {
4646
return (data[i / bits_per_block] >> (i % bits_per_block)) & 1;
4747
}
48+
void set(size_t i) {
49+
data[i / bits_per_block] |= 1ULL << (i % bits_per_block);
50+
}
4851

4952
std::string to_string() const {
5053
std::string res(blocks * bits_per_block, '0');
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// @brief Inverse Matrix (Mod 2)
2+
#define PROBLEM "https://judge.yosupo.jp/problem/inverse_matrix_mod_2"
3+
#pragma GCC optimize("Ofast,unroll-loops")
4+
#include "cp-algo/data_structures/bitpack.hpp"
5+
#include <bits/stdc++.h>
6+
7+
using namespace std;
8+
using namespace cp_algo::data_structures;
9+
10+
const int maxn = 1 << 13;
11+
bitpack<maxn> a[maxn];
12+
13+
void solve() {
14+
int n;
15+
cin >> n;
16+
string row;
17+
vector<int> lead(n);
18+
for(int i = 0; i < n; i++) {
19+
cin >> row;
20+
a[i] = row;
21+
a[i].set(n + i);
22+
for(int j = 0; j < i; j++) {
23+
if(a[i][lead[j]]) {
24+
a[i].xor_hint(a[j], lead[j]);
25+
}
26+
}
27+
lead[i] = a[i].ctz();
28+
if(lead[i] >= n) {
29+
cout << -1 << "\n";
30+
return;
31+
}
32+
for(int j = 0; j < i; j++) {
33+
if(a[j][lead[i]]) {
34+
a[j].xor_hint(a[i], lead[i]);
35+
}
36+
}
37+
}
38+
for(int i = 0; i < n; i++) {
39+
while(lead[i] != i) {
40+
swap(a[i], a[lead[i]]);
41+
swap(lead[i], lead[lead[i]]);
42+
}
43+
cout << a[i].to_string().substr(n, n) << "\n";
44+
}
45+
}
46+
47+
signed main() {
48+
//freopen("input.txt", "r", stdin);
49+
ios::sync_with_stdio(0);
50+
cin.tie(0);
51+
int t = 1;
52+
while(t--) {
53+
solve();
54+
}
55+
}

0 commit comments

Comments
 (0)