結果
問題 | No.1421 国勢調査 (Hard) |
ユーザー | hitonanode |
提出日時 | 2022-11-06 22:09:05 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 22 ms / 2,000 ms |
コード長 | 4,885 bytes |
コンパイル時間 | 1,320 ms |
コンパイル使用メモリ | 112,516 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-20 09:18:20 |
合計ジャッジ時間 | 3,485 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 3 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 1 ms
5,376 KB |
testcase_11 | AC | 2 ms
5,376 KB |
testcase_12 | AC | 2 ms
5,376 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 1 ms
5,376 KB |
testcase_15 | AC | 2 ms
5,376 KB |
testcase_16 | AC | 2 ms
5,376 KB |
testcase_17 | AC | 2 ms
5,376 KB |
testcase_18 | AC | 2 ms
5,376 KB |
testcase_19 | AC | 1 ms
5,376 KB |
testcase_20 | AC | 2 ms
5,376 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 20 ms
5,376 KB |
testcase_23 | AC | 20 ms
5,376 KB |
testcase_24 | AC | 21 ms
5,376 KB |
testcase_25 | AC | 22 ms
5,376 KB |
testcase_26 | AC | 22 ms
5,376 KB |
testcase_27 | AC | 20 ms
5,376 KB |
testcase_28 | AC | 16 ms
5,376 KB |
testcase_29 | AC | 14 ms
5,376 KB |
testcase_30 | AC | 7 ms
5,376 KB |
testcase_31 | AC | 5 ms
5,376 KB |
ソースコード
#line 1 "linear_algebra_matrix/test/linalg_bitset.yuki1421.test.cpp" #define PROBLEM "https://yukicoder.me/problems/no/1421" #line 2 "linear_algebra_matrix/linalg_bitset.hpp" #include <bitset> #include <cassert> #include <tuple> #include <utility> #include <vector> // Gauss-Jordan elimination of n * m matrix M // Complexity: O(nm + nm rank(M) / 64) // Verified: abc276_h (2000 x 8000) template <int Wmax> std::vector<std::bitset<Wmax>> gauss_jordan(int W, std::vector<std::bitset<Wmax>> M) { assert(W <= Wmax); int H = M.size(), c = 0; for (int h = 0; h < H and c < W; ++h, ++c) { int piv = -1; for (int j = h; j < H; ++j) { if (M[j][c]) { piv = j; break; } } if (piv == -1) { --h; continue; } std::swap(M[piv], M[h]); for (int hh = 0; hh < H; ++hh) { if (hh != h and M[hh][c]) M[hh] ^= M[h]; } } return M; } // Rank of Gauss-Jordan eliminated matrix template <int Wmax> int rank_gauss_jordan(int W, const std::vector<std::bitset<Wmax>> &M) { assert(W <= Wmax); for (int h = (int)M.size() - 1; h >= 0; h--) { int j = 0; while (j < W and !M[h][j]) ++j; if (j < W) return h + 1; } return 0; } template <int W1, int W2> std::vector<std::bitset<W2>> matmul(const std::vector<std::bitset<W1>> &A, const std::vector<std::bitset<W2>> &B) { int H = A.size(), K = B.size(); std::vector<std::bitset<W2>> C(H); for (int i = 0; i < H; i++) { for (int j = 0; j < K; j++) { if (A[i][j]) C[i] ^= B[j]; } } return C; } template <int Wmax> std::vector<std::bitset<Wmax>> matpower(std::vector<std::bitset<Wmax>> X, long long n) { int D = X.size(); std::vector<std::bitset<Wmax>> ret(D); for (int i = 0; i < D; i++) ret[i][i] = 1; while (n) { if (n & 1) ret = matmul<Wmax, Wmax>(ret, X); X = matmul<Wmax, Wmax>(X, X), n >>= 1; } return ret; } // Solve Ax = b on F_2 // - retval: {true, one of the solutions, {freedoms}} (if solution exists) // {false, {}, {}} (otherwise) template <int Wmax, class Vec> std::tuple<bool, std::bitset<Wmax>, std::vector<std::bitset<Wmax>>> system_of_linear_equations(std::vector<std::bitset<Wmax>> A, Vec b, int W) { int H = A.size(); assert(W <= Wmax); assert(A.size() == b.size()); std::vector<std::bitset<Wmax + 1>> M(H); for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) M[i][j] = A[i][j]; M[i][W] = b[i]; } M = gauss_jordan<Wmax + 1>(W + 1, M); std::vector<int> ss(W, -1); for (int i = 0; i < H; i++) { int j = 0; while (j <= W and !M[i][j]) ++j; if (j == W) return {false, 0, {}}; if (j < W) ss[j] = i; } std::bitset<Wmax> x; std::vector<std::bitset<Wmax>> D; for (int j = 0; j < W; ++j) { if (ss[j] == -1) { std::bitset<Wmax> d; d[j] = 1; for (int jj = 0; jj < W; jj++) if (ss[jj] != -1) d[jj] = M[ss[jj]][j]; D.emplace_back(d); } else { x[j] = M[ss[j]][W]; } } return std::make_tuple(true, x, D); } #line 3 "linear_algebra_matrix/test/linalg_bitset.yuki1421.test.cpp" #include <iostream> #line 6 "linear_algebra_matrix/test/linalg_bitset.yuki1421.test.cpp" using namespace std; template <typename T> bool chmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } void bad() { puts("-1"); exit(0); } int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int N, M; cin >> N >> M; using ull = unsigned long long; vector<pair<ull, int>> basis; while (M--) { int a; cin >> a; ull mask = 0; while (a--) { int b; cin >> b; b--; mask += 1ULL << b; } int y; cin >> y; for (auto [v, w] : basis) { if (chmin(mask, mask ^ v)) y ^= w; } if (!mask and y) bad(); if (mask) basis.emplace_back(mask, y); } vector<int> ret(N); for (int d = 0; d < 30; ++d) { constexpr int Wmax = 320; vector<bitset<Wmax>> A; vector<bool> b; for (int i = 0; i < int(basis.size()); ++i) { b.push_back((basis[i].second >> d) & 1); bitset<Wmax> a; a.reset(); for (int j = 0; j < N; ++j) { if ((basis[i].first >> j) & 1) a[j] = 1; } A.emplace_back(a); } auto [ok, solution, freedoms] = system_of_linear_equations<Wmax>(A, b, N); if (!ok) bad(); for (int i = 0; i < N; ++i) ret[i] += int(solution[i]) << d; } for (auto x : ret) cout << x << '\n'; }