結果

問題 No.621 3 x N グリッド上のドミノの置き方の数
ユーザー H3PO4H3PO4
提出日時 2024-05-05 10:21:19
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
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
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 466 ms
43,892 KB
testcase_01 AC 468 ms
44,152 KB
testcase_02 AC 466 ms
44,288 KB
testcase_03 AC 460 ms
44,280 KB
testcase_04 AC 468 ms
44,156 KB
testcase_05 AC 457 ms
44,032 KB
testcase_06 AC 462 ms
43,776 KB
testcase_07 AC 460 ms
44,152 KB
testcase_08 AC 461 ms
44,288 KB
testcase_09 AC 457 ms
43,900 KB
testcase_10 AC 463 ms
44,284 KB
testcase_11 AC 462 ms
43,896 KB
testcase_12 AC 482 ms
44,284 KB
testcase_13 AC 479 ms
44,152 KB
testcase_14 AC 485 ms
44,412 KB
testcase_15 AC 493 ms
44,284 KB
testcase_16 AC 506 ms
44,540 KB
testcase_17 AC 514 ms
44,280 KB
testcase_18 AC 530 ms
44,064 KB
testcase_19 AC 529 ms
43,896 KB
testcase_20 AC 541 ms
43,896 KB
testcase_21 AC 531 ms
44,056 KB
testcase_22 AC 562 ms
44,404 KB
testcase_23 AC 552 ms
44,148 KB
testcase_24 AC 568 ms
43,900 KB
testcase_25 AC 583 ms
43,900 KB
testcase_26 AC 580 ms
44,024 KB
testcase_27 AC 607 ms
43,996 KB
testcase_28 AC 600 ms
44,156 KB
testcase_29 AC 606 ms
44,668 KB
testcase_30 AC 615 ms
44,532 KB
testcase_31 AC 604 ms
44,280 KB
testcase_32 AC 602 ms
44,796 KB
testcase_33 AC 604 ms
44,532 KB
testcase_34 AC 610 ms
44,292 KB
testcase_35 AC 595 ms
44,788 KB
testcase_36 AC 599 ms
44,412 KB
testcase_37 AC 603 ms
44,152 KB
testcase_38 AC 620 ms
44,540 KB
testcase_39 AC 608 ms
44,532 KB
testcase_40 AC 635 ms
44,312 KB
testcase_41 AC 616 ms
44,536 KB
testcase_42 AC 609 ms
44,540 KB
testcase_43 AC 608 ms
44,412 KB
testcase_44 AC 619 ms
44,540 KB
testcase_45 AC 612 ms
44,672 KB
testcase_46 AC 626 ms
44,792 KB
testcase_47 AC 612 ms
44,504 KB
testcase_48 AC 631 ms
44,540 KB
testcase_49 AC 623 ms
44,536 KB
testcase_50 AC 618 ms
44,536 KB
testcase_51 AC 618 ms
44,280 KB
testcase_52 AC 616 ms
44,664 KB
testcase_53 AC 614 ms
44,280 KB
testcase_54 AC 630 ms
44,540 KB
testcase_55 AC 622 ms
44,668 KB
testcase_56 AC 618 ms
44,504 KB
testcase_57 AC 625 ms
44,148 KB
testcase_58 AC 632 ms
44,536 KB
testcase_59 AC 640 ms
44,532 KB
testcase_60 AC 639 ms
44,280 KB
testcase_61 AC 653 ms
44,064 KB
testcase_62 AC 651 ms
44,536 KB
testcase_63 AC 653 ms
44,032 KB
testcase_64 AC 562 ms
44,160 KB
testcase_65 AC 559 ms
44,792 KB
testcase_66 AC 565 ms
44,804 KB
testcase_67 AC 568 ms
44,676 KB
権限があれば一括ダウンロードができます

ソースコード

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)), 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)
0