結果

問題 No.1136 Four Points Tour
ユーザー norioc
提出日時 2026-11-02 09:56:49
言語 PyPy3
(8.0.1 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 41 ms / 2,000 ms
+ 619µs
コード長 954 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 85 ms
コンパイル使用メモリ 64,480 KB
実行使用メモリ 58,400 KB
最終ジャッジ日時 2026-11-02 09:56:54
合計ジャッジ時間 4,433 ms
ジャッジサーバーID
(参考情報)
judge7_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

def mat_pow(m, n: int, mod: int):
    assert n >= 0
    size = len(m)
    assert size == len(m[0])

    if n == 0: return [[int(i == j) for j in range(size)] for i in range(size)]
    if n == 1: return m

    res = mat_pow(m, n // 2, mod)
    res = mat_mul(res, res, mod)
    if n % 2 == 1:
        res = mat_mul(res, m, mod)

    return res


def mat_mul(a, b, mod: int):
    ar, ac, br, bc = len(a), len(a[0]), len(b), len(b[0])
    assert ac == br

    res = [[0] * bc for _ in range(ar)]
    for i in range(ar):
        out = res[i]
        for j in range(bc):
            r = a[i]
            x = 0
            for k in range(ac):
                x += r[k] * b[k][j]
                x %= mod
            out[j] = x

    return res


MOD = 10**9 + 7
N = int(input())

mat = [[0, 1, 1, 1],
       [1, 0, 1, 1],
       [1, 1, 0, 1],
       [1, 1, 1, 0]]

m = mat_pow(mat, N, MOD)
a = [[1, 0, 0, 0]]
res = mat_mul(a, m, MOD)

ans = res[0][0]
print(ans)
0