結果

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

ソースコード

diff #

def compute_next(current, D_mod, B):
    base = (current + D_mod) % B
    exponent = current
    if base == 0 and exponent == 0:
        return 1 % B
    return pow(base, exponent, B)

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

D_mod = D % B
current = M % B

if N == 0:
    res = current
else:
    history = [current]  # history[0] is the initial value (0 applications)
    # Apply the first transformation
    current = compute_next(current, D_mod, B)
    history.append(current)  # history[1] is after 1 application
    found = False
    for i in range(1, N):  # We need to apply N transformations in total
        next_val = compute_next(current, D_mod, B)
        if next_val in history:
            start_idx = history.index(next_val)
            cycle = history[start_idx:]
            cycle_len = len(cycle)
            remaining = N - (i + 1)  # remaining steps after i+1 transformations
            if remaining <= 0:
                res = next_val
            else:
                pos = remaining % cycle_len
                res = cycle[pos]
            found = True
            break
        history.append(next_val)
        current = next_val
    if not found:
        res = current  # after N transformations

# Convert res to B-based digit
if res < 10:
    print(res)
else:
    print('A')
0