結果

問題 No.403 2^2^2
ユーザー はむ吉🐹
提出日時 2016-07-23 20:49:18
言語 Haskell
(9.10.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 693 bytes
コンパイル時間 5,263 ms
コンパイル使用メモリ 171,648 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-06 15:36:39
合計ジャッジ時間 6,188 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 #

{-# LANGUAGE BangPatterns #-}

import Control.Applicative ((<$>))
import Data.Int (Int64)
import Data.List.Split (splitOn)

modexp :: Integral a => a -> a -> a -> a
modexp b ex m
    | ex == 0   = 1
    | even ex   = flip mod m $ square $ flip mod m $ modexp b (div ex 2) m
    | otherwise = flip mod m $ mod b m * mod (modexp b (ex - 1) m) m
    where square x = x * x

p :: Integral a => a
p = 10 ^ 9 + 7

main :: IO ()
main = do
    ![a, b, c] <- fmap read . splitOn "^" <$> getLine :: IO [Int64]
    let !m = mod a p
    let !x = if m == 0 then 0 else modexp (modexp a b p) c p
    let !y = if m == 0 then 0 else modexp a (modexp b c $ p - 1) p
    putStrLn . unwords . fmap show $ [x, y]
0