結果

問題 No.621 3 x N グリッド上のドミノの置き方の数
ユーザー H3PO4
提出日時 2024-05-05 10:15:39
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 1,391 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 45,044 KB
最終ジャッジ日時 2024-11-27 06:19:55
合計ジャッジ時間 44,265 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 10 WA * 56
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools
import numpy as np

MOD = 10**9 + 7


def pow_matrix_mod(x, n, mod=MOD):
    x = x.astype(object)
    if not n:
        return np.eye(len(x), dtype=object)
    if n % 2 == 0:
        return pow_matrix_mod(x @ x % mod, n // 2) % mod
    else:
        return x @ pow_matrix_mod(x @ x % mod, (n - 1) // 2) % mod


# 0: ドミノ無し, 1: 左列にまたがる横ドミノあり,2: 右列にまたがる横ドミノあり,3: 縦ドミノあり
cols = []
for i in range(3):
    cols.append((3, 3, i))
    cols.append((i, 3, 3))
for t in itertools.product(range(3), repeat=3):
    if t[0] == 0 and t[1] == 0:
        continue
    if t[1] == 0 and t[2] == 0:
        continue
    cols.append(t)

matr = np.zeros((len(cols), len(cols)))
for i, col_left in enumerate(cols):
    for j, col_right in enumerate(cols):
        for k in range(3):
            if col_left[k] == 0 and col_right[k] == 0:
                break
            if (col_left[k] == 2) ^ (col_right[k] == 1):
                break
        else:
            matr[i, j] = 1

N = int(input())
start = np.zeros(len(cols), dtype=np.int64)
for i, col in enumerate(cols):
    if 1 in col:
        continue
    start[i] = 1
end = np.zeros(len(cols), dtype=np.int64)
for i, col in enumerate(cols):
    if 2 in col:
        continue
    end[i] = 1

print(start @ pow_matrix_mod(matr, N - 1).astype(np.int64) @ end % MOD)
0