結果

問題 No.181 A↑↑N mod M
ユーザー __math__math
提出日時 2015-04-05 23:53:30
言語 Python2
(2.7.18)
結果
WA  
実行時間 -
コード長 1,120 bytes
コンパイル時間 207 ms
コンパイル使用メモリ 6,644 KB
実行使用メモリ 7,152 KB
最終ジャッジ日時 2023-09-17 04:47:06
合計ジャッジ時間 2,863 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
6,956 KB
testcase_01 AC 28 ms
7,092 KB
testcase_02 AC 27 ms
7,052 KB
testcase_03 AC 26 ms
7,052 KB
testcase_04 AC 26 ms
6,956 KB
testcase_05 AC 25 ms
6,992 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 27 ms
7,096 KB
testcase_10 AC 26 ms
7,108 KB
testcase_11 AC 26 ms
6,944 KB
testcase_12 AC 26 ms
6,936 KB
testcase_13 AC 26 ms
7,056 KB
testcase_14 AC 26 ms
6,940 KB
testcase_15 AC 26 ms
7,092 KB
testcase_16 AC 26 ms
7,116 KB
testcase_17 AC 26 ms
7,032 KB
testcase_18 AC 26 ms
6,952 KB
testcase_19 AC 25 ms
6,960 KB
testcase_20 AC 26 ms
6,960 KB
testcase_21 AC 26 ms
7,096 KB
testcase_22 AC 26 ms
7,056 KB
testcase_23 WA -
testcase_24 AC 26 ms
7,092 KB
testcase_25 AC 26 ms
7,100 KB
testcase_26 AC 26 ms
7,096 KB
testcase_27 AC 26 ms
6,920 KB
testcase_28 WA -
testcase_29 AC 27 ms
6,960 KB
testcase_30 AC 26 ms
7,052 KB
testcase_31 AC 26 ms
6,968 KB
testcase_32 AC 26 ms
7,036 KB
testcase_33 AC 26 ms
6,956 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 25 ms
6,944 KB
testcase_38 WA -
testcase_39 AC 26 ms
7,092 KB
testcase_40 AC 26 ms
6,960 KB
testcase_41 WA -
testcase_42 AC 26 ms
6,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#coding=utf-8
import time
import math

def primes(n):
    return list(i_primes(n))

def i_primes(n):
    p = [True] * (n+1)
    p[0] = p[1] = False
    max = int(math.sqrt(n+1))
    for i in xrange(2,max):
        if p[i]:
            yield i
            for j in xrange(i*2,n+1,i):
                p[j] = False
    for i in xrange(max,n+1):
        if p[i]: yield i

pr = primes(10**5)

def euler_phi(n):
    ret = n
    for i in pr:
        if i*i > n: break
        if n % i == 0:
            while n%i==0:
                n/=i
            ret = ret/i*(i-1)

    if n != 1:
        ret = ret/n*(n-1)
    return ret

def tetration(a,b,mod):
    if b == 0: return 1
    return pow(a,tetration(a,b-1,euler_phi(mod)),mod)

def tetration2(a,b,mod):
    if b == 0:
        return 1 % mod
    dp = [mod]
    for i in xrange(1,b):
        dp.append(euler_phi(dp[i-1]))
        if dp[-1] == 1:
            break

    ans = 1
    for i in reversed(dp):
        # print "%d^%d %% %d" % (a,ans,i)
        ans = pow(a,ans,i)
    return ans

def main():
    a,n,m = map(int,raw_input().split())
    print tetration2(a,n,m)

main()
0