結果

問題 No.8030 ミラー・ラビン素数判定法のテスト
ユーザー こまるこまる
提出日時 2020-10-29 15:36:29
言語 Haskell
(9.8.2)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 12,709 bytes
コンパイル時間 534 ms
コンパイル使用メモリ 155,484 KB
最終ジャッジ日時 2024-11-14 23:53:26
合計ジャッジ時間 968 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )

Main.hs:19:1: error: [GHC-87110]
    Could not load module ‘Data.Time.Clock.POSIX’.
    It is a member of the hidden package ‘time-1.12.2’.
    Use -v to see a list of the files searched for.
   |
19 | import           Data.Time.Clock.POSIX
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Main.hs:27:1: error: [GHC-87110]
    Could not load module ‘GHC.Integer.GMP.Internals’.
    It is a member of the hidden package ‘integer-gmp-1.1’.
    Use -v to see a list of the files searched for.
   |
27 | import qualified GHC.Integer.GMP.Internals         as GMP
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ソースコード

diff #

{-# LANGUAGE BangPatterns              #-}
{-# LANGUAGE DefaultSignatures         #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleInstances         #-}
{-# LANGUAGE MagicHash                 #-}
{-# LANGUAGE MonomorphismRestriction   #-}
{-# LANGUAGE OverloadedStrings         #-}
{-# LANGUAGE TypeApplications          #-}
{-# LANGUAGE UndecidableInstances      #-}

import           Control.Monad.Fix
import           Control.Monad.State
import           Data.Bits
import           Data.Bool
import           Data.Char
import           Data.Coerce
import           Data.Int
import           Data.Maybe
import           Data.Time.Clock.POSIX
import           Data.Word
import           GHC.Exts
import           System.IO
import           Unsafe.Coerce
import qualified Data.ByteString                   as BS
import qualified Data.ByteString.Builder           as BSB
import qualified Data.ByteString.Char8             as BSC8
import qualified GHC.Integer.GMP.Internals         as GMP
import qualified Data.Vector.Fusion.Stream.Monadic as VFSM
import qualified Data.Vector.Generic               as VG
import qualified Data.Vector.Unboxed               as VU
import qualified Data.Vector.Unboxed.Mutable       as VUM

main :: IO ()
main = do
  n  <- readLn :: IO Int
  xs <- parseN1 n
  rep n $ \i -> do
    let x = xs VU.! i
    mr <- millerRabin x
    putBuilder $ BSB.intDec x <> (bool " 0\n" " 1\n" mr)

millerRabin :: Int -> IO Bool
millerRabin n
  | n <= 3 = return $ n == 2 || n == 3
  | even n = return False
  | otherwise = do
    flip fix 4 $ \loop !i -> do
      rng <- newRNG
      a   <- randomR rng (1, n - 1)
      let allok = all (\r -> (powModInt a ((1 .<<. r) * d) n) /= m) [0..(s - 1)]
      if (powModInt a d n /= 1) && allok
        then return False
        else if i < 0
          then return True
          else loop (i - 1)
  where
    !m = n - 1
    !s = countTrailingZeros m
    !d = m .>>. s

type RNG = VUM.IOVector Word

getSeed :: IO Word
getSeed = do
  t <- getPOSIXTime
  let ret = floor $ t * 1e9 :: Word
  if ret == 0
    then getSeed
    else return ret

splitMix64 :: Word -> (Word, Word)
splitMix64 state = case state + 0x9e3779b97f4a7c15 of
  z0 -> case (z0 .^. z0 .>>. 30) * 0xbf58476d1ce4e5b9 of
    z1 -> case (z1 .^. z1 .>>. 27) * 0x94d049bb133111eb of
      z2 -> case z2 .^. z2 .>>. 31 of
        z3 -> (z0, z3)

newRNG :: IO RNG
newRNG = newRNGWithSeed =<< getSeed

newRNGWithSeed :: Word -> IO RNG
newRNGWithSeed seed = do
  let (s0, seed') = splitMix64 seed
  let (s1, _)     = splitMix64 seed'
  rng <- VUM.unsafeNew 2
  VUM.unsafeWrite rng 0 s0
  VUM.unsafeWrite rng 1 s1
  return rng

nextWord :: RNG -> IO Word
nextWord rng = do
  !s0 <-            VUM.unsafeRead rng 0
  !s1 <- xor s0 <$> VUM.unsafeRead rng 1
  VUM.unsafeWrite rng 0 $ (s0 .<<. 24  .|.  s0 .>>. 40) .^. s1 .^. (s1 .<<. 16)
  VUM.unsafeWrite rng 1 $ (s1 .<<. 37) .|. (s1 .>>. 27)
  let s05 = s0 * 5
  return $! (s05 .<<. 7 .|. s05 .>>. 57) * 9

nextInt :: RNG -> IO Int
nextInt rng = unsafeCoerce @Word @Int <$> nextWord rng

nextDouble :: RNG -> IO Double
nextDouble rng = do
  t <- nextWord rng
  let x = 0x3ff .<<. 52 .|. t .>>. 12
  return $! unsafeCoerce @Word @Double x - 1.0

nextGauss :: RNG -> Double -> Double -> IO Double
nextGauss rng mu sigma = do
  x <- nextDouble rng
  y <- nextDouble rng
  let z = sqrt (-2.0 * log x) * cos (2.0 * pi * y)
  return $! sigma * z + mu

withRNG :: (RNG -> IO a) -> IO a
withRNG f = do
  seed <- getSeed
  withRNGWithSeed seed f

withRNGWithSeed :: Word -> (RNG -> IO a) -> IO a
withRNGWithSeed seed f = newRNGWithSeed seed >>= f

shuffleM :: VUM.Unbox a => RNG -> VUM.IOVector a -> IO ()
shuffleM rng mvec = do
  rev (VUM.length mvec) $ \i -> do
    j <- nextWord rng
    VUM.unsafeSwap mvec i (unsafeCoerce $ rem j (unsafeCoerce i + 1))

shuffle :: VU.Unbox a => VU.Vector a -> IO (VU.Vector a)
shuffle vec = withRNG $ \rng -> do
  mv <- VU.unsafeThaw vec
  shuffleM rng mv
  VU.unsafeFreeze mv

randomR :: RNG -> (Int, Int) -> IO Int
randomR rng (l, r) = flip mod (r - l + 1) <$> nextInt rng

infixl 8 .<<., .>>., .>>>.
infixl 6 .^.

(.<<.) :: Bits b => b -> Int -> b
(.<<.) = unsafeShiftL
{-# INLINE (.<<.) #-}

(.>>.) :: Bits b => b -> Int -> b
(.>>.) = unsafeShiftR
{-# INLINE (.>>.) #-}

(.>>>.) :: Int -> Int -> Int
(I# x#) .>>>. (I# y#) = I# (uncheckedIShiftRL# x# y#)
{-# INLINE (.>>>.) #-}

(.^.) :: Bits b => b -> b -> b
(.^.) = xor
{-# INLINE (.^.) #-}

stream :: Monad m => Int -> Int -> VFSM.Stream m Int
stream !l !r = VFSM.Stream step l
  where
    step x
      | x < r     = return $ VFSM.Yield x (x + 1)
      | otherwise = return $ VFSM.Done
    {-# INLINE [0] step #-}
{-# INLINE [1] stream #-}

rep :: Monad m => Int -> (Int -> m ()) -> m ()
rep n = flip VFSM.mapM_ (stream 0 n)
{-# INLINE rep #-}

streamR :: Monad m => Int -> Int -> VFSM.Stream m Int
streamR !l !r = VFSM.Stream step (r - 1)
  where
    step x
      | x >= l    = return $ VFSM.Yield x (x - 1)
      | otherwise = return $ VFSM.Done
    {-# INLINE [0] step #-}
{-# INLINE [1] streamR #-}

rev :: Monad m => Int -> (Int -> m ()) -> m ()
rev n = flip VFSM.mapM_ (streamR 0 n)
{-# INLINE rev #-}

fi :: Int -> Integer
fi = fromIntegral
{-# INLINE fi #-}

fI :: Integer -> Int
fI = fromInteger
{-# INLINE fI #-}

powModInt :: Int -> Int -> Int -> Int
powModInt a b m = fI $ GMP.powModInteger (fi a) (fi b) (fi m)


type CParser a = StateT BSC8.ByteString Maybe a
runCParser :: CParser a -> BSC8.ByteString -> Maybe (a, BSC8.ByteString)
runCParser = runStateT
{-# INLINE runCParser #-}
int :: CParser Int
int = coerce $ BSC8.readInt . BSC8.dropWhile isSpace
{-# INLINE int #-}
int1 :: CParser Int
int1 = fmap (subtract 1) int
{-# INLINE int1 #-}
char :: CParser Char
char = coerce BSC8.uncons
{-# INLINE char #-}
byte :: CParser Word8
byte = coerce BS.uncons
{-# INLINE byte #-}
skipSpaces :: CParser ()
skipSpaces = modify' (BSC8.dropWhile isSpace)
{-# INLINE skipSpaces #-}
seqInput :: Int -> IO (VU.Vector Int)
seqInput n = VU.unfoldrN n (runCParser int) <$> BSC8.getLine
{-# INLINE seqInput #-}
parseN1 :: Int -> IO (VU.Vector Int)
parseN1 n = VU.unfoldrN n (runCParser int) <$> BSC8.getContents
{-# INLINE parseN1 #-}
parseN2 :: Int -> IO (VU.Vector (Int, Int))
parseN2 n = VU.unfoldrN n (runCParser $ (,) <$> int <*> int) <$> BSC8.getContents
{-# INLINE parseN2 #-}
parseN3 :: Int -> IO (VU.Vector (Int, Int, Int))
parseN3 n = VU.unfoldrN n (runCParser $ (,,) <$> int <*> int <*> int) <$> BSC8.getContents
{-# INLINE parseN3 #-}
parseN4 :: Int -> IO (VU.Vector (Int, Int, Int, Int))
parseN4 n = VU.unfoldrN n (runCParser $ (,,,) <$> int <*> int <*> int <*> int) <$> BSC8.getContents
{-# INLINE parseN4 #-}
parseN5 :: Int -> IO (VU.Vector (Int, Int, Int, Int, Int))
parseN5 n = VU.unfoldrN n (runCParser $ (,,,,) <$> int <*> int <*> int <*> int <*> int) <$> BSC8.getContents
{-# INLINE parseN5 #-}
parseANBN :: Int -> IO (VU.Vector Int, VU.Vector Int)
parseANBN n = VU.unzip . VU.unfoldrN n (runCParser $ (,) <$> int <*> int) <$> BSC8.getContents
{-# INLINE parseANBN #-}
parseANBNCN :: Int -> IO (VU.Vector Int, VU.Vector Int, VU.Vector Int)
parseANBNCN n = VU.unzip3 . VU.unfoldrN n (runCParser $ (,,) <$> int <*> int <*> int) <$> BSC8.getContents
{-# INLINE parseANBNCN #-}

type Query3 = (Int, Int, Int)
query3Parser :: CParser Query3
query3Parser = do
  skipSpaces
  t <- char
  case t of
    '0' -> (,,) 0 <$> int <*> int
    _   -> (,,) 1 <$> int <*> pure 0
parseQ3 :: Int -> IO (VU.Vector Query3)
parseQ3 n = VU.unfoldrN n (runCParser query3Parser) <$> BSC8.getContents
{-# INLINE parseQ3 #-}

type Query5 = (Int, Int, Int, Int, Int)
query5Parser :: CParser Query5
query5Parser = do
  skipSpaces
  t <- char
  case t of
    '0' -> (,,,,) 0 <$> int <*> int <*> int    <*> int
    _   -> (,,,,) 1 <$> int <*> int <*> pure 0 <*> pure 0
parseQ5 :: Int -> IO (VU.Vector Query5)
parseQ5 n = VU.unfoldrN n (runCParser query5Parser) <$> BSC8.getContents
{-# INLINE parseQ5 #-}

readInt :: BSC8.ByteString -> Int
readInt = fst . fromJust . BSC8.readInt
{-# INLINE readInt #-}
getInt :: IO Int
getInt = readInt <$> BSC8.getLine
{-# INLINE getInt #-}
readIntList :: BSC8.ByteString -> [Int]
readIntList = map readInt . BSC8.words
{-# INLINE readIntList #-}
getIntList :: IO [Int]
getIntList = readIntList <$> BS.getLine
{-# INLINE getIntList #-}
readInteger :: BSC8.ByteString -> Integer
readInteger = fst . fromJust . BSC8.readInteger
{-# INLINE readInteger #-}
getInteger :: IO Integer
getInteger = readInteger <$> BSC8.getLine
{-# INLINE getInteger #-}
readIntegerList :: BSC8.ByteString -> [Integer]
readIntegerList = map readInteger . BSC8.words
{-# INLINE readIntegerList #-}
getIntegerList :: IO [Integer]
getIntegerList = readIntegerList <$> BS.getLine
{-# INLINE getIntegerList #-}

class ShowAsBuilder a where
  showAsBuilder :: a -> BSB.Builder
  default showAsBuilder :: (Show a) => a -> BSB.Builder
  showAsBuilder = BSB.string8 . show

instance ShowAsBuilder Int where
  showAsBuilder = BSB.intDec
  {-# INLINE showAsBuilder #-}
instance ShowAsBuilder Int8 where
  showAsBuilder = BSB.int8Dec
  {-# INLINE showAsBuilder #-}
instance ShowAsBuilder Int16 where
  showAsBuilder = BSB.int16Dec
  {-# INLINE showAsBuilder #-}
instance ShowAsBuilder Int32 where
  showAsBuilder = BSB.int32Dec
  {-# INLINE showAsBuilder #-}
instance ShowAsBuilder Int64 where
  showAsBuilder = BSB.int64Dec
  {-# INLINE showAsBuilder #-}
instance ShowAsBuilder Word8 where
  showAsBuilder = BSB.word8Dec
  {-# INLINE showAsBuilder #-}
instance ShowAsBuilder Word16 where
  showAsBuilder = BSB.word16Dec
  {-# INLINE showAsBuilder #-}
instance ShowAsBuilder Word32 where
  showAsBuilder = BSB.word32Dec
  {-# INLINE showAsBuilder #-}
instance ShowAsBuilder Word64 where
  showAsBuilder = BSB.word64Dec
  {-# INLINE showAsBuilder #-}
instance ShowAsBuilder Integer where
  showAsBuilder = BSB.integerDec
  {-# INLINE showAsBuilder #-}
instance ShowAsBuilder Float where
  showAsBuilder = BSB.floatDec
  {-# INLINE showAsBuilder #-}
instance ShowAsBuilder Double where
  showAsBuilder = BSB.doubleDec
  {-# INLINE showAsBuilder #-}

instance (ShowAsBuilder a, VG.Vector v a) => ShowAsBuilder (v a) where
  showAsBuilder = v2BSpcSep

putBuilder :: BSB.Builder -> IO ()
putBuilder = BSB.hPutBuilder stdout
{-# INLINE putBuilder #-}

printVecInLines :: (VG.Vector v a, ShowAsBuilder a) => v a -> IO ()
printVecInLines = putBuilder . v2BLines
{-# INLINE printVecInLines #-}

printVecInSpcSepLn :: (VG.Vector v a, ShowAsBuilder a) => v a -> IO ()
printVecInSpcSepLn = putBuilder . v2BSpcSepLn
{-# INLINE printVecInSpcSepLn #-}

v2BSpcSepLn :: (VG.Vector v a, ShowAsBuilder a) => v a -> BSB.Builder
v2BSpcSepLn = v2BSpcSepLnWith showAsBuilder
{-# INLINE v2BSpcSepLn #-}

v2BSpcSep :: (VG.Vector v a, ShowAsBuilder a) => v a -> BSB.Builder
v2BSpcSep = v2BSpcSepWith showAsBuilder
{-# INLINE v2BSpcSep #-}

v2BConcat:: (VG.Vector v a, ShowAsBuilder a) => v a -> BSB.Builder
v2BConcat = v2BConcatWith showAsBuilder
{-# INLINE v2BConcat #-}

v2BLines:: (VG.Vector v a, ShowAsBuilder a) => v a -> BSB.Builder
v2BLines = v2BLinesWith showAsBuilder
{-# INLINE v2BLines #-}

v2BSpcSepLnWith :: VG.Vector v a => (a -> BSB.Builder) -> v a -> BSB.Builder
v2BSpcSepLnWith = v2BSpcSepPostfWith "\n"
{-# INLINE v2BSpcSepLnWith #-}

v2BSpcSepWith :: VG.Vector v a => (a -> BSB.Builder) -> v a -> BSB.Builder
v2BSpcSepWith = v2BSpcSepPostfWith ""
{-# INLINE v2BSpcSepWith #-}

v2BConcatWith :: VG.Vector v a => (a -> BSB.Builder) -> v a -> BSB.Builder
v2BConcatWith showFct = VG.foldr ((<>) . showFct) mempty
{-# INLINE v2BConcatWith #-}

v2BLinesWith :: VG.Vector v a => (a -> BSB.Builder) -> v a -> BSB.Builder
v2BLinesWith showFct = VG.foldr (\a -> (showFct a <>) . (BSB.char7 '\n' <>)) mempty
{-# INLINE v2BLinesWith #-}

v2BSpcSepPostf :: (VG.Vector v a, ShowAsBuilder a) => BS.ByteString -> v a -> BSB.Builder
v2BSpcSepPostf = (`v2BSpcSepPostfWith` showAsBuilder)
{-# INLINE v2BSpcSepPostf #-}

v2BSpcSepPostfWith :: VG.Vector v a => BS.ByteString -> (a -> BSB.Builder) -> v a -> BSB.Builder
v2BSpcSepPostfWith = vecToBuilder "" " "
{-# INLINE v2BSpcSepPostfWith #-}

vecToBuilder :: VG.Vector v a => BS.ByteString -> BS.ByteString -> BS.ByteString -> (a -> BSB.Builder) -> v a -> BSB.Builder
vecToBuilder !prefix !separator !postfix = vecToBuilder_ (BSB.byteString prefix) (BSB.byteString separator) (BSB.byteString postfix)
{-# INLINE vecToBuilder #-}

vecToBuilder_ :: VG.Vector v a => BSB.Builder -> BSB.Builder -> BSB.Builder -> (a -> BSB.Builder) -> v a -> BSB.Builder
vecToBuilder_ !prefix !separator !postfix showFct vec = prefix <> VG.foldr (\a rest !prefx -> prefx <> (showFct a <> rest separator)) (const postfix) vec mempty
{-# INLINE vecToBuilder_ #-}
0