結果

問題 No.181 A↑↑N mod M
ユーザー takuktakuk
提出日時 2015-04-06 12:07:46
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 651 bytes
コンパイル時間 145 ms
コンパイル使用メモリ 11,164 KB
実行使用メモリ 15,684 KB
最終ジャッジ日時 2023-09-17 06:11:56
合計ジャッジ時間 5,760 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
15,272 KB
testcase_01 RE -
testcase_02 RE -
testcase_03 AC 96 ms
15,100 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 AC 94 ms
15,344 KB
testcase_20 AC 94 ms
15,360 KB
testcase_21 AC 93 ms
15,164 KB
testcase_22 AC 94 ms
15,224 KB
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 AC 92 ms
15,160 KB
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

require 'prime'
def phi(n)
    ps = Array.new
    Prime.each(n) do |p|
        ps.push(p) if n % p == 0
    end
    ret = n
    ps.each do |p|
        ret *= (1.0 - (1.0/p))
    end
    return ret.round
end
def modpow(b,e,m)
    res = 1
    while e > 0
        if e & 1 == 1
            res = (res * b) % m
        end
        e >>= 1
        b = (b * b) % m
    end
    res
end
def rec(a,n,m)
    return 1 if n == 0
    return 0 if m == 1
    m2 = phi(m)
    b = rec(a,n-1,m2) + m2
    return modpow(a,b,m)
end
a, n, m = gets.split.map(&:to_i)
if n == 0
    p 1 % m
elsif n == 1
    p a % m
elsif n == 2
    p modpow(a,a,m)
else
    p rec(a,p,m)
end
0