結果

問題 No.181 A↑↑N mod M
ユーザー pekempeypekempey
提出日時 2016-12-28 22:59:55
言語 Haskell
(9.8.2)
結果
WA  
実行時間 -
コード長 1,174 bytes
コンパイル時間 5,322 ms
コンパイル使用メモリ 171,776 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-09 00:10:42
合計ジャッジ時間 6,522 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 1 ms
5,376 KB
testcase_39 AC 1 ms
5,376 KB
testcase_40 AC 1 ms
5,376 KB
testcase_41 AC 1 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

import Control.Applicative
import Data.List

modpow :: Integer -> Integer -> Integer -> Integer
modpow a b m
    | b == 0 = 1
    | even b = modpow (a * a `mod` m) (b `quot` 2) m
    | otherwise = a * modpow a (b - 1) m `mod` m

tetration :: Integer -> Integer -> Integer
tetration a n
    | a == 1 || n == 0 = 1
    | n == 1 = a
    | n == 2 && a == 2 = 4
    | n == 2 && a == 3 = 27
    | n == 2 && a == 4 = 256
    | n == 3 && a == 2 = 16
    | otherwise = 1234567

factors :: Integer -> [Integer]
factors n = f n 2
    where
        f :: Integer -> Integer -> [Integer]
        f n i
            | i * i > n = if n /= 1 then [n] else []
            | n `mod` i == 0 = i : f (n `quot` i) i
            | otherwise = f n (i + 1)

phi :: Integer -> Integer
phi n = foldr (\p acc -> (acc `quot` p) * (p - 1)) n $ nub $ factors n

modtet :: Integer -> Integer -> Integer -> Integer
modtet a n m
    | m == 1 = 0
    | n == 0 = 1
    | tetration a n < phi m =
        modpow a (modtet a (n - 1) (phi m)) m
    | otherwise =
        modpow a (modtet a (n - 1) (phi m) + phi m) m

main = do
    [a, n, m] <- map read . words <$> getLine :: IO [Integer]
    print $ modtet a n m
0