結果

問題 No.181 A↑↑N mod M
ユーザー こまるこまる
提出日時 2020-10-24 04:42:27
言語 Haskell
(9.8.2)
結果
WA  
実行時間 -
コード長 2,052 bytes
コンパイル時間 10,541 ms
コンパイル使用メモリ 188,224 KB
実行使用メモリ 7,464 KB
最終ジャッジ日時 2023-09-28 20:07:16
合計ジャッジ時間 13,585 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,368 KB
testcase_01 AC 3 ms
7,320 KB
testcase_02 AC 3 ms
7,004 KB
testcase_03 AC 2 ms
6,928 KB
testcase_04 AC 3 ms
7,452 KB
testcase_05 AC 3 ms
7,280 KB
testcase_06 AC 3 ms
7,368 KB
testcase_07 AC 3 ms
7,340 KB
testcase_08 AC 3 ms
7,464 KB
testcase_09 AC 3 ms
7,376 KB
testcase_10 AC 3 ms
7,348 KB
testcase_11 AC 3 ms
7,332 KB
testcase_12 AC 3 ms
7,312 KB
testcase_13 AC 3 ms
7,348 KB
testcase_14 AC 3 ms
7,424 KB
testcase_15 AC 2 ms
7,324 KB
testcase_16 AC 3 ms
7,012 KB
testcase_17 AC 3 ms
7,328 KB
testcase_18 AC 3 ms
6,896 KB
testcase_19 AC 3 ms
6,884 KB
testcase_20 AC 3 ms
6,880 KB
testcase_21 AC 3 ms
6,948 KB
testcase_22 AC 2 ms
6,932 KB
testcase_23 AC 3 ms
7,280 KB
testcase_24 AC 3 ms
7,332 KB
testcase_25 AC 3 ms
7,284 KB
testcase_26 AC 3 ms
7,332 KB
testcase_27 AC 3 ms
7,376 KB
testcase_28 AC 3 ms
7,320 KB
testcase_29 AC 3 ms
7,324 KB
testcase_30 AC 3 ms
7,448 KB
testcase_31 AC 3 ms
7,312 KB
testcase_32 WA -
testcase_33 AC 3 ms
7,328 KB
testcase_34 AC 3 ms
7,360 KB
testcase_35 AC 3 ms
7,340 KB
testcase_36 AC 3 ms
7,384 KB
testcase_37 AC 3 ms
7,376 KB
testcase_38 AC 3 ms
7,308 KB
testcase_39 AC 3 ms
7,308 KB
testcase_40 AC 3 ms
7,384 KB
testcase_41 AC 3 ms
7,360 KB
testcase_42 AC 2 ms
7,384 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:27:37: warning: [GHC-68441] [-Wdeprecations]
    In the use of ‘powModInteger’
    (imported from GHC.Integer.GMP.Internals):
    Deprecated: "Use integerPowMod# instead"
   |
27 |     powModInt a b c = fromInteger $ GMP.powModInteger (fromIntegral a) (fromIntegral b) (fromIntegral c)
   |                                     ^^^^^^^^^^^^^^^^^
[2 of 2] Linking a.out

ソースコード

diff #

{-# LANGUAGE BangPatterns #-}

import           Control.Monad.Fix
import           Control.Monad.ST
import           Data.Bool
import qualified GHC.Integer.GMP.Internals   as GMP
import qualified Data.Vector.Unboxed         as VU
import qualified Data.Vector.Unboxed.Mutable as VUM

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
  | otherwise = powModInt (a `mod` m) (prev + phi) m
  where
    !phi = eulerPhi 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 #-}

eulerPhi :: Int -> Int
eulerPhi n = runST $ do
  xs <- VU.unsafeThaw $ VU.fromList [n, n, 2]
  ys <- loopFor xs
  p  <- VUM.unsafeRead ys 0
  q  <- VUM.unsafeRead ys 1
  if q > 1 then return (p - p `div` q) else return p
    where
      loopFor :: VUM.STVector s Int -> ST s (VUM.STVector s Int)
      loopFor xx = do
        idx <- VUM.unsafeRead xx 2
        if idx * idx > n then return xx else do
          xret <- VUM.unsafeRead xx 0
          xn   <- VUM.unsafeRead xx 1
          if xn `mod` idx /= 0
            then do
              VUM.unsafeWrite xx 2 (idx + 1)
              loopFor xx
            else do
              VUM.unsafeWrite xx 0 (xret - xret `div` idx)
              whileXS <- loopWhile xx
              VUM.unsafeWrite whileXS 2 (idx + 1)
              loopFor whileXS
                where
                  loopWhile :: VUM.STVector s Int -> ST s (VUM.STVector s Int)
                  loopWhile ks = do
                    whileN <- VUM.unsafeRead ks 1
                    whileI <- VUM.unsafeRead ks 2
                    if whileN `mod` whileI /= 0 then return ks else do
                      VUM.unsafeWrite ks 1 (whileN `div` whileI)
                      loopWhile ks
0