結果

問題 No.2505 matriX cOnstRuction
ユーザー suisensuisen
提出日時 2023-02-13 17:14:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
J_ERR  
(最初)
実行時間 -
コード長 2,669 bytes
コンパイル時間 1,621 ms
コンパイル使用メモリ 85,788 KB
実行使用メモリ 11,376 KB
最終ジャッジ日時 2023-10-13 22:54:15
合計ジャッジ時間 15,799 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 TLE -
testcase_35 WA -
testcase_36 TLE -
testcase_37 TLE -
testcase_38 TLE -
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 #

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

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

void println() {
    std::cout << '\n';
}
template <typename Head, typename ...Tails>
void println(Head&& head, Tails &&...tails) {
    std::cout << std::forward<Head>(head);
    if constexpr (sizeof...(Tails)) {
        std::cout << ' ';
    }
    println(std::forward<Tails>(tails)...);
}

using Weight = long long;

constexpr int L = 30;

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

void solve(int n, int m, const std::vector<int>& r, const std::vector<int>& c, const std::vector<std::vector<int>>& 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]) {
            println(-1);
            return;
        }
    }

    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_ctz(floor_pow2(a[i][j])) + 1);
    }

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

    for (int x = 0; x < 1 << bit_num; ++x) {
        Weight cost = 0;
        for (int i = 0; i < n; ++i) {
            int s = x ^ a[i][0] ^ a[0][0];
            if (s == 0) {
                continue;
            } else {
                cost += 1;
            }
        }
        for (int j = 0; j < m; ++j) {
            int t = x ^ a[0][j];
            if (t == 0) {
                continue;
            } else {
                cost += 1;
            }
        }
        min_cost = std::min(min_cost, std::pair{ cost, x });
    }

    auto [cost, X] = min_cost;

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

    println(cost);
    std::vector<std::tuple<char, int, int>> ops;
    for (int i = 0; i < n; ++i) {
        int s = X ^ a[0][0] ^ a[i][0];
        if (s == 0) {
            continue;
        } else {
            println('r', i + 1, s);
        }
    }
    for (int j = 0; j < m; ++j) {
        int t = X ^ a[0][j];
        if (t == 0) {
            continue;
        } else {
            println('c', j + 1, t);
        }
    }
}

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<int> r(n), c(m);
        for (auto& e : r) std::cin >> e;
        for (auto& e : c) std::cin >> e;

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

        solve(n, m, r, c, a);
    }

    return 0;
}
0