結果

問題 No.2505 matriX cOnstRuction
ユーザー 👑 emthrmemthrm
提出日時 2023-07-28 15:38:08
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,932 bytes
コンパイル時間 1,193 ms
コンパイル使用メモリ 113,880 KB
実行使用メモリ 139,596 KB
最終ジャッジ日時 2024-04-19 21:06:31
合計ジャッジ時間 6,849 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 15 ms
6,940 KB
testcase_31 AC 16 ms
6,940 KB
testcase_32 AC 15 ms
6,940 KB
testcase_33 AC 17 ms
6,940 KB
testcase_34 AC 24 ms
6,944 KB
testcase_35 AC 18 ms
6,944 KB
testcase_36 AC 26 ms
6,944 KB
testcase_37 AC 21 ms
6,944 KB
testcase_38 AC 30 ms
12,592 KB
testcase_39 AC 51 ms
37,816 KB
testcase_40 AC 30 ms
12,604 KB
testcase_41 AC 198 ms
135,220 KB
testcase_42 AC 32 ms
12,332 KB
testcase_43 AC 216 ms
138,204 KB
testcase_44 AC 194 ms
136,836 KB
testcase_45 AC 200 ms
138,460 KB
testcase_46 AC 193 ms
136,508 KB
testcase_47 AC 202 ms
139,596 KB
testcase_48 AC 94 ms
70,172 KB
testcase_49 AC 192 ms
136,604 KB
testcase_50 AC 198 ms
136,360 KB
testcase_51 AC 306 ms
6,940 KB
testcase_52 AC 140 ms
87,280 KB
testcase_53 AC 133 ms
84,540 KB
testcase_54 AC 139 ms
87,108 KB
testcase_55 AC 30 ms
6,940 KB
testcase_56 AC 29 ms
6,944 KB
testcase_57 AC 29 ms
6,940 KB
testcase_58 AC 33 ms
6,948 KB
testcase_59 AC 33 ms
9,804 KB
testcase_60 AC 31 ms
11,660 KB
testcase_61 WA -
testcase_62 WA -
testcase_63 AC 18 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// <WA> #895662 ベース
// 2R' の計算を間違っている。
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;
    }
  }

  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, 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