結果

問題 No.181 A↑↑N mod M
コンテスト
ユーザー maspy
提出日時 2020-04-10 12:46:03
言語 Python3
(3.15.0 + numpy 2.5.4 + scipy 1.18.1 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 308 ms / 5,000 ms
+ 88µs
コード長 595 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 56 ms
コンパイル使用メモリ 15,104 KB
実行使用メモリ 34,092 KB
最終ジャッジ日時 2026-10-10 22:21:24
合計ジャッジ時間 16,514 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np

U = int(10 ** 4)
phi = np.arange(U)
for p in range(2, U):
    if phi[p] == p:
        phi[::p] -= phi[::p] // p


def f(A, N, M):
    if A == 1:
        return 1
    if N == 0:
        return 1
    if M == 1:
        return 1
    MM = int(phi[M])
    x = f(A, N - 1, MM)
    if x >= MM or x >= 20:
        return M + pow(A, x, M)
    x = A ** x
    if x >= M:
        return (x % M) + M
    return x


A, N, M = map(int, read().split())
print(f(A, N, M) % M)
0