結果

問題 No.2505 matriX cOnstRuction
ユーザー suisensuisen
提出日時 2023-07-27 23:56:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,865 bytes
コンパイル時間 914 ms
コンパイル使用メモリ 83,532 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-04-16 05:23:18
合計ジャッジ時間 7,403 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// TLE: x 全探索

#include <array>
#include <cassert>
#include <iostream>
#include <limits>
#include <tuple>
#include <vector>

long long floor_pow2(long long n) {
    long long x = 1;
    while ((x << 1) <= n) x <<= 1;
    return x;
}

using Weight = long long;

constexpr int L = 30;

constexpr Weight inf = std::numeric_limits<int>::max();

long long solve(int n, int m, const std::vector<long long>& r, const std::vector<long long>& c, const std::vector<std::vector<long long>>& a) {
    for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) {
        if (a[0][0] ^ a[0][j] ^ a[i][0] ^ a[i][j]) {
            return -1;
        }
    }

    int bit_num = 0;
    for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) {
        bit_num = std::max(bit_num, __builtin_ctzll(floor_pow2(a[i][j])) + 1);
    }

    std::pair<Weight, long long> min_cost{ inf, 0 };

    for (long long x = 0; x < 1LL << bit_num; ++x) {
        Weight cost = 0;
        for (int i = 0; i < n; ++i) {
            long long s = x ^ a[i][0] ^ a[0][0];
            if (s == 0) {
                continue;
            } else if (s <= r[i]) {
                cost += 1;
            } else {
                bool ok = false;
                for (long long y = r[i]; y >= 0; --y) {
                    if ((s ^ y) <= r[i]) {
                        ok = true;
                        break;
                    }
                }
                if (ok) {
                    cost += 2;
                } else {
                    cost += inf;
                }
            }
        }
        for (int j = 0; j < m; ++j) {
            long long t = x ^ a[0][j];
            if (t == 0) {
                continue;
            } else if (t <= c[j]) {
                cost += 1;
            } else {
                bool ok = false;
                for (long long y = c[j]; y >= 0; --y) {
                    if ((t ^ y) <= c[j]) {
                        ok = true;
                        break;
                    }
                }
                if (ok) {
                    cost += 2;
                } else {
                    cost += inf;
                }
            }
        }
        min_cost = std::min(min_cost, std::pair{ cost, x });
    }

    auto [cost, X] = min_cost;

    if (cost >= inf) {
        return -1;
    }
    return cost;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;

    while (t-- > 0) {
        int n, m;
        std::cin >> n >> m;

        std::vector<long long> r(n), c(m);
        for (auto& e : r) std::cin >> e;
        for (auto& e : c) std::cin >> e;

        std::vector a(n, std::vector<long long>(m));
        for (auto& row : a) for (auto& e : row) {
            std::cin >> e;
        }

        std::cout << solve(n, m, r, c, a) << std::endl;
    }

    return 0;
}
0