結果

問題 No.579 3 x N グリッド上のサイクルのサイズ(hard)
ユーザー maspymaspy
提出日時 2020-05-06 18:23:41
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 684 ms / 2,000 ms
コード長 2,982 bytes
コンパイル時間 94 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 44,948 KB
最終ジャッジ日時 2024-07-03 09:05:39
合計ジャッジ時間 44,679 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 461 ms
44,560 KB
testcase_01 AC 454 ms
44,304 KB
testcase_02 AC 466 ms
44,560 KB
testcase_03 AC 470 ms
44,560 KB
testcase_04 AC 462 ms
44,692 KB
testcase_05 AC 461 ms
44,724 KB
testcase_06 AC 456 ms
44,300 KB
testcase_07 AC 459 ms
44,688 KB
testcase_08 AC 457 ms
44,304 KB
testcase_09 AC 463 ms
44,560 KB
testcase_10 AC 465 ms
44,304 KB
testcase_11 AC 463 ms
44,300 KB
testcase_12 AC 456 ms
44,560 KB
testcase_13 AC 458 ms
44,560 KB
testcase_14 AC 469 ms
44,176 KB
testcase_15 AC 455 ms
44,564 KB
testcase_16 AC 462 ms
44,560 KB
testcase_17 AC 462 ms
44,300 KB
testcase_18 AC 469 ms
44,300 KB
testcase_19 AC 460 ms
44,056 KB
testcase_20 AC 473 ms
44,304 KB
testcase_21 AC 482 ms
44,556 KB
testcase_22 AC 505 ms
44,812 KB
testcase_23 AC 490 ms
44,308 KB
testcase_24 AC 496 ms
44,696 KB
testcase_25 AC 495 ms
44,608 KB
testcase_26 AC 499 ms
44,176 KB
testcase_27 AC 488 ms
44,944 KB
testcase_28 AC 490 ms
44,560 KB
testcase_29 AC 502 ms
44,684 KB
testcase_30 AC 507 ms
44,556 KB
testcase_31 AC 506 ms
44,300 KB
testcase_32 AC 486 ms
44,172 KB
testcase_33 AC 488 ms
44,812 KB
testcase_34 AC 486 ms
44,648 KB
testcase_35 AC 496 ms
44,308 KB
testcase_36 AC 487 ms
44,180 KB
testcase_37 AC 493 ms
44,816 KB
testcase_38 AC 492 ms
44,684 KB
testcase_39 AC 486 ms
44,428 KB
testcase_40 AC 532 ms
44,560 KB
testcase_41 AC 536 ms
44,692 KB
testcase_42 AC 553 ms
44,436 KB
testcase_43 AC 532 ms
44,688 KB
testcase_44 AC 530 ms
44,564 KB
testcase_45 AC 528 ms
44,812 KB
testcase_46 AC 531 ms
44,560 KB
testcase_47 AC 527 ms
44,180 KB
testcase_48 AC 538 ms
44,556 KB
testcase_49 AC 544 ms
44,564 KB
testcase_50 AC 527 ms
44,176 KB
testcase_51 AC 530 ms
44,300 KB
testcase_52 AC 530 ms
44,560 KB
testcase_53 AC 535 ms
44,940 KB
testcase_54 AC 523 ms
44,560 KB
testcase_55 AC 541 ms
44,560 KB
testcase_56 AC 528 ms
44,716 KB
testcase_57 AC 531 ms
44,176 KB
testcase_58 AC 528 ms
44,560 KB
testcase_59 AC 530 ms
44,688 KB
testcase_60 AC 594 ms
44,052 KB
testcase_61 AC 593 ms
44,180 KB
testcase_62 AC 684 ms
44,308 KB
testcase_63 AC 617 ms
44,340 KB
testcase_64 AC 612 ms
44,684 KB
testcase_65 AC 610 ms
44,176 KB
testcase_66 AC 622 ms
44,304 KB
testcase_67 AC 607 ms
44,944 KB
testcase_68 AC 620 ms
44,308 KB
testcase_69 AC 606 ms
44,784 KB
testcase_70 AC 622 ms
44,176 KB
testcase_71 AC 619 ms
44,560 KB
testcase_72 AC 609 ms
44,172 KB
testcase_73 AC 607 ms
44,948 KB
testcase_74 AC 620 ms
44,692 KB
testcase_75 AC 618 ms
44,688 KB
testcase_76 AC 615 ms
44,304 KB
testcase_77 AC 604 ms
44,312 KB
testcase_78 AC 610 ms
44,432 KB
testcase_79 AC 612 ms
44,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# 頂点の代わりに辺を選ぶ

MOD = 10**9 + 7

def compute_next_status(status, vertical):
    status = list(status)
    deg = [0] * 4
    for i, x in enumerate(status):
        if x != 0:
            deg[i] = 1

    loop = False
    for i in range(3):
        if vertical & (1 << i):
            deg[i] += 1
            deg[i+1] += 1
            if status[i] == status[i+1] != 0:
                if loop:
                    return None
                loop = True
                status[i] = status[i+1] = 0
                continue
            if status[i] == status[i+1] == 0:
                status[i] = status[i+1] = max(status) + 1
            elif status[i] != 0 and status[i+1] == 0:
                status[i+1] = status[i]
            elif status[i+1] != 0 and status[i] == 0:
                status[i] = status[i+1]
            else:
                n, m = status[i:i+2]
                n, m = max(n, m), min(n, m)
                for v in range(4):
                    if status[v] == m:
                        status[v] = n
    if loop:
        if any(x in [1,3] for x in deg):
            return None
        return (0,0,0,0)
    if any(x >= 3 for x in deg):
        return None
    for v in range(4):
        if deg[v] != 1:
            status[v] = 0
    for n, k in enumerate(set(status + [0])):
        if k == 0:
            continue
        for v in range(4):
            if status[v] == k:
                status[v] = n
    return tuple(status)

visited = set([(0,0,0,0)])
q = [(0,0,0,0)]
edges = []  # 遷移に加えて、追加した辺の本数を持たせる
popcount = [0,1,1,2,1,2,2,3]
while q:
    v = q.pop()
    for i in range(8):
        w = compute_next_status(v, i)
        if w is None:
            continue
        edges.append((v,w, popcount[i] + sum(x != 0 for x in w)))
        if w in visited:
            continue
        visited.add(w)
        q.append(w)

L = len(visited) + 1
A = np.zeros((2*L,2*L), dtype=object)
v_to_i = {v:i for i,v in enumerate(visited)}
i0 = v_to_i[(0,0,0,0)]
i_end = L - 1
for v,w,k in edges:
    iv, iw = v_to_i[v], v_to_i[w]
    if iv != i0 and iw == i0:
        iw = i_end
    A[2*iw, 2*iv] = 1 # 選択なし -> 選択なし
    A[2*iw+1, 2*iv] = k # 選択なし -> 選択あり
    A[2*iw+1,2*iv+1] = 1  # 選択あり -> 選択あり

A[2*i_end, 2*i_end] += 1
A[2*i_end+1, 2*i_end+1] += 1

def mult_mod(A, B, MOD=MOD):
    mask = (1<<15) - 1
    A1, A2 = A>>15, A & mask
    B1, B2 = B>>15, B&mask
    X = np.dot(A1, B1) % MOD
    Z = np.dot(A2, B2) % MOD
    Y = (np.dot(A1+A2,B1+B2)-X-Z)%MOD
    return ((X<<30)+(Y<<15)+Z)%MOD

def matrix_power(A, n, MOD=MOD):
    if n == 1:
        return A
    B = matrix_power(A, n//2)
    B = mult_mod(B, B)
    if n & 1:
        B = mult_mod(A, B)
    return B

N = int(read())
print(matrix_power(A, N + 1)[2*i_end+1, 2*i0])
0