結果

問題 No.2505 matriX cOnstRuction
ユーザー suisensuisen
提出日時 2023-07-27 16:46:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,648 ms / 2,500 ms
コード長 2,513 bytes
コンパイル時間 369 ms
コンパイル使用メモリ 87,068 KB
実行使用メモリ 395,044 KB
最終ジャッジ日時 2023-10-13 23:00:02
合計ジャッジ時間 34,837 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 155 ms
80,840 KB
testcase_01 AC 412 ms
85,588 KB
testcase_02 AC 336 ms
85,804 KB
testcase_03 AC 349 ms
85,156 KB
testcase_04 AC 358 ms
84,028 KB
testcase_05 AC 335 ms
85,880 KB
testcase_06 AC 435 ms
85,784 KB
testcase_07 AC 284 ms
84,160 KB
testcase_08 AC 280 ms
84,348 KB
testcase_09 AC 272 ms
84,876 KB
testcase_10 AC 278 ms
84,428 KB
testcase_11 AC 288 ms
85,300 KB
testcase_12 AC 278 ms
84,544 KB
testcase_13 AC 332 ms
84,484 KB
testcase_14 AC 290 ms
84,684 KB
testcase_15 AC 274 ms
84,028 KB
testcase_16 AC 280 ms
83,976 KB
testcase_17 AC 297 ms
84,532 KB
testcase_18 AC 311 ms
83,844 KB
testcase_19 AC 301 ms
84,948 KB
testcase_20 AC 286 ms
84,920 KB
testcase_21 AC 289 ms
84,136 KB
testcase_22 AC 278 ms
84,608 KB
testcase_23 AC 291 ms
83,776 KB
testcase_24 AC 278 ms
84,428 KB
testcase_25 AC 278 ms
84,392 KB
testcase_26 AC 276 ms
84,764 KB
testcase_27 AC 274 ms
84,464 KB
testcase_28 AC 273 ms
83,700 KB
testcase_29 AC 275 ms
84,172 KB
testcase_30 AC 249 ms
84,604 KB
testcase_31 AC 244 ms
84,088 KB
testcase_32 AC 236 ms
83,168 KB
testcase_33 AC 255 ms
83,992 KB
testcase_34 AC 289 ms
90,828 KB
testcase_35 AC 239 ms
85,284 KB
testcase_36 AC 276 ms
90,140 KB
testcase_37 AC 273 ms
90,284 KB
testcase_38 AC 261 ms
98,032 KB
testcase_39 AC 353 ms
136,952 KB
testcase_40 AC 245 ms
96,692 KB
testcase_41 AC 1,535 ms
379,640 KB
testcase_42 AC 703 ms
99,172 KB
testcase_43 AC 1,516 ms
391,884 KB
testcase_44 AC 1,618 ms
379,876 KB
testcase_45 AC 1,463 ms
391,864 KB
testcase_46 AC 1,648 ms
380,516 KB
testcase_47 AC 1,486 ms
391,908 KB
testcase_48 AC 671 ms
209,132 KB
testcase_49 AC 1,499 ms
393,716 KB
testcase_50 AC 1,478 ms
395,044 KB
testcase_51 AC 829 ms
90,896 KB
testcase_52 AC 1,042 ms
257,868 KB
testcase_53 AC 988 ms
259,124 KB
testcase_54 AC 1,056 ms
257,648 KB
testcase_55 AC 260 ms
84,580 KB
testcase_56 AC 256 ms
84,572 KB
testcase_57 AC 258 ms
84,936 KB
testcase_58 AC 339 ms
94,072 KB
testcase_59 AC 317 ms
98,780 KB
testcase_60 AC 316 ms
99,164 KB
testcase_61 AC 259 ms
84,368 KB
testcase_62 AC 260 ms
85,956 KB
testcase_63 AC 250 ms
83,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0