Skip to content

Commit a330ab9

Browse files
authored
Merge pull request #334 from hitonanode/f2-intersection
F2 intersection
2 parents c09c07f + ad2b41e commit a330ab9

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
#include <algorithm>
3+
#include <utility>
4+
#include <vector>
5+
6+
template <class Int> struct f2_vector_space {
7+
std::vector<Int> basis;
8+
9+
f2_vector_space() = default;
10+
11+
Int add(Int x) {
12+
for (const Int &b : basis) x = std::min(x, x ^ b);
13+
14+
if (x) {
15+
basis.push_back(x);
16+
return x;
17+
} else {
18+
return Int(0);
19+
}
20+
}
21+
};
22+
23+
std::vector<int> f2_intersection(const std::vector<int> &A, const std::vector<int> &B) {
24+
f2_vector_space<long long> tmp;
25+
for (int a : A) tmp.add(((long long)a << 32) + a);
26+
27+
std::vector<int> ret;
28+
29+
for (int b : B) {
30+
long long v = (long long)b << 32;
31+
32+
auto u = tmp.add(v);
33+
if (u < (1LL << 32)) ret.push_back(u);
34+
}
35+
36+
return ret;
37+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: $\mathbb{F}_{2}$ linear space ($\mathbb{F}_{2}$ 線形空間)
3+
documentation_of: ./f2_linear_space.hpp
4+
---
5+
6+
$\mathbb{F}_{2}$ 線形空間に関する各種演算.
7+
8+
## 使用方法
9+
10+
`A` の元で張られる線形空間と `B` の元で張られる線形空間の共通部分の基底を一つ求める関数方法.
11+
12+
```cpp
13+
int n, m;
14+
vector<int> A(n), B(m);
15+
16+
vector<int> C = f2_intersection(A, B);
17+
```
18+
19+
## 問題例
20+
21+
- [Library Checker: Intersection of F_2 vector spaces](https://judge.yosupo.jp/problem/intersection_of_f2_vector_spaces)

linear_algebra_matrix/hafnian.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ documentation_of: ./hafnian.hpp
1313

1414
- [1] A. Björklund, "Counting Perfect Matchings as Fast as Ryser,
1515
Proc. of 23rd ACM-SIAM symposium on Discrete Algorithms, pp.914–921, 2012.
16-

linear_algebra_matrix/linalg_bitset.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Linear algebra on $\mathbb{F}_{2}$ using std::bitset ($\mathbb{F}_{2}$ 線形代数)
2+
title: Linear algebra on $\mathbb{F}_{2}$ using std::bitset (std::bitset を使用した $\mathbb{F}_{2}$ 線形代数)
33
documentation_of: ./linalg_bitset.hpp
44
---
55

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#define PROBLEM "https://judge.yosupo.jp/problem/intersection_of_f2_vector_spaces"
2+
#include "../f2_linear_space.hpp"
3+
4+
#include <iostream>
5+
using namespace std;
6+
7+
int main() {
8+
cin.tie(nullptr);
9+
ios::sync_with_stdio(false);
10+
11+
int T;
12+
cin >> T;
13+
14+
while (T--) {
15+
int n;
16+
cin >> n;
17+
vector<int> A(n);
18+
for (int &x : A) cin >> x;
19+
20+
int m;
21+
cin >> m;
22+
vector<int> B(m);
23+
for (int &x : B) cin >> x;
24+
25+
auto C = f2_intersection(A, B);
26+
27+
cout << C.size();
28+
for (int x : C) cout << ' ' << x;
29+
cout << '\n';
30+
}
31+
}

0 commit comments

Comments
 (0)