結果

問題 No.2193 メガの下1桁
ユーザー lam6er
提出日時 2025-04-16 16:29:21
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,137 bytes
コンパイル時間 562 ms
コンパイル使用メモリ 81,656 KB
実行使用メモリ 53,564 KB
最終ジャッジ日時 2025-04-16 16:30:51
合計ジャッジ時間 3,393 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33 WA * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

M = int(input())
D = int(input())
N = int(input())
B = int(input())

current = M % B

if N == 0:
    res = current
else:
    history = {current: 0}
    sequence = [current]
    steps = 0
    found_cycle = False
    while steps < N:
        base = (current + D) % B
        exponent = current
        if base == 0:
            if exponent == 0:
                next_current = 1 % B
            else:
                next_current = 0
        else:
            next_current = pow(base, exponent, B)
        steps += 1
        if next_current in history:
            cycle_start = history[next_current]
            cycle_length = steps - cycle_start
            remaining_steps = N - cycle_start
            if remaining_steps < 0:
                res = sequence[N]
            else:
                res = sequence[cycle_start + (remaining_steps % cycle_length)]
            found_cycle = True
            break
        else:
            history[next_current] = steps
            sequence.append(next_current)
            current = next_current
    if not found_cycle:
        res = current

if res == 10:
    print('A')
else:
    print(res)
0