結果

問題 No.1595 The Final Digit
ユーザー tktk_snsntktk_snsn
提出日時 2021-07-09 21:38:43
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 805 bytes
コンパイル時間 647 ms
コンパイル使用メモリ 10,888 KB
実行使用メモリ 8,368 KB
最終ジャッジ日時 2023-09-14 08:06:58
合計ジャッジ時間 1,931 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,292 KB
testcase_01 AC 17 ms
8,240 KB
testcase_02 AC 17 ms
8,244 KB
testcase_03 AC 17 ms
8,368 KB
testcase_04 AC 17 ms
8,240 KB
testcase_05 AC 16 ms
8,240 KB
testcase_06 AC 17 ms
8,240 KB
testcase_07 AC 18 ms
8,192 KB
testcase_08 AC 16 ms
8,236 KB
testcase_09 AC 16 ms
8,304 KB
testcase_10 AC 16 ms
8,324 KB
testcase_11 AC 17 ms
8,324 KB
testcase_12 AC 17 ms
8,192 KB
testcase_13 AC 17 ms
7,916 KB
testcase_14 AC 17 ms
8,152 KB
testcase_15 AC 17 ms
8,204 KB
testcase_16 AC 17 ms
8,236 KB
testcase_17 AC 17 ms
8,328 KB
testcase_18 AC 16 ms
8,268 KB
testcase_19 AC 17 ms
8,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 10


def mat_mul(A, B):
    res = [[0] * len(B[0]) for _ in range(len(A))]
    for i in range(len(A)):
        for k in range(len(A[0])):
            for j in range(len(B[0])):
                res[i][j] += A[i][k] * B[k][j]
                res[i][j] %= mod
    return res


def mat_pow(A, n):
    size = len(A)
    res = [[0] * size for _ in range(size)]
    for i in range(size):
        res[i][i] = 1
    while n:
        if n & 1:
            res = mat_mul(res, A)
        A = mat_mul(A, A)
        n >>= 1
    return res


p, q, r, k = map(int, input().split())
p %= mod
q %= mod
r %= mod

a, b, c = p, q, r
k -= 1
if k < 3:
    print([p, q, r][k])
    exit()

A = [[1, 1, 1], [1, 0, 0], [0, 1, 0]]
A = mat_pow(A, k - 2)
ans = A[0][0] * r + A[0][1] * q + A[0][2] * p
ans %= mod
print(ans % mod)
0