結果

問題 No.2505 matriX cOnstRuction
ユーザー SSRSSSRS
提出日時 2023-10-13 23:30:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,000 bytes
コンパイル時間 2,677 ms
コンパイル使用メモリ 215,496 KB
実行使用メモリ 140,284 KB
最終ジャッジ日時 2023-10-13 23:30:31
合計ジャッジ時間 18,658 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 42 ms
4,352 KB
testcase_08 AC 42 ms
4,348 KB
testcase_09 AC 45 ms
4,352 KB
testcase_10 AC 41 ms
4,352 KB
testcase_11 WA -
testcase_12 AC 44 ms
4,352 KB
testcase_13 AC 41 ms
4,352 KB
testcase_14 AC 42 ms
4,348 KB
testcase_15 AC 42 ms
4,352 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 42 ms
4,348 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 41 ms
4,352 KB
testcase_22 AC 41 ms
4,356 KB
testcase_23 AC 43 ms
4,348 KB
testcase_24 AC 43 ms
4,352 KB
testcase_25 AC 42 ms
4,348 KB
testcase_26 AC 43 ms
4,348 KB
testcase_27 AC 43 ms
4,352 KB
testcase_28 AC 42 ms
4,348 KB
testcase_29 AC 42 ms
4,348 KB
testcase_30 AC 26 ms
4,348 KB
testcase_31 AC 25 ms
4,352 KB
testcase_32 AC 24 ms
4,348 KB
testcase_33 AC 27 ms
4,352 KB
testcase_34 AC 40 ms
4,520 KB
testcase_35 AC 21 ms
4,352 KB
testcase_36 AC 41 ms
4,868 KB
testcase_37 AC 37 ms
4,836 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 1,057 ms
140,284 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 722 ms
4,352 KB
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 AC 28 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
const long long INF2 = 1000000000000;
int main(){
  int T;
  cin >> T;
  for (int i = 0; i < T; i++){
    int n, m;
    cin >> n >> m;
    vector<int> R(n);
    for (int j = 0; j < n; j++){
      cin >> R[j];
    }
    vector<int> C(m);
    for (int j = 0; j < m; j++){
      cin >> C[j];
    }
    vector<vector<int>> a(n, vector<int>(m));
    for (int j = 0; j < n; j++){
      for (int k = 0; k < m; k++){
        cin >> a[j][k];
      }
    }
    bool ok = true;
    for (int j = 0; j < n - 1; j++){
      for (int k = 0; k < m - 1; k++){
        if ((a[j][k] ^ a[j][k + 1] ^ a[j + 1][k] ^ a[j + 1][k + 1]) != 0){
          ok = false;
        }
      }
    }
    if (!ok){
      cout << -1 << endl;
    } else {
      vector<int> A(n + m);
      for (int j = 0; j < n; j++){
        A[j] = R[j];
      }
      for (int j = 0; j < m; j++){
        A[n + j] = C[j];
      }
      vector<int> B(n + m);
      B[0] = 0;
      for (int j = 1; j < n; j++){
        B[j] = B[0] ^ a[0][0] ^ a[j][0];
      }
      for (int j = 0; j < m; j++){
        B[n + j] = B[0] ^ a[0][j];
      }
      vector<int> A2(n + m);
      for (int j = 0; j < n + m; j++){
        if (A[j] == 0){
          A2[j] = 0;
        } else {
          int p = 32 - __builtin_clz(A[j]);
          A2[j] = (1 << p) - 1;
        }
      }
      vector<pair<int, int>> query;
      auto add = [&](int b, int mn, int add){
        int curr = 0;
        for (int j = 29; j >= 0; j--){
          if ((mn >> j & 1) == 0){
            if ((b >> j & 1) == 0){
              query.push_back(make_pair(curr + (1 << j), add));
              query.push_back(make_pair(curr + (1 << j) * 2, -add));
            } else {
              query.push_back(make_pair(curr, add));
              query.push_back(make_pair(curr + (1 << j), -add));
              curr |= 1 << j;
            }
          } else {
            if ((b >> j & 1) == 0){
              curr |= 1 << j;
            }
          }
        }
      };
      for (int j = 0; j < n + m; j++){
        add(B[j], 0, 1);
        add(B[j], A[j], 1);
        add(B[j], A2[j], INF);
      }
      query.push_back(make_pair(0, 0));
      query.push_back(make_pair(1 << 30, 0));
      int cnt = query.size();
      vector<int> X;
      for (int j = 0; j < cnt; j++){
        X.push_back(query[j].first);
      }
      sort(X.begin(), X.end());
      X.erase(unique(X.begin(), X.end()), X.end());
      int cnt2 = X.size();
      vector<long long> imos(cnt2, 0);
      for (int j = 0; j < cnt; j++){
        int p = lower_bound(X.begin(), X.end(), query[j].first) - X.begin();
        imos[p] += query[j].second;
      }
      for (int j = 1; j < cnt2; j++){
        imos[j] += imos[j - 1];
      }
      long long ans = INF2;
      for (int j = 0; j < cnt2 - 1; j++){
        ans = min(ans, imos[j]);
      }
      if (ans > n + m){
        cout << -1 << endl;
      } else {
        cout << ans << endl;
      }
    }
  }
}
0