結果

問題 No.181 A↑↑N mod M
ユーザー Eki1009Eki1009
提出日時 2020-12-25 17:16:31
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 35 ms / 5,000 ms
コード長 1,687 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 11,988 KB
実行使用メモリ 10,536 KB
最終ジャッジ日時 2023-10-23 15:54:29
合計ジャッジ時間 3,606 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
10,536 KB
testcase_01 AC 33 ms
10,536 KB
testcase_02 AC 33 ms
10,536 KB
testcase_03 AC 32 ms
10,536 KB
testcase_04 AC 32 ms
10,536 KB
testcase_05 AC 31 ms
10,536 KB
testcase_06 AC 31 ms
10,536 KB
testcase_07 AC 34 ms
10,536 KB
testcase_08 AC 31 ms
10,536 KB
testcase_09 AC 32 ms
10,536 KB
testcase_10 AC 35 ms
10,536 KB
testcase_11 AC 32 ms
10,536 KB
testcase_12 AC 32 ms
10,536 KB
testcase_13 AC 32 ms
10,536 KB
testcase_14 AC 32 ms
10,536 KB
testcase_15 AC 33 ms
10,536 KB
testcase_16 AC 33 ms
10,536 KB
testcase_17 AC 33 ms
10,536 KB
testcase_18 AC 32 ms
10,536 KB
testcase_19 AC 32 ms
10,536 KB
testcase_20 AC 33 ms
10,536 KB
testcase_21 AC 32 ms
10,536 KB
testcase_22 AC 32 ms
10,536 KB
testcase_23 AC 32 ms
10,536 KB
testcase_24 AC 32 ms
10,536 KB
testcase_25 AC 32 ms
10,536 KB
testcase_26 AC 32 ms
10,536 KB
testcase_27 AC 32 ms
10,536 KB
testcase_28 AC 32 ms
10,536 KB
testcase_29 AC 33 ms
10,536 KB
testcase_30 AC 32 ms
10,536 KB
testcase_31 AC 33 ms
10,536 KB
testcase_32 AC 33 ms
10,536 KB
testcase_33 AC 34 ms
10,536 KB
testcase_34 AC 33 ms
10,536 KB
testcase_35 AC 32 ms
10,536 KB
testcase_36 AC 33 ms
10,536 KB
testcase_37 AC 32 ms
10,536 KB
testcase_38 AC 32 ms
10,536 KB
testcase_39 AC 32 ms
10,536 KB
testcase_40 AC 32 ms
10,536 KB
testcase_41 AC 34 ms
10,536 KB
testcase_42 AC 33 ms
10,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def prime_table(m=10**9):
    thres = int(m ** 0.5 + 1)
    flg = [0] * (thres + 1)
    i = 2
    while i * i <= thres:
        if not flg[i]:
            for j in range(i * i, thres + 1, i):
                flg[j] = 1
        i += 1
    
    res = []
    for i in range(2, thres + 1):
        if not flg[i]:
            res.append(i)
    return res
    
ps = prime_table()

def tetration(a, b, m):
    def totient(n):
        res = n
        for p in ps:
            if p * p > n:
                break
            if n % p == 0:
                res = res // p * (p - 1)
                while n % p == 0:
                    n //= p
        if n != 1:
            res = res // n * (n - 1)
        return res
    
    def mpow(a, p, m):
        res, flg = 1 % m, 1
        while p:
            if p & 1:
                res *= a
                if res >= m:
                    flg = 0
                    res %= m
            if p == 1:
                break
            a *= a
            if a >= m:
                flg = 0
                a %= m
            p >>= 1
        return res, flg
    
    def calc(a, b, m):
        if a == 0:
            return (b & 1) ^ 1, 1
        if a == 1:
            return 1, 1
        if m == 1:
            return 0, 0
        if b == 0:
            return 1, 1
        if b == 1:
            return a % m, int(a < m)
        phi_m = totient(m)
        pre, flg1 = calc(a, b - 1, phi_m)
        if flg1:
            res, flg2 = mpow(a % m, pre, m)
        else:
            res, flg2 = mpow(a % m, pre + phi_m, m)
        return res, flg1 & flg2
    
    return calc(a, b, m)[0] % m


a, n, m = map(int, input().split())
print(tetration(a, n, m))
0