C++ Library for Competitive Programming
#include "emthrm/math/matrix/binary_matrix/gauss_jordan.hpp"
有限体 $\mathbb{F}_2$ 上の行列である。
template <int N>
struct BinaryMatrix;
N
:列数の最大値名前 | 効果・戻り値 |
---|---|
explicit BinaryMatrix(const int m, const int n = N, const bool def = false); |
初期値 $\mathrm{def}$ の $M \times N$ 型バイナリ行列を表すオブジェクトを構築する。 |
int nrow() const; |
$M$ |
int ncol() const; |
$N$ |
BinaryMatrix pow(long long exponent) const; |
$A^\mathrm{exponent}$ |
inline const std::bitset<N>& operator[](const int i) const inline std::bitset<N>& operator[](const int i);
|
$A$ の $i$ 行目 |
BinaryMatrix& operator=(const BinaryMatrix& x); |
代入演算子 |
BinaryMatrix& operator+=(const BinaryMatrix& x); BinaryMatrix operator+(const BinaryMatrix& x) const;
|
加算 |
BinaryMatrix& operator*=(const BinaryMatrix& x); BinaryMatrix operator*(const BinaryMatrix& x) const;
|
乗算 |
名前 | 戻り値 | 備考 |
---|---|---|
template <bool IS_EXTENDED = false, int N> int gauss_jordan(BinaryMatrix<N>* a);
|
行列 $A$ のランク |
is_extended は $A$ が拡大係数行列かを表す。$A$ は行階段形に変形される。 |
名前 | 戻り値 |
---|---|
template <int N> std::vector<bool> linear_equation(const BinaryMatrix<N>& a, const std::vector<bool>& b);
|
$A \boldsymbol{x} = \boldsymbol{b}$ を満たす $\boldsymbol{x}$。ただし解なしのときは空配列を返す。 |
名前 | 戻り値 |
---|---|
template <int N> BinaryMatrix<N> inverse_matrix(const BinaryMatrix<N>& a);
|
行列 $A$ の逆行列。ただし存在しないときは空行列を返す。 |
#ifndef EMTHRM_MATH_MATRIX_BINARY_MATRIX_GAUSS_JORDAN_HPP_
#define EMTHRM_MATH_MATRIX_BINARY_MATRIX_GAUSS_JORDAN_HPP_
#include <utility>
#include "emthrm/math/matrix/binary_matrix/binary_matrix.hpp"
namespace emthrm {
template <bool IS_EXTENDED = false, int N>
int gauss_jordan(BinaryMatrix<N>* a) {
const int m = a->nrow(), n = a->ncol();
int rank = 0;
for (int col = 0; col < (IS_EXTENDED ? n - 1 : n); ++col) {
int pivot = -1;
for (int row = rank; row < m; ++row) {
if ((*a)[row][col]) {
pivot = row;
break;
}
}
if (pivot == -1) continue;
std::swap((*a)[rank], (*a)[pivot]);
for (int row = 0; row < m; ++row) {
if (row != rank && (*a)[row][col]) (*a)[row] ^= (*a)[rank];
}
++rank;
}
return rank;
}
} // namespace emthrm
#endif // EMTHRM_MATH_MATRIX_BINARY_MATRIX_GAUSS_JORDAN_HPP_
#line 1 "include/emthrm/math/matrix/binary_matrix/gauss_jordan.hpp"
#include <utility>
#line 1 "include/emthrm/math/matrix/binary_matrix/binary_matrix.hpp"
#include <bitset>
#include <string>
#include <vector>
namespace emthrm {
template <int N>
struct BinaryMatrix {
explicit BinaryMatrix(const int m, const int n = N, const bool def = false)
: n(n), data(m, std::bitset<N>(std::string(n, def ? '1' : '0'))) {}
int nrow() const { return data.size(); }
int ncol() const { return n; }
BinaryMatrix pow(long long exponent) const {
BinaryMatrix res(n, n), tmp = *this;
for (int i = 0; i < n; ++i) {
res[i].set(i);
}
for (; exponent > 0; exponent >>= 1) {
if (exponent & 1) res *= tmp;
tmp *= tmp;
}
return res;
}
inline const std::bitset<N>& operator[](const int i) const { return data[i]; }
inline std::bitset<N>& operator[](const int i) { return data[i]; }
BinaryMatrix& operator=(const BinaryMatrix& x) = default;
BinaryMatrix& operator+=(const BinaryMatrix& x) {
const int m = nrow();
for (int i = 0; i < m; ++i) {
data[i] ^= x[i];
}
return *this;
}
BinaryMatrix& operator*=(const BinaryMatrix& x) {
const int m = nrow(), l = x.ncol();
BinaryMatrix t_x(l, n), res(m, l);
for (int i = 0; i < l; ++i) {
for (int j = 0; j < n; ++j) {
t_x[i][j] = x[j][i];
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < l; ++j) {
if ((data[i] & t_x[j]).count() & 1) res[i].set(j);
}
}
return *this = res;
}
BinaryMatrix operator+(const BinaryMatrix& x) const {
return BinaryMatrix(*this) += x;
}
BinaryMatrix operator*(const BinaryMatrix& x) const {
return BinaryMatrix(*this) *= x;
}
private:
int n;
std::vector<std::bitset<N>> data;
};
} // namespace emthrm
#line 7 "include/emthrm/math/matrix/binary_matrix/gauss_jordan.hpp"
namespace emthrm {
template <bool IS_EXTENDED = false, int N>
int gauss_jordan(BinaryMatrix<N>* a) {
const int m = a->nrow(), n = a->ncol();
int rank = 0;
for (int col = 0; col < (IS_EXTENDED ? n - 1 : n); ++col) {
int pivot = -1;
for (int row = rank; row < m; ++row) {
if ((*a)[row][col]) {
pivot = row;
break;
}
}
if (pivot == -1) continue;
std::swap((*a)[rank], (*a)[pivot]);
for (int row = 0; row < m; ++row) {
if (row != rank && (*a)[row][col]) (*a)[row] ^= (*a)[rank];
}
++rank;
}
return rank;
}
} // namespace emthrm