結果

問題 No.1595 The Final Digit
ユーザー T0RA1107T0RA1107
提出日時 2021-10-09 15:52:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 142 ms / 2,000 ms
コード長 941 bytes
コンパイル時間 235 ms
コンパイル使用メモリ 10,992 KB
実行使用メモリ 29,796 KB
最終ジャッジ日時 2023-10-11 02:02:29
合計ジャッジ時間 4,014 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
29,628 KB
testcase_01 AC 125 ms
29,652 KB
testcase_02 AC 125 ms
29,724 KB
testcase_03 AC 126 ms
29,764 KB
testcase_04 AC 125 ms
29,680 KB
testcase_05 AC 129 ms
29,756 KB
testcase_06 AC 129 ms
29,660 KB
testcase_07 AC 131 ms
29,624 KB
testcase_08 AC 132 ms
29,640 KB
testcase_09 AC 131 ms
29,728 KB
testcase_10 AC 126 ms
29,632 KB
testcase_11 AC 129 ms
29,696 KB
testcase_12 AC 126 ms
29,700 KB
testcase_13 AC 134 ms
29,572 KB
testcase_14 AC 132 ms
29,652 KB
testcase_15 AC 130 ms
29,796 KB
testcase_16 AC 132 ms
29,688 KB
testcase_17 AC 130 ms
29,608 KB
testcase_18 AC 127 ms
29,644 KB
testcase_19 AC 130 ms
29,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

def from_read(dtype=np.int64):
    return np.fromstring(read().decode(), dtype=dtype, sep=' ')

def from_readline(dtype=np.int64):
    return np.fromstring(readline().decode(), dtype=dtype, sep=' ')

def read_matrix(N, M, dtype=np.int64):
    matrix = np.empty((N, M), dtype=dtype)
    for i in range(N):
        matrix[i] = from_readline(dtype)
    return matrix

def matrix_pow(A, N):
    res = np.eye(len(A), dtype=np.int64)
    while N:
        if N & 1:
            res = res @ A % 10
        A = A @ A % 10
        N >>= 1
    return res

def solve(p, q, r, K):
    A = np.array([[1, 1, 1], [1, 0, 0], [0, 1, 0]])
    B = matrix_pow(A, K - 3)
    res = B[0, 0] * r + B[0, 1] * q + B[0, 2] * p
    res %= 10
    return res

def main():
    p, q, r, K = from_readline()
    print(solve(p, q, r, K))

if __name__ == "__main__":
    main()

0