結果

問題 No.1140 EXPotentiaLLL!
ユーザー gemmarogemmaro
提出日時 2020-07-31 22:07:24
言語 Haskell
(9.8.2)
結果
TLE  
実行時間 -
コード長 724 bytes
コンパイル時間 6,664 ms
コンパイル使用メモリ 164,348 KB
実行使用メモリ 20,504 KB
最終ジャッジ日時 2023-09-20 23:34:02
合計ジャッジ時間 14,085 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

import qualified Control.Monad as M

main :: IO ()
main = do
    t <- readLn
    _ <- solve t
    return ()

solve :: Int -> IO [()]
solve t =
    M.replicateM t solve'

solve' :: IO ()
solve' = do
    [a, p] <- map read . words <$> getLine
    print $ solve'' a p

solve'' :: Int -> Int -> Int
solve'' a p =
    if isPrime p
       then (a ^ fact p) `mod` p
       else -1

-- ref: https://gist.github.com/marcoscastro/a4c109bb6df608f14bf5
isPrime :: Int -> Bool
isPrime 1 = False
isPrime 2 = True
isPrime n
  | (length [x | x <- [2 .. n - 1], n `mod` x == 0]) > 0 = False
  | otherwise = True

fact :: Int -> Int
fact n = fact' n 1

fact' :: Int -> Int -> Int
fact' n x
  | n == 0 = x
  | otherwise = fact' (n - 1) (x * n)
0