結果
| 問題 |
No.181 A↑↑N mod M
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-12-28 23:03:40 |
| 言語 | Haskell (9.10.1) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 1,180 bytes |
| コンパイル時間 | 6,185 ms |
| コンパイル使用メモリ | 170,624 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-12-15 08:08:39 |
| 合計ジャッジ時間 | 2,982 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 6 |
| other | AC * 37 |
コンパイルメッセージ
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
ソースコード
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 - 1) < 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