Skip to content

Commit

Permalink
Add F2 intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
hitonanode committed May 3, 2024
1 parent c09c07f commit 64c16f3
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
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)
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_vector_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';
}
}

0 comments on commit 64c16f3

Please sign in to comment.