結果

問題 No.181 A↑↑N mod M
ユーザー btkbtk
提出日時 2015-07-07 17:19:38
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 116 ms / 5,000 ms
コード長 686 bytes
コンパイル時間 203 ms
コンパイル使用メモリ 10,908 KB
実行使用メモリ 20,772 KB
最終ジャッジ日時 2023-08-28 21:32:19
合計ジャッジ時間 8,802 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
20,516 KB
testcase_01 AC 112 ms
20,668 KB
testcase_02 AC 112 ms
20,544 KB
testcase_03 AC 111 ms
20,700 KB
testcase_04 AC 112 ms
20,576 KB
testcase_05 AC 114 ms
20,772 KB
testcase_06 AC 115 ms
20,452 KB
testcase_07 AC 114 ms
20,460 KB
testcase_08 AC 112 ms
20,732 KB
testcase_09 AC 112 ms
20,472 KB
testcase_10 AC 112 ms
20,460 KB
testcase_11 AC 113 ms
20,456 KB
testcase_12 AC 115 ms
20,656 KB
testcase_13 AC 115 ms
20,412 KB
testcase_14 AC 112 ms
20,732 KB
testcase_15 AC 111 ms
20,704 KB
testcase_16 AC 110 ms
20,424 KB
testcase_17 AC 113 ms
20,424 KB
testcase_18 AC 110 ms
20,648 KB
testcase_19 AC 111 ms
20,644 KB
testcase_20 AC 114 ms
20,616 KB
testcase_21 AC 115 ms
20,416 KB
testcase_22 AC 116 ms
20,708 KB
testcase_23 AC 113 ms
20,680 KB
testcase_24 AC 113 ms
20,652 KB
testcase_25 AC 112 ms
20,716 KB
testcase_26 AC 113 ms
20,680 KB
testcase_27 AC 111 ms
20,492 KB
testcase_28 AC 112 ms
20,420 KB
testcase_29 AC 112 ms
20,480 KB
testcase_30 AC 112 ms
20,416 KB
testcase_31 AC 110 ms
20,412 KB
testcase_32 AC 113 ms
20,740 KB
testcase_33 AC 112 ms
20,420 KB
testcase_34 AC 112 ms
20,572 KB
testcase_35 AC 111 ms
20,472 KB
testcase_36 AC 111 ms
20,476 KB
testcase_37 AC 112 ms
20,416 KB
testcase_38 AC 112 ms
20,568 KB
testcase_39 AC 111 ms
20,708 KB
testcase_40 AC 110 ms
20,452 KB
testcase_41 AC 110 ms
20,676 KB
testcase_42 AC 113 ms
20,768 KB
権限があれば一括ダウンロードができます

ソースコード

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