結果

問題 No.181 A↑↑N mod M
ユーザー convexineq
提出日時 2020-03-18 18:46:23
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 34 ms / 5,000 ms
コード長 1,089 bytes
コンパイル時間 131 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-12-14 02:22:22
合計ジャッジ時間 2,975 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

def totient(N): #トーシェント関数
    res = N
    if N&1==0:
        res //= 2
        while N%2 == 0: N //= 2
    M = int(N**0.5)+1
    for i in range(3,M,2):
        if N%i==0:
            res = res//i*(i-1)
            while N%i == 0: N //= i
    if N!= 1: res = res//N*(N-1)
    return res
    
def tetration_mod(a,n,MOD): #a↑↑n mod MOD を求める
    if a==0: return (1-(n&1))%MOD
    if a==1: return 1%MOD
    if n==0: return 1%MOD
    if n==1: return a%MOD
    return pow(a,tetration_amend(a,n-1,totient(MOD)),MOD)%MOD
    
def tetration_amend(a,n,MOD): # #a↑↑n mod MOD (ただし周期修正あり)を求める
    # 小さくて周期に入っていない
    if n==0: return 1
    if n==1: return a
    if a==2:
        if n==2: return 4
        if n==3: return 16
    if a==3 and n==2: return 27
    # MOD=1 の場合は値が確定
    if MOD == 1: return 65
    #でかい
    x = pow(a,tetration_amend(a,n-1,totient(MOD)),MOD)
    if x >= 65: return x
    else: return x + (65+MOD)//MOD*MOD

a,b,m = map(int,input().split())
print(tetration_mod(a,b,m))
0