結果

問題 No.658 テトラナッチ数列 Hard
ユーザー lloyzlloyz
提出日時 2022-08-12 01:00:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,778 ms / 2,000 ms
コード長 849 bytes
コンパイル時間 203 ms
コンパイル使用メモリ 81,824 KB
実行使用メモリ 79,176 KB
最終ジャッジ日時 2023-10-22 05:45:30
合計ジャッジ時間 9,605 ms
ジャッジサーバーID
(参考情報)
judge9 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
53,488 KB
testcase_01 AC 38 ms
53,488 KB
testcase_02 AC 59 ms
66,072 KB
testcase_03 AC 102 ms
75,844 KB
testcase_04 AC 734 ms
77,968 KB
testcase_05 AC 807 ms
77,572 KB
testcase_06 AC 982 ms
78,776 KB
testcase_07 AC 1,047 ms
77,744 KB
testcase_08 AC 1,215 ms
77,904 KB
testcase_09 AC 1,778 ms
78,992 KB
testcase_10 AC 1,772 ms
79,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 17

def matmul(A, B):
    Ah, Bh, Bw = len(A), len(B), len(B[0])
    C = [[0 for _ in range(Bw)] for _ in range(Ah)]
    for i in range(Ah):
        for j in range(Bw):
            for k in range(Bh):
                C[i][j] += A[i][k] * B[k][j] % mod
                C[i][j] %= mod
    return C

# Mのk乗を効率的に計算する
def doubling(M, k):
    k -= 1
    Mc = M.copy()
    while k > 0:
        if k & 1 == 1:
            Mc = matmul(Mc, M)
        M = matmul(M, M) # Mの(2のi乗)の乗 を計算する
        k >>= 1
    return Mc

M = [[1, 1, 1, 1],
     [1, 0, 0, 0],
     [0, 1, 0, 0],
     [0, 0, 1, 0]]
F = [[1], [0], [0], [0]]
q = int(input())
for _ in range(q):
    n = int(input())
    if n <= 4:
        print(F[4 - n][0])
        continue
    n -= 4
    Mn = doubling(M, n)
    T = matmul(Mn, F)
    print(T[0][0])
0