結果

問題 No.181 A↑↑N mod M
ユーザー btk
提出日時 2015-07-07 17:19:38
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 152 ms / 5,000 ms
コード長 686 bytes
コンパイル時間 196 ms
コンパイル使用メモリ 12,288 KB
実行使用メモリ 24,960 KB
最終ジャッジ日時 2024-12-30 10:16:34
合計ジャッジ時間 9,691 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

from pip._vendor.distlib.compat import raw_input

def find_cycle(a,m):
    memo ={1:0}
    p=1
    n=1
    while (a*p)%m  not in memo :
        p=(a*p)%m
        memo[p]=n
        n=n+1
    return (memo[(a*p)%m],n-memo[(a*p)%m])

def getval(a,n,s):
    res=1
    for i in range(0,n):
        res=pow(a,res)
        if res>s:
            return -1
    return res

def modtetration(a,n,m):
    if m==1:
        return 0
    if a==1 or n==0:
        return 1
    s,T=find_cycle(a,m)
    g=getval(a,n-1,s)
    if g>=0:
        p=pow(a,g,m)
    else:
        p= pow(a,s+((modtetration(a, n-1, T)-s+1000*T)%T),m)
    return p

A, N, M=map(int,raw_input().split())
print(modtetration(A, N, M))
0