結果

問題 No.2505 matriX cOnstRuction
ユーザー 👑 emthrmemthrm
提出日時 2023-07-28 05:31:11
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 343 ms / 2,500 ms
コード長 2,927 bytes
コンパイル時間 1,763 ms
コンパイル使用メモリ 105,896 KB
実行使用メモリ 138,924 KB
最終ジャッジ日時 2023-10-13 18:18:26
合計ジャッジ時間 8,905 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 49 ms
4,352 KB
testcase_02 AC 44 ms
4,348 KB
testcase_03 AC 44 ms
4,352 KB
testcase_04 AC 44 ms
4,356 KB
testcase_05 AC 44 ms
4,352 KB
testcase_06 AC 45 ms
4,368 KB
testcase_07 AC 31 ms
4,352 KB
testcase_08 AC 32 ms
4,352 KB
testcase_09 AC 31 ms
4,352 KB
testcase_10 AC 31 ms
4,352 KB
testcase_11 AC 32 ms
4,356 KB
testcase_12 AC 31 ms
4,348 KB
testcase_13 AC 31 ms
4,348 KB
testcase_14 AC 31 ms
4,352 KB
testcase_15 AC 31 ms
4,368 KB
testcase_16 AC 30 ms
4,352 KB
testcase_17 AC 31 ms
4,348 KB
testcase_18 AC 31 ms
4,352 KB
testcase_19 AC 31 ms
4,352 KB
testcase_20 AC 30 ms
4,348 KB
testcase_21 AC 31 ms
4,352 KB
testcase_22 AC 31 ms
4,356 KB
testcase_23 AC 31 ms
4,356 KB
testcase_24 AC 31 ms
4,356 KB
testcase_25 AC 31 ms
4,348 KB
testcase_26 AC 31 ms
4,500 KB
testcase_27 AC 31 ms
4,352 KB
testcase_28 AC 31 ms
4,352 KB
testcase_29 AC 31 ms
4,352 KB
testcase_30 AC 17 ms
4,352 KB
testcase_31 AC 17 ms
4,352 KB
testcase_32 AC 16 ms
4,348 KB
testcase_33 AC 17 ms
4,352 KB
testcase_34 AC 26 ms
6,472 KB
testcase_35 AC 19 ms
4,640 KB
testcase_36 AC 26 ms
6,012 KB
testcase_37 AC 23 ms
6,608 KB
testcase_38 AC 32 ms
11,532 KB
testcase_39 AC 56 ms
36,224 KB
testcase_40 AC 31 ms
12,532 KB
testcase_41 AC 235 ms
136,000 KB
testcase_42 AC 34 ms
11,644 KB
testcase_43 AC 234 ms
138,924 KB
testcase_44 AC 225 ms
135,128 KB
testcase_45 AC 232 ms
138,564 KB
testcase_46 AC 237 ms
136,260 KB
testcase_47 AC 236 ms
137,632 KB
testcase_48 AC 111 ms
71,252 KB
testcase_49 AC 236 ms
136,100 KB
testcase_50 AC 240 ms
135,636 KB
testcase_51 AC 343 ms
4,348 KB
testcase_52 AC 168 ms
87,100 KB
testcase_53 AC 160 ms
84,448 KB
testcase_54 AC 171 ms
87,000 KB
testcase_55 AC 31 ms
4,668 KB
testcase_56 AC 31 ms
4,352 KB
testcase_57 AC 31 ms
4,352 KB
testcase_58 AC 34 ms
6,644 KB
testcase_59 AC 35 ms
9,448 KB
testcase_60 AC 36 ms
11,164 KB
testcase_61 AC 28 ms
4,668 KB
testcase_62 AC 27 ms
4,584 KB
testcase_63 AC 19 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <bit>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <utility>
#include <vector>

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

  // binary trie
  static constexpr int kNoEdge = -1;
  static constexpr std::array<std::pair<int, std::int64_t>, 2> kLeaf{
      std::make_pair(kNoEdge, 0), std::make_pair(kNoEdge, 0)};
  const int inf = (n + m) * 2 + 1;
  std::vector trie{kLeaf};
  const auto MakeNode = [&trie]() -> int {
    const int index = trie.size();
    trie.emplace_back(kLeaf);
    return index;
  };
  const auto Add = [&trie, MakeNode](const int y_i, const int z_i, const int w)
      -> void {
    int index = 0;
    for (int bit = D - 1; bit >= 0; --bit) {
      const int edge_label = (y_i ^ z_i) >> bit & 1;
      if ((z_i >> bit & 1) == 0) {
        if (trie[index][edge_label ^ 1].first == kNoEdge) {
          trie[index][edge_label ^ 1].first = MakeNode();
        }
        trie[index][edge_label ^ 1].second += w;
      }
      if (trie[index][edge_label].first == kNoEdge) {
        trie[index][edge_label].first = MakeNode();
      }
      index = trie[index][edge_label].first;
    }
  };

  for (int i = 0; i < n + m; ++i) {
    if (z[i] == 0) {
      Add(y[i], z[i], inf);
    } else {
      Add(y[i], 0, 1);
      Add(y[i], z[i], 1);
      Add(y[i], std::bit_ceil(static_cast<unsigned int>(z[i] + 1)) - 1, inf);
    }
  }

  std::int64_t ans = inf;
  const int node_num = trie.size();
  std::vector<std::int64_t> f(node_num, 0);  // 根からその頂点までの重み
  for (int i = 0; i < node_num; ++i) {
    for (const auto& [j, weight] : trie[i]) {
      if (j == kNoEdge) {
        ans = std::min(ans, f[i]);
      } else {
        f[j] = f[i] + weight;
      }
    }
  }
  return ans < inf ? ans : -1;
}

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