Skip to content

Commit

Permalink
adds gauss xor and it's respective tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iagorrr committed Jul 14, 2024
1 parent 74103c0 commit d0de9c1
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 0 deletions.
45 changes: 45 additions & 0 deletions algorithms/math/gauss-xor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const int MAXXI = 2009;
using Equation = bitset<MAXXI>;
struct GaussXor {
vector<char> B;
vector<Equation> A;

void add(const Equation& ai, bool bi) {
A.push_back(ai);
B.push_back(bi);
}

pair<bool, Equation> solution() {
int cnt = 0, n = A.size();
Equation vis; vis.set();
Equation x;
for(int j = MAXXI-1, i; j >= 0; j--) {
for(i = cnt; i < n; i++) {
if (A[i][j]) break;
}
if (i == n) continue;
swap(A[i], A[cnt]), swap(B[i], B[cnt]);
i = cnt++; vis[j] = 0;
for(int k = 0; k < n; k++) {
if (i == k || !A[k][j]) continue;
A[k] ^= A[i]; B[k] ^= B[i];
}
}
x = vis;
for(int i = 0; i < n; i++) {
int acum = 0;
for(int j = 0; j < MAXXI; j++) {
if (!A[i][j]) continue;
if (!vis[j]) {
vis[j] = 1;
x[j] = acum^B[i];
}
acum ^= x[j];
}
if (acum != B[i]) return {false, Equation()};
}
return {true, x};
}

};

5 changes: 5 additions & 0 deletions algorithms/math/gauss-xor.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
\subsection{Gauss XOR Elimination / XOR-SAT}

Execute gaussian elimination with xor over the system $Ax = b$ in. The add method must receive a bitset indicating which variables are present in the equation, and the solution of the equation.

Time complexitiy: $O(\frac{nm^{2}}{64})$
5 changes: 5 additions & 0 deletions tests/math/gauss-xor/CF-GYM-102861-K/in/001.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
4 4
4 2
1 3
2 3
1 4
4 changes: 4 additions & 0 deletions tests/math/gauss-xor/CF-GYM-102861-K/in/002.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
4 3
4 2
2 3
1 2
6 changes: 6 additions & 0 deletions tests/math/gauss-xor/CF-GYM-102861-K/in/003.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
5 5
3 5
3 1
1 4
2 5
2 4
41 changes: 41 additions & 0 deletions tests/math/gauss-xor/CF-GYM-102861-K/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <bits/stdc++.h>
using namespace std;

#include "../../../../algorithms/math/gauss-xor.cpp"

int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);

int n, f;
cin >> n >> f;

vector<vector<int>> adj(n);
while (f--) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}

GaussXor gauss;
for (int i = 0; i < n; i++) {
Equation eq;

for (auto v : adj[i]) {
eq.set(v);
}

if (adj[i].size() & 1) {
eq.set(i);
gauss.add(eq, 0);
}
else {
gauss.add(eq, 1);
}
}

cout << "NY\n"[gauss.solution().first] << '\n';
}

1 change: 1 addition & 0 deletions tests/math/gauss-xor/CF-GYM-102861-K/out/001.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Y
1 change: 1 addition & 0 deletions tests/math/gauss-xor/CF-GYM-102861-K/out/002.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Y
1 change: 1 addition & 0 deletions tests/math/gauss-xor/CF-GYM-102861-K/out/003.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
N

0 comments on commit d0de9c1

Please sign in to comment.