diff --git a/linear_algebra_matrix/linalg_bitset.hpp b/linear_algebra_matrix/linalg_bitset.hpp index 043a8cc0..5292096e 100644 --- a/linear_algebra_matrix/linalg_bitset.hpp +++ b/linear_algebra_matrix/linalg_bitset.hpp @@ -9,7 +9,7 @@ // Complexity: O(nm + nm rank(M) / 64) // Verified: abc276_h (2000 x 8000) template -std::vector> gauss_jordan(int W, std::vector> M) { +std::vector> f2_gauss_jordan(int W, std::vector> M) { assert(W <= Wmax); int H = M.size(), c = 0; for (int h = 0; h < H and c < W; ++h, ++c) { @@ -33,7 +33,7 @@ std::vector> gauss_jordan(int W, std::vector } // Rank of Gauss-Jordan eliminated matrix -template int rank_gauss_jordan(int W, const std::vector> &M) { +template int f2_rank_gauss_jordan(int W, const std::vector> &M) { assert(W <= Wmax); for (int h = (int)M.size() - 1; h >= 0; h--) { int j = 0; @@ -72,25 +72,25 @@ template int f2_determinant(const std::vector> &M) template std::vector> -matmul(const std::vector> &A, const std::vector> &B) { +f2_matmul(const std::vector> &A, const std::vector> &B) { int H = A.size(), K = B.size(); std::vector> C(H); for (int i = 0; i < H; i++) { for (int j = 0; j < K; j++) { - if (A[i][j]) C[i] ^= B[j]; + if (A.at(i).test(j)) C.at(i) ^= B.at(j); } } return C; } template -std::vector> matpower(std::vector> X, long long n) { +std::vector> f2_matpower(std::vector> X, long long n) { int D = X.size(); std::vector> ret(D); for (int i = 0; i < D; i++) ret[i][i] = 1; while (n) { - if (n & 1) ret = matmul(ret, X); - X = matmul(X, X), n >>= 1; + if (n & 1) ret = f2_matmul(ret, X); + X = f2_matmul(X, X), n >>= 1; } return ret; } @@ -101,7 +101,7 @@ std::vector> matpower(std::vector> X, long l // Complexity: O(HW + HW rank(A) / 64 + W^2 len(freedoms)) template std::tuple, std::vector>> -system_of_linear_equations(std::vector> A, Vec b, int W) { +f2_system_of_linear_equations(std::vector> A, Vec b, int W) { int H = A.size(); assert(W <= Wmax); assert(A.size() == b.size()); @@ -111,7 +111,7 @@ system_of_linear_equations(std::vector> A, Vec b, int W) { for (int j = 0; j < W; ++j) M[i][j] = A[i][j]; M[i][W] = b[i]; } - M = gauss_jordan(W + 1, M); + M = f2_gauss_jordan(W + 1, M); std::vector ss(W, -1); std::vector ss_nonneg_js; for (int i = 0; i < H; i++) { diff --git a/linear_algebra_matrix/linalg_bitset.md b/linear_algebra_matrix/linalg_bitset.md index 6e63571d..68614a9f 100644 --- a/linear_algebra_matrix/linalg_bitset.md +++ b/linear_algebra_matrix/linalg_bitset.md @@ -15,7 +15,7 @@ vector> A; vector b; // Solve Ax = b (x: F_2^W) -auto [feasible, x0, freedoms] = system_of_linear_equations>(A, b, W); +auto [feasible, x0, freedoms] = f2_system_of_linear_equations>(A, b, W); // Calc determinant (or check whether A is regular) int det = f2_determinant(mat); diff --git a/linear_algebra_matrix/test/linalg_bitset.test.cpp b/linear_algebra_matrix/test/linalg_bitset.test.cpp index bff207bd..a7034940 100644 --- a/linear_algebra_matrix/test/linalg_bitset.test.cpp +++ b/linear_algebra_matrix/test/linalg_bitset.test.cpp @@ -26,9 +26,9 @@ int main() { } cin >> T; - A = matpower(A, T); + A = f2_matpower(A, T); for (int i = 0; i < N; i++) A[i][N] = v[i]; - A = gauss_jordan(N + 1, A); + A = f2_gauss_jordan(N + 1, A); for (int i = 0; i < N; i++) { if (A[i].count() == 1 and A[i][N]) { diff --git a/linear_algebra_matrix/test/linalg_bitset.yuki1421.test.cpp b/linear_algebra_matrix/test/linalg_bitset.yuki1421.test.cpp index dfbbda8d..b12d8039 100644 --- a/linear_algebra_matrix/test/linalg_bitset.yuki1421.test.cpp +++ b/linear_algebra_matrix/test/linalg_bitset.yuki1421.test.cpp @@ -59,7 +59,7 @@ int main() { } A.emplace_back(a); } - auto [ok, solution, freedoms] = system_of_linear_equations>(A, b, N); + auto [ok, solution, freedoms] = f2_system_of_linear_equations>(A, b, N); if (!ok) bad(); for (int i = 0; i < N; ++i) ret[i] += int(solution[i]) << d; } diff --git a/linear_algebra_matrix/test/linalg_bitset_mul.test.cpp b/linear_algebra_matrix/test/linalg_bitset_mul.test.cpp new file mode 100644 index 00000000..f4cddafa --- /dev/null +++ b/linear_algebra_matrix/test/linalg_bitset_mul.test.cpp @@ -0,0 +1,43 @@ +#define PROBELM "https://judge.yosupo.jp/problem/matrix_product_mod_2" +#include "../linalg_bitset.hpp" + +#include +#include +#include +#include +#include +using namespace std; + +int main() { + cin.tie(nullptr); + ios::sync_with_stdio(false); + + constexpr int dim = 1 << 12; + + using BS = bitset; + + int N, M, K; + cin >> N >> M >> K; + + vector A(N), B(M); + for (auto &v : A) { + string s; + cin >> s; + std::reverse(s.begin(), s.end()); // bitset に文字列 s を与えると s[0] が最高位になるので反転 + v = BS(s); + } + + for (auto &v : B) { + string s; + cin >> s; + std::reverse(s.begin(), s.end()); + v = BS(s); + } + + auto C = f2_matmul(A, B); + for (const auto &v : C) { + auto tmp = v.to_string(); + std::reverse(tmp.begin(), tmp.end()); + cout << tmp.substr(0, K) << '\n'; + } +}