結果

問題 No.181 A↑↑N mod M
ユーザー ayaoni
提出日時 2021-08-31 19:16:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 38 ms / 5,000 ms
コード長 1,112 bytes
コンパイル時間 223 ms
コンパイル使用メモリ 82,184 KB
実行使用メモリ 54,384 KB
最終ジャッジ日時 2024-11-26 02:34:15
合計ジャッジ時間 2,970 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


def phi(n):
    if n == 1:
        return 1
    res = n
    for i in range(2,int(n**.5)+1):
        if n % i == 0:
            res -= res//i
            while n % i == 0:
                n //= i
            if n == 1:
                break
    else:
        res -= res//n
    return res


def tetration(A,B,M):  # A^(A^(A^(...))) (mod M)
    if M == 1:
        return 0
    if A == 0:
        return (B+1) % 2
    if B == 0:
        return 1
    if B == 1:
        return A % M
    if B == 2:
        return pow(A,A,M)
    f = phi(M)
    e = tetration(A,B-1,f)
    if e == 0:
        e = f
    return pow(A,e,M)


A,N,M = MI()
ans = tetration(A,N,M)
print(ans)
0