結果

問題 No.181 A↑↑N mod M
ユーザー (ΦωΦ)(ΦωΦ)
提出日時 2015-04-13 14:06:48
言語 Python2
(2.7.18)
結果
AC  
実行時間 11 ms / 5,000 ms
コード長 1,430 bytes
コンパイル時間 588 ms
コンパイル使用メモリ 7,152 KB
実行使用メモリ 6,216 KB
最終ジャッジ日時 2023-08-15 19:01:56
合計ジャッジ時間 2,546 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
5,980 KB
testcase_01 AC 10 ms
6,036 KB
testcase_02 AC 10 ms
6,180 KB
testcase_03 AC 11 ms
6,200 KB
testcase_04 AC 10 ms
6,172 KB
testcase_05 AC 10 ms
6,036 KB
testcase_06 AC 10 ms
6,028 KB
testcase_07 AC 9 ms
5,976 KB
testcase_08 AC 10 ms
5,948 KB
testcase_09 AC 10 ms
6,100 KB
testcase_10 AC 10 ms
6,124 KB
testcase_11 AC 10 ms
6,036 KB
testcase_12 AC 10 ms
5,980 KB
testcase_13 AC 10 ms
6,032 KB
testcase_14 AC 11 ms
6,124 KB
testcase_15 AC 11 ms
5,940 KB
testcase_16 AC 10 ms
5,992 KB
testcase_17 AC 10 ms
6,156 KB
testcase_18 AC 11 ms
6,060 KB
testcase_19 AC 10 ms
6,112 KB
testcase_20 AC 10 ms
6,040 KB
testcase_21 AC 11 ms
5,976 KB
testcase_22 AC 11 ms
6,136 KB
testcase_23 AC 11 ms
5,952 KB
testcase_24 AC 11 ms
6,024 KB
testcase_25 AC 11 ms
6,068 KB
testcase_26 AC 10 ms
5,968 KB
testcase_27 AC 10 ms
6,096 KB
testcase_28 AC 10 ms
5,936 KB
testcase_29 AC 10 ms
6,124 KB
testcase_30 AC 10 ms
6,088 KB
testcase_31 AC 9 ms
6,096 KB
testcase_32 AC 9 ms
6,024 KB
testcase_33 AC 10 ms
5,952 KB
testcase_34 AC 9 ms
5,948 KB
testcase_35 AC 10 ms
6,180 KB
testcase_36 AC 9 ms
6,144 KB
testcase_37 AC 9 ms
6,036 KB
testcase_38 AC 10 ms
6,072 KB
testcase_39 AC 10 ms
5,936 KB
testcase_40 AC 10 ms
6,168 KB
testcase_41 AC 9 ms
6,216 KB
testcase_42 AC 10 ms
6,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/python

def mod_pow(a, b, m):
    res = 1
    while b > 0:
        if b & 1:
            res = (res * a) % m
        a = (a * a) % m
        b >>= 1
    return res

M_MAX = 2010

from itertools import count
class Hyper4mod(object):
    def __init__(self, a, b, m):
        self.a = a
        self.b = b
        self.m = m

    def _cycle(self, a, m):
        memo = [-1] * M_MAX
        x = mod_pow(a, M_MAX, m)
        for i in count(1):
            j = memo[x]
            if j >= 0:
                return i - j
            memo[x] = i
            x = x * a % m

    def _nya(self, a, b, m):
        if m == 1:
            return 0
        if b == 0:
            return 1
        if b - 1 <= self.bmax:
            return mod_pow(a, self.arr[b-1], m)
        c = self._cycle(a, m)
        return mod_pow(a, M_MAX + (self._nya(a, b-1, c) - M_MAX) % c, m)

    def solve(self):
        if self.a == 1:
            return 1 % self.m
        self.arr = [1]
        for i in count(1):
            nuke = 0
            x = 1
            exp = self.arr[i-1]
            for j in xrange(exp):
                x *= self.a
                if x >= M_MAX:
                    nuke = 1
                    break
            if nuke:
                break
            self.arr.append(x)
        self.bmax = len(self.arr) - 1
        return self._nya(self.a, self.b, self.m)

print Hyper4mod(*map(int, raw_input().split())).solve()
0