結果

問題 No.621 3 x N グリッド上のドミノの置き方の数
ユーザー maspymaspy
提出日時 2020-04-08 14:25:16
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 2,228 ms / 3,000 ms
コード長 1,893 bytes
コンパイル時間 79 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 44,744 KB
最終ジャッジ日時 2024-07-18 11:50:52
合計ジャッジ時間 102,047 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 66
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import itertools
import numpy as np
MOD = 10 ** 9 + 7

# 0: empty, 1~: RULD


def cnt_mat(i, j):
    x1, y1, z1 = i % 5, (i // 5) % 5, i // 25
    x2, y2, z2 = j % 5, (j // 5) % 5, j // 25
    # 2つ連続の空マスは許容しない
    if any(s == t == 0 for s, t in ((x1, x2), (y1, y2), (z1, z2), (x1, y1), (y1, z1), (x2, y2), (y2, z2))):
        return 0
    # 貼り合わせ右
    if any(s == 1 and t != 3 for s, t in ((x1, x2), (y1, y2), (z1, z2))):
        return 0
    # 貼り合わせ左
    if any(s == 3 and t != 1 for s, t in ((x2, x1), (y2, y1), (z2, z1))):
        return 0
    # 貼り合わせ下
    if any(s == 4 and t != 2 for s, t in ((x1, y1), (y1, z1), (x2, y2), (y2, z2))):
        return 0
    # 貼り合わせ上
    if any(s != 4 and t == 2 for s, t in ((x1, y1), (y1, z1), (x2, y2), (y2, z2))):
        return 0
    # はみ出し
    if x1 == 2 or x2 == 2 or z1 == 4 or z2 == 4:
        return 0
    return 1


mat = np.zeros((125, 125), np.int64)
for i, j in itertools.product(range(125), repeat=2):
    mat[i, j] = cnt_mat(i, j)


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


def power(A, n):
    if n == 1:
        return A
    B = power(A, n // 2)
    B = mat_mul(B, B)
    return mat_mul(A, B) if n & 1 else B


N = int(read())
init = 93
A = power(mat, N)[init, :]

answer = 0
for i in range(125):
    x, y, z = i % 5, (i // 5) % 5, i // 25
    if x != 1 and y != 1 and z != 1 and A[i] != 0:
        answer += A[i]
answer %= MOD
print(answer)
0