結果

問題 No.2505 matriX cOnstRuction
ユーザー 👑 emthrmemthrm
提出日時 2023-07-28 05:52:42
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,062 bytes
コンパイル時間 1,243 ms
コンパイル使用メモリ 112,204 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2024-04-19 20:58:57
合計ジャッジ時間 6,187 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 16 ms
5,376 KB
testcase_31 AC 15 ms
5,376 KB
testcase_32 AC 17 ms
5,376 KB
testcase_33 AC 17 ms
5,376 KB
testcase_34 AC 22 ms
5,376 KB
testcase_35 AC 18 ms
5,376 KB
testcase_36 AC 21 ms
5,376 KB
testcase_37 AC 20 ms
5,376 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 AC 28 ms
5,376 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
権限があれば一括ダウンロードができます

ソースコード

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; }

// <WA> #895425 ベース
// 実装ミス
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);

  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;

  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) {
          if (KthBit(z[i], bit) == 0) {
            is_valid = false;
            break;
          }
          ++next_fx;
          next_or_12.emplace_back(i);
        } else {
          (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) {
          ++next_fx;
        } else {
          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