結果
問題 | No.2505 matriX cOnstRuction |
ユーザー | suisen |
提出日時 | 2023-07-27 16:46:57 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,591 ms / 2,500 ms |
コード長 | 2,513 bytes |
コンパイル時間 | 399 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 380,552 KB |
最終ジャッジ日時 | 2024-09-15 18:49:47 |
合計ジャッジ時間 | 30,280 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge6 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 64 ms
68,992 KB |
testcase_01 | AC | 368 ms
81,980 KB |
testcase_02 | AC | 257 ms
80,588 KB |
testcase_03 | AC | 253 ms
80,680 KB |
testcase_04 | AC | 258 ms
80,896 KB |
testcase_05 | AC | 263 ms
80,584 KB |
testcase_06 | AC | 359 ms
81,804 KB |
testcase_07 | AC | 209 ms
79,488 KB |
testcase_08 | AC | 210 ms
80,128 KB |
testcase_09 | AC | 203 ms
79,488 KB |
testcase_10 | AC | 208 ms
80,824 KB |
testcase_11 | AC | 214 ms
79,488 KB |
testcase_12 | AC | 209 ms
79,744 KB |
testcase_13 | AC | 212 ms
79,360 KB |
testcase_14 | AC | 204 ms
79,616 KB |
testcase_15 | AC | 201 ms
80,364 KB |
testcase_16 | AC | 207 ms
79,616 KB |
testcase_17 | AC | 218 ms
79,616 KB |
testcase_18 | AC | 208 ms
79,744 KB |
testcase_19 | AC | 210 ms
79,488 KB |
testcase_20 | AC | 202 ms
79,232 KB |
testcase_21 | AC | 210 ms
79,616 KB |
testcase_22 | AC | 205 ms
80,044 KB |
testcase_23 | AC | 208 ms
79,700 KB |
testcase_24 | AC | 202 ms
80,384 KB |
testcase_25 | AC | 202 ms
79,876 KB |
testcase_26 | AC | 206 ms
79,872 KB |
testcase_27 | AC | 207 ms
80,280 KB |
testcase_28 | AC | 204 ms
80,000 KB |
testcase_29 | AC | 204 ms
80,256 KB |
testcase_30 | AC | 174 ms
80,032 KB |
testcase_31 | AC | 166 ms
80,016 KB |
testcase_32 | AC | 165 ms
79,600 KB |
testcase_33 | AC | 182 ms
80,152 KB |
testcase_34 | AC | 214 ms
87,552 KB |
testcase_35 | AC | 163 ms
81,624 KB |
testcase_36 | AC | 202 ms
86,320 KB |
testcase_37 | AC | 198 ms
85,440 KB |
testcase_38 | AC | 185 ms
96,504 KB |
testcase_39 | AC | 317 ms
137,824 KB |
testcase_40 | AC | 175 ms
95,284 KB |
testcase_41 | AC | 1,573 ms
364,704 KB |
testcase_42 | AC | 183 ms
97,172 KB |
testcase_43 | AC | 1,591 ms
372,504 KB |
testcase_44 | AC | 1,577 ms
364,092 KB |
testcase_45 | AC | 1,560 ms
371,564 KB |
testcase_46 | AC | 1,566 ms
364,372 KB |
testcase_47 | AC | 1,584 ms
372,320 KB |
testcase_48 | AC | 605 ms
207,128 KB |
testcase_49 | AC | 1,510 ms
380,552 KB |
testcase_50 | AC | 1,527 ms
376,812 KB |
testcase_51 | AC | 824 ms
87,552 KB |
testcase_52 | AC | 984 ms
265,340 KB |
testcase_53 | AC | 949 ms
261,172 KB |
testcase_54 | AC | 976 ms
257,696 KB |
testcase_55 | AC | 184 ms
80,452 KB |
testcase_56 | AC | 182 ms
81,636 KB |
testcase_57 | AC | 185 ms
81,276 KB |
testcase_58 | AC | 262 ms
92,012 KB |
testcase_59 | AC | 241 ms
89,856 KB |
testcase_60 | AC | 250 ms
94,356 KB |
testcase_61 | AC | 183 ms
80,624 KB |
testcase_62 | AC | 181 ms
81,152 KB |
testcase_63 | AC | 162 ms
79,488 KB |
ソースコード
from itertools import product import sys from time import perf_counter from typing import List, Tuple def floor_pow2(n: int): x = 1 while (x << 1) <= n: x <<= 1 return x class Node: def __init__(self) -> None: self.lch = None self.rch = None self.weight = 0 def left_child_or_create(self) -> 'Node': if self.lch is None: self.lch = Node() return self.lch def right_child_or_create(self) -> 'Node': if self.rch is None: self.rch = Node() return self.rch L = 30 inf = 1 << 30 def solve(n: int, m: int, R: List[int], C: List[int], A: List[List[int]]): for i, j in product(range(n), range(m)): if A[0][0] ^ A[0][j] ^ A[i][0] ^ A[i][j]: print(-1) return root = Node() # f(X) += W * [X ^ Y > Z] def add_weight(Y: int, Z: int, W: int): YZ = Y ^ Z cur = root for bit in reversed(range(L)): if (YZ >> bit) & 1: nxt = cur.right_child_or_create() else: nxt = cur.left_child_or_create() if not ((Z >> bit) & 1): cur.weight += W nxt.weight -= W cur = nxt for i in range(n): Y = A[0][0] ^ A[i][0] if R[i]: add_weight(Y, 0, 1) add_weight(Y, R[i], 1) add_weight(Y, 2 * floor_pow2(R[i]) - 1, inf) else: add_weight(Y, 0, inf) for j in range(m): Y = A[0][j] if C[j]: add_weight(Y, 0, 1) add_weight(Y, C[j], 1) add_weight(Y, 2 * floor_pow2(C[j]) - 1, inf) else: add_weight(Y, 0, inf) min_weight = inf q : List[Node] = [root] for node in q: weight = node.weight if node.lch is None: min_weight = min(min_weight, weight) else: node.lch.weight += weight q.append(node.lch) if node.rch is None: min_weight = min(min_weight, weight) else: node.rch.weight += weight q.append(node.rch) if min_weight >= inf: print(-1) return print(min_weight) input = sys.stdin.readline T = int(input()) for _ in range(T): n, m = map(int, input().split()) R = list(map(int, input().split())) C = list(map(int, input().split())) A = [list(map(int, input().split())) for _ in range(n)] solve(n, m, R, C, A)