結果

問題 No.1595 The Final Digit
コンテスト
ユーザー norioc
提出日時 2021-07-10 04:39:12
言語 Python3
(3.14.7 + numpy 2.5.2 + scipy 1.18.0 + ac-library)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 90 ms / 2,000 ms
+ 615µs
コード長 691 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 276 ms
コンパイル使用メモリ 21,544 KB
実行使用メモリ 15,868 KB
最終ジャッジ日時 2026-08-03 15:35:51
合計ジャッジ時間 3,365 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from itertools import count


def cycle_detection(k, inits, fnext):
    memo = {}
    memo[inits[0], inits[1], inits[2]] = 2
    a = inits[:]
    for i in count(len(inits)):
        x = fnext(a)
        a.append(x)
        if i == k:
            return x

        key = (a[-3], a[-2], a[-1])
        if key in memo: # found cycle
            loop_start = memo[key]
            loop_len = i - loop_start
            index = (k - loop_start) % loop_len + loop_start
            return a[index]
        memo[x] = i


p, q, r, K = map(int, input().split())
p %= 10
q %= 10
r %= 10


def fnext(a):
    return (a[-1] + a[-2] + a[-3]) % 10


ans = cycle_detection(K-1, [p, q, r], fnext)
print(ans)
0