結果

問題 No.1595 The Final Digit
ユーザー lam6er
提出日時 2025-03-20 20:25:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 1,434 bytes
コンパイル時間 166 ms
コンパイル使用メモリ 82,372 KB
実行使用メモリ 53,752 KB
最終ジャッジ日時 2025-03-20 20:26:39
合計ジャッジ時間 1,616 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

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

mod_p = p % 10
mod_q = q % 10
mod_r = r % 10

if K == 1:
    print(mod_p)
elif K == 2:
    print(mod_q)
elif K == 3:
    print(mod_r)
else:
    current_state = (mod_p, mod_q, mod_r)
    prev_states = {current_state: 3}
    step = 3  # Last processed step is 3 (A3)
    found_cycle = False

    while step < K:
        step += 1
        a, b, c = current_state
        next_val = (a + b + c) % 10
        new_state = (b, c, next_val)
        if new_state in prev_states:
            # Cycle detected
            cycle_start = prev_states[new_state]
            cycle_length = step - cycle_start
            # Generate cycle_values starting from new_state
            cycle_values = []
            state_in_cycle = new_state
            for _ in range(cycle_length):
                a_cycle, b_cycle, c_cycle = state_in_cycle
                val = (a_cycle + b_cycle + c_cycle) % 10
                cycle_values.append(val)
                state_in_cycle = (b_cycle, c_cycle, val)
            # Calculate the position within the cycle
            start = cycle_start + 1
            remaining = K - start
            remainder = remaining % cycle_length
            print(cycle_values[remainder])
            found_cycle = True
            break
        else:
            prev_states[new_state] = step
            current_state = new_state

    if not found_cycle:
        print(current_state[2])
0