結果

問題 No.2193 メガの下1桁
ユーザー gew1fw
提出日時 2025-06-12 14:14:03
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,596 bytes
コンパイル時間 322 ms
コンパイル使用メモリ 82,816 KB
実行使用メモリ 52,608 KB
最終ジャッジ日時 2025-06-12 14:14:18
合計ジャッジ時間 2,867 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33 WA * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    M = int(sys.stdin.readline())
    D = int(sys.stdin.readline())
    N = int(sys.stdin.readline())
    B = int(sys.stdin.readline())

    if N == 0:
        res = M % B
    else:
        D_mod = D % B
        f = [0] * B
        for x in range(B):
            base = (x + D_mod) % B
            exponent = x
            if base == 0 and exponent == 0:
                f[x] = 1 % B
            else:
                if exponent == 0:
                    f[x] = 1 % B
                else:
                    f[x] = pow(base, exponent, B)
        
        path = []
        state_map = {}
        current = M % B
        state_map[current] = 0
        path.append(current)
        found = False
        for step in range(1, N + 1):
            current = f[current]
            if current in state_map:
                first_occurrence = state_map[current]
                pre_cycle = path[:first_occurrence]
                cycle = path[first_occurrence:]
                cycle_length = len(cycle)
                if N < first_occurrence:
                    res = path[N]
                else:
                    remaining = N - first_occurrence
                    pos_in_cycle = remaining % cycle_length
                    res = cycle[pos_in_cycle]
                found = True
                break
            else:
                state_map[current] = step
                path.append(current)
        if not found:
            res = current
        
    if res == 10:
        print('A')
    else:
        print(res)
    
if __name__ == "__main__":
    main()
0