結果
問題 | No.2505 matriX cOnstRuction |
ユーザー |
|
提出日時 | 2023-07-27 23:56:25 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,865 bytes |
コンパイル時間 | 709 ms |
コンパイル使用メモリ | 81,004 KB |
最終ジャッジ日時 | 2025-02-15 19:41:26 |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 35 TLE * 4 -- * 25 |
ソースコード
// 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;}