結果

問題 No.181 A↑↑N mod M
ユーザー こまるこまる
提出日時 2020-10-24 05:19:09
言語 Haskell
(9.6.2)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,265 bytes
コンパイル時間 1,222 ms
コンパイル使用メモリ 162,964 KB
実行使用メモリ 7,500 KB
最終ジャッジ日時 2023-09-28 20:07:38
合計ジャッジ時間 2,993 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,404 KB
testcase_01 AC 3 ms
7,400 KB
testcase_02 AC 2 ms
7,000 KB
testcase_03 AC 2 ms
7,032 KB
testcase_04 AC 3 ms
7,392 KB
testcase_05 AC 3 ms
7,436 KB
testcase_06 AC 2 ms
6,908 KB
testcase_07 AC 3 ms
6,984 KB
testcase_08 AC 3 ms
7,480 KB
testcase_09 AC 3 ms
7,460 KB
testcase_10 AC 3 ms
7,492 KB
testcase_11 AC 3 ms
7,472 KB
testcase_12 AC 3 ms
7,436 KB
testcase_13 AC 3 ms
7,440 KB
testcase_14 AC 3 ms
7,444 KB
testcase_15 AC 3 ms
7,444 KB
testcase_16 AC 3 ms
7,124 KB
testcase_17 AC 3 ms
7,464 KB
testcase_18 AC 3 ms
7,112 KB
testcase_19 AC 3 ms
6,968 KB
testcase_20 AC 3 ms
7,016 KB
testcase_21 AC 2 ms
6,892 KB
testcase_22 AC 3 ms
6,972 KB
testcase_23 AC 3 ms
7,448 KB
testcase_24 AC 3 ms
7,432 KB
testcase_25 AC 3 ms
7,396 KB
testcase_26 AC 3 ms
7,492 KB
testcase_27 AC 3 ms
7,464 KB
testcase_28 AC 3 ms
7,456 KB
testcase_29 AC 3 ms
6,968 KB
testcase_30 AC 3 ms
7,412 KB
testcase_31 AC 3 ms
7,432 KB
testcase_32 AC 3 ms
6,996 KB
testcase_33 AC 3 ms
7,424 KB
testcase_34 AC 3 ms
7,468 KB
testcase_35 AC 3 ms
7,500 KB
testcase_36 AC 3 ms
7,492 KB
testcase_37 AC 3 ms
7,420 KB
testcase_38 AC 3 ms
7,428 KB
testcase_39 AC 3 ms
7,424 KB
testcase_40 AC 3 ms
7,436 KB
testcase_41 AC 3 ms
7,428 KB
testcase_42 AC 3 ms
7,460 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )

Main.hs:26:33: warning: [GHC-68441] [-Wdeprecations]
    In the use of ‘powModInteger’
    (imported from GHC.Integer.GMP.Internals):
    Deprecated: "Use integerPowMod# instead"
   |
26 | powModInt a b c = fromInteger $ GMP.powModInteger (fromIntegral a) (fromIntegral b) (fromIntegral c)
   |                                 ^^^^^^^^^^^^^^^^^
[2 of 2] Linking a.out

ソースコード

diff #

{-# LANGUAGE BangPatterns #-}

import           Data.Bool
import           Data.List
import qualified GHC.Integer.GMP.Internals         as GMP

main :: IO ()
main = do
  [a, b, n] <- map read . words <$> getLine
  print $ tetration a b n

tetration :: Int -> Int -> Int -> Int
tetration a b m
  | m == 1 = 0
  | a == 0 = bool 0 1 (even b)
  | b == 0 = 1
  | b == 1 = a `mod` m
  | b == 2 = powModInt (a `mod` m) a m
  | a == 2 && b == 3 = 16 `mod` m
  | otherwise = powModInt (a `mod` m) (prev + phi) m
  where
    !phi = totient m
    prev = tetration a (b - 1) phi

powModInt :: Int -> Int -> Int -> Int
powModInt a b c = fromInteger $ GMP.powModInteger (fromIntegral a) (fromIntegral b) (fromIntegral c)
{-# INLINE powModInt #-}

totient :: Int -> Int
totient n = n `quot` product ps * (product $ map (subtract 1) ps)
  where ps = map head . group $ primeFactors n

smallPrimes :: [Int]
smallPrimes = 2 : [ n | n <- [3, 5 .. 46337], all ((0 <) . rem n) $ takeWhile (\x -> x * x <= n) smallPrimes]

primeFactors :: Int -> [Int]
primeFactors n | n < 2 = []
primeFactors n = go n smallPrimes
  where
    go !n pps@(p:ps)
      | n < p * p = [n]
      | r > 0     = go n ps
      | otherwise = p : go q pps
      where
        (q, r) = quotRem n p
    go n [] = [n]
0