結果

問題 No.569 3 x N グリッドのパスの数
ユーザー maspymaspy
提出日時 2020-05-06 16:08:59
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 515 ms / 2,000 ms
コード長 2,314 bytes
コンパイル時間 177 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 44,652 KB
最終ジャッジ日時 2024-06-28 17:37:33
合計ジャッジ時間 31,244 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 451 ms
44,132 KB
testcase_01 AC 462 ms
44,264 KB
testcase_02 AC 463 ms
44,392 KB
testcase_03 AC 476 ms
44,136 KB
testcase_04 AC 481 ms
44,008 KB
testcase_05 AC 458 ms
44,144 KB
testcase_06 AC 456 ms
44,012 KB
testcase_07 AC 461 ms
44,404 KB
testcase_08 AC 458 ms
44,136 KB
testcase_09 AC 463 ms
44,136 KB
testcase_10 AC 464 ms
44,396 KB
testcase_11 AC 466 ms
44,144 KB
testcase_12 AC 466 ms
44,520 KB
testcase_13 AC 463 ms
44,136 KB
testcase_14 AC 460 ms
44,396 KB
testcase_15 AC 460 ms
44,016 KB
testcase_16 AC 459 ms
44,008 KB
testcase_17 AC 455 ms
44,004 KB
testcase_18 AC 455 ms
44,388 KB
testcase_19 AC 466 ms
44,136 KB
testcase_20 AC 477 ms
44,388 KB
testcase_21 AC 479 ms
44,648 KB
testcase_22 AC 479 ms
44,136 KB
testcase_23 AC 478 ms
44,012 KB
testcase_24 AC 470 ms
44,272 KB
testcase_25 AC 475 ms
44,396 KB
testcase_26 AC 476 ms
44,544 KB
testcase_27 AC 479 ms
44,016 KB
testcase_28 AC 482 ms
44,400 KB
testcase_29 AC 477 ms
44,140 KB
testcase_30 AC 473 ms
44,012 KB
testcase_31 AC 472 ms
44,004 KB
testcase_32 AC 479 ms
44,520 KB
testcase_33 AC 473 ms
44,528 KB
testcase_34 AC 471 ms
44,396 KB
testcase_35 AC 472 ms
44,388 KB
testcase_36 AC 476 ms
44,484 KB
testcase_37 AC 471 ms
44,012 KB
testcase_38 AC 475 ms
44,396 KB
testcase_39 AC 469 ms
44,528 KB
testcase_40 AC 485 ms
44,144 KB
testcase_41 AC 489 ms
44,004 KB
testcase_42 AC 494 ms
44,008 KB
testcase_43 AC 494 ms
44,392 KB
testcase_44 AC 504 ms
44,004 KB
testcase_45 AC 497 ms
44,388 KB
testcase_46 AC 496 ms
44,524 KB
testcase_47 AC 507 ms
44,388 KB
testcase_48 AC 504 ms
44,520 KB
testcase_49 AC 508 ms
44,520 KB
testcase_50 AC 497 ms
43,880 KB
testcase_51 AC 506 ms
44,392 KB
testcase_52 AC 501 ms
44,004 KB
testcase_53 AC 503 ms
44,136 KB
testcase_54 AC 501 ms
44,400 KB
testcase_55 AC 515 ms
44,392 KB
testcase_56 AC 498 ms
44,652 KB
testcase_57 AC 492 ms
44,528 KB
testcase_58 AC 499 ms
44,132 KB
testcase_59 AC 503 ms
44,516 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
    
    for i in range(3):
        if vertical & (1<<i):
            deg[i] += 1
            deg[i+1] += 1
            if status[i] == status[i+1] != 0:
                # loop
                return None
            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 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)):
        if k == 0:
            continue
        for v in range(4):
            if status[v] == k:
                status[v] = n
    return tuple(status)

visited = set([(1,0,0,0)])
q = [(1,0,0,0)]
edges = []
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))
        if w in visited:
            continue
        visited.add(w)
        q.append(w)

L = len(visited)
A = np.zeros((L,L), dtype=object)
v_to_i = {v:i for i,v in enumerate(visited)}
i0 = v_to_i[(1,0,0,0)]
j0 = v_to_i[(0,0,0,1)]
for v,w in edges:
    A[v_to_i[w], v_to_i[v]] = 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

MOD = 10**9 + 7
N = int(read())
print(matrix_power(A, N+1)[j0, i0])
0