Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F2 intersection #334

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions linear_algebra_matrix/f2_linear_space.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once
#include <algorithm>
#include <utility>
#include <vector>

template <class Int> struct f2_vector_space {
std::vector<Int> basis;

f2_vector_space() = default;

Int add(Int x) {
for (const Int &b : basis) x = std::min(x, x ^ b);

if (x) {
basis.push_back(x);
return x;
} else {
return Int(0);
}
}
};

std::vector<int> f2_intersection(const std::vector<int> &A, const std::vector<int> &B) {
f2_vector_space<long long> tmp;
for (int a : A) tmp.add(((long long)a << 32) + a);

std::vector<int> ret;

for (int b : B) {
long long v = (long long)b << 32;

auto u = tmp.add(v);
if (u < (1LL << 32)) ret.push_back(u);
}

return ret;
}
21 changes: 21 additions & 0 deletions linear_algebra_matrix/f2_linear_space.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: $\mathbb{F}_{2}$ linear space ($\mathbb{F}_{2}$ 線形空間)
documentation_of: ./f2_linear_space.hpp
---

$\mathbb{F}_{2}$ 線形空間に関する各種演算.

## 使用方法

`A` の元で張られる線形空間と `B` の元で張られる線形空間の共通部分の基底を一つ求める関数方法.

```cpp
int n, m;
vector<int> A(n), B(m);

vector<int> C = f2_intersection(A, B);
```

## 問題例

- [Library Checker: Intersection of F_2 vector spaces](https://judge.yosupo.jp/problem/intersection_of_f2_vector_spaces)
1 change: 0 additions & 1 deletion linear_algebra_matrix/hafnian.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ documentation_of: ./hafnian.hpp

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

2 changes: 1 addition & 1 deletion linear_algebra_matrix/linalg_bitset.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Linear algebra on $\mathbb{F}_{2}$ using std::bitset ($\mathbb{F}_{2}$ 線形代数)
title: Linear algebra on $\mathbb{F}_{2}$ using std::bitset (std::bitset を使用した $\mathbb{F}_{2}$ 線形代数)
documentation_of: ./linalg_bitset.hpp
---

Expand Down
31 changes: 31 additions & 0 deletions linear_algebra_matrix/test/f2_intersection.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#define PROBLEM "https://judge.yosupo.jp/problem/intersection_of_f2_vector_spaces"
#include "../f2_linear_space.hpp"

#include <iostream>
using namespace std;

int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);

int T;
cin >> T;

while (T--) {
int n;
cin >> n;
vector<int> A(n);
for (int &x : A) cin >> x;

int m;
cin >> m;
vector<int> B(m);
for (int &x : B) cin >> x;

auto C = f2_intersection(A, B);

cout << C.size();
for (int x : C) cout << ' ' << x;
cout << '\n';
}
}
Loading