結果

問題 No.2505 matriX cOnstRuction
ユーザー 👑 emthrmemthrm
提出日時 2023-07-26 21:41:48
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 495 ms / 2,500 ms
コード長 3,178 bytes
コンパイル時間 1,061 ms
コンパイル使用メモリ 111,592 KB
実行使用メモリ 12,368 KB
最終ジャッジ日時 2023-10-13 18:17:39
合計ジャッジ時間 6,013 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 57 ms
4,356 KB
testcase_02 AC 48 ms
4,352 KB
testcase_03 AC 46 ms
4,352 KB
testcase_04 AC 45 ms
4,352 KB
testcase_05 AC 47 ms
4,352 KB
testcase_06 AC 54 ms
4,372 KB
testcase_07 AC 32 ms
4,352 KB
testcase_08 AC 31 ms
4,352 KB
testcase_09 AC 31 ms
4,352 KB
testcase_10 AC 30 ms
4,348 KB
testcase_11 AC 31 ms
4,348 KB
testcase_12 AC 30 ms
4,352 KB
testcase_13 AC 30 ms
4,376 KB
testcase_14 AC 31 ms
4,348 KB
testcase_15 AC 29 ms
4,348 KB
testcase_16 AC 31 ms
4,352 KB
testcase_17 AC 30 ms
4,352 KB
testcase_18 AC 30 ms
4,352 KB
testcase_19 AC 30 ms
4,352 KB
testcase_20 AC 29 ms
4,348 KB
testcase_21 AC 30 ms
4,352 KB
testcase_22 AC 31 ms
4,352 KB
testcase_23 AC 29 ms
4,352 KB
testcase_24 AC 29 ms
4,352 KB
testcase_25 AC 31 ms
4,348 KB
testcase_26 AC 32 ms
4,348 KB
testcase_27 AC 31 ms
4,348 KB
testcase_28 AC 30 ms
4,372 KB
testcase_29 AC 31 ms
4,352 KB
testcase_30 AC 18 ms
4,348 KB
testcase_31 AC 16 ms
4,356 KB
testcase_32 AC 16 ms
4,352 KB
testcase_33 AC 17 ms
4,348 KB
testcase_34 AC 23 ms
4,352 KB
testcase_35 AC 17 ms
4,352 KB
testcase_36 AC 24 ms
4,352 KB
testcase_37 AC 21 ms
4,352 KB
testcase_38 AC 19 ms
4,352 KB
testcase_39 AC 22 ms
4,348 KB
testcase_40 AC 18 ms
4,352 KB
testcase_41 AC 37 ms
4,668 KB
testcase_42 AC 19 ms
4,352 KB
testcase_43 AC 42 ms
7,156 KB
testcase_44 AC 38 ms
4,524 KB
testcase_45 AC 42 ms
7,128 KB
testcase_46 AC 39 ms
4,560 KB
testcase_47 AC 42 ms
7,436 KB
testcase_48 AC 24 ms
4,352 KB
testcase_49 AC 208 ms
4,352 KB
testcase_50 AC 195 ms
4,408 KB
testcase_51 AC 495 ms
4,348 KB
testcase_52 AC 65 ms
12,248 KB
testcase_53 AC 63 ms
9,768 KB
testcase_54 AC 66 ms
12,368 KB
testcase_55 AC 20 ms
4,356 KB
testcase_56 AC 19 ms
4,348 KB
testcase_57 AC 19 ms
4,348 KB
testcase_58 AC 19 ms
4,352 KB
testcase_59 AC 20 ms
4,348 KB
testcase_60 AC 20 ms
4,348 KB
testcase_61 AC 19 ms
4,348 KB
testcase_62 AC 19 ms
4,352 KB
testcase_63 AC 13 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <iterator>
#include <numeric>
#include <tuple>
#include <vector>

int KthBit(const int x, const int k) { return x >> k & 1; }

template <int D>
int Solve(const std::vector<int>& z, const std::vector<std::vector<int>>& a) {
  const int n = a.size(), m = a.front().size();
  assert(std::ssize(z) == n + m);

  // A から S, T を決める
  std::vector<int> y(n + m, 0);
  for (int i = 1; i < n; ++i) {
    y[i] = a[i].front() ^ a.front().front();
  }
  for (int j = 0; j < m; ++j) {
    y[j + n] = a.front()[j];
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if ((y[i] ^ y[j + n]) != a[i][j]) return -1;
    }
  }

  int min_fx = (n + m) * 2 + 1;

  // 上位から x を決める
  const auto dfs = [&z, &y, &min_fx](
      auto dfs, const int bit, const int fx, const std::vector<int>& or_012,
      const std::vector<int>& or_01, const std::vector<int>& or_12) -> void {
    if ((or_012.empty() && or_01.empty() && or_12.empty()) || bit < 0) {
      min_fx = std::min(min_fx, fx);
      return;
    }
    for (const int x_bit : std::array<int, 2>{0, 1}) {
      bool is_valid = true;
      int next_fx = fx;
      std::vector<int> next_or_012, next_or_01, next_or_12;
      for (const int i : or_012) {
        if (KthBit(y[i], bit) != x_bit) {  // (x ⊕ Y[i])[bit] = 1
          if (KthBit(z[i], bit) == 0) {
            is_valid = false;
            break;
          }
          ++next_fx;
          next_or_12.emplace_back(i);
        } else {  // (x ⊕ Y[i])[bit] = 0
          (KthBit(z[i], bit) == 1 ? next_or_01 : next_or_012).emplace_back(i);
        }
      }
      if (!is_valid) continue;
      for (const int i : or_01) {
        if (KthBit(y[i], bit) != x_bit) {  // (x ⊕ Y[i])[bit] = 1
          ++next_fx;
        } else {  // (x ⊕ Y[i])[bit] = 0
          next_or_01.emplace_back(i);
        }
      }
      for (const int i : or_12) {
        const int x_xor_y = (KthBit(y[i], bit) != x_bit ? 1 : 0);
        const int z_bit = KthBit(z[i], bit);
        if (x_xor_y > z_bit) {
          ++next_fx;
        } else if (x_xor_y == z_bit) {
          next_or_12.emplace_back(i);
        }
      }
      dfs(dfs, bit - 1, next_fx, next_or_012, next_or_01, next_or_12);
    }
  };
  std::vector<int> or_012(n + m);
  std::iota(or_012.begin(), or_012.end(), 0);
  dfs(dfs, D - 1, 0, or_012, std::vector<int>{}, std::vector<int>{});

  return min_fx > (n + m) * 2 ? -1 : min_fx;
}

int main() {
  constexpr int kMaxT = 50000, kMaxNM = 50000, kDigits = 30;

  int t;
  std::cin >> t;
  assert(1 <= t && t <= kMaxT);

  while (t--) {
    int n, m;
    std::cin >> n >> m;
    assert(1 <= n && 1 <= m && n * m <= kMaxNM);
    std::vector<int> z(n + m);
    for (int i = 0; i < n + m; ++i) {
      std::cin >> z[i];
      assert(0 <= z[i] && z[i] < (1 << kDigits));
    }
    std::vector a(n, std::vector(m, 0));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        std::cin >> a[i][j];
        assert(0 <= a[i][j] && a[i][j] < (1 << kDigits));
      }
    }
    std::cout << Solve<kDigits>(z, a) << '\n';
  }
  return 0;
}
0