結果
| 問題 | No.621 3 x N グリッド上のドミノの置き方の数 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2024-05-05 10:21:19 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 653 ms / 3,000 ms | 
| コード長 | 1,409 bytes | 
| コンパイル時間 | 182 ms | 
| コンパイル使用メモリ | 12,672 KB | 
| 実行使用メモリ | 44,804 KB | 
| 最終ジャッジ日時 | 2024-11-27 06:26:59 | 
| 合計ジャッジ時間 | 43,335 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 66 | 
ソースコード
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)), dtype=object)
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=object)
for i, col in enumerate(cols):
    if 1 in col:
        continue
    start[i] = 1
end = np.zeros(len(cols), dtype=object)
for i, col in enumerate(cols):
    if 2 in col:
        continue
    end[i] = 1
ans = start @ pow_matrix_mod(matr, N - 1) @ end
ans = int(ans) % MOD
print(ans)
            
            
            
        