{-# LANGUAGE BangPatterns #-} import qualified Control.Arrow as Arrow import Data.Bits (Bits (unsafeShiftL, unsafeShiftR), FiniteBits (countTrailingZeros)) import Data.Bool (bool) import qualified Data.ByteString.Char8 as BSC8 import qualified Data.List as List import qualified Data.Vector.Unboxed as VU import qualified Data.Vector.Fusion.Stream.Monadic as MS import qualified GHC.Integer.GMP.Internals as GMP ------------------------------------------------------------------------------- main :: IO () main = do n <- parse1 xs <- parseN n rep n (\i -> BSC8.putStrLn $ solve $ xs VU.! i) solve :: Int -> BSC8.ByteString solve n = BSC8.pack $ bool (show n ++ " 0") (show n ++ " 1") (isPrime n) ------------------------------------------------------------------------------- isPrime :: Int -> Bool isPrime k | k <= 3 = k == 2 || k == 3 | even k = False | otherwise = millerRabin k where millerRabin :: Int -> Bool millerRabin n | n < 2047 = loop [2] | n < 1373653 = loop [2,3] | n < 9080191 = loop [31,73] | n < 25326001 = loop [2,3,5] | n < 4759123141 = loop [2,7,61] | n < 1122004669633 = loop [2,13,23,1662803] | n < 2152302898747 = loop [2,3,5,7,11] | n < 3474749660383 = loop [2,3,5,7,11,13] | n < 341550071728321 = loop [2,3,5,7,11,13,17] | otherwise = loop [2,325,9375,28178,450775,9780504,1795265022] where !m = n - 1 !s = countTrailingZeros m !d = unsafeShiftR m s check1 :: Int -> Bool check1 a = powModInt a d n /= 1 {-# INLINE check1 #-} check2 :: Int -> Int -> Bool check2 a i = (powModInt a (d * (unsafeShiftL 1 i)) n) /= m {-# INLINE check2 #-} loop [] = True loop (a:as) | check1 a && allok = False | otherwise = loop as where allok = all (check2 a) [0..(s - 1)] powModInt :: Int -> Int -> Int -> Int powModInt a n mo = fromInteger $ GMP.powModInteger (fromIntegral a) (fromIntegral n) (fromIntegral mo) {-# INLINE powModInt #-} ------------------------------------------------------------------------------- type Parser a = BSC8.ByteString -> Maybe (a, BSC8.ByteString) parseInt :: Parser Int parseInt = fmap (Arrow.second BSC8.tail) . BSC8.readInt parse1 :: IO Int parse1 = readLn parseN :: Int -> IO (VU.Vector Int) parseN n = VU.replicateM n parse1 ------------------------------------------------------------------------------- rep :: (Monad m) => Int -> (Int -> m ()) -> m () rep n = flip MS.mapM_ (stream 0 n) {-# INLINE rep #-} stream :: (Monad m) => Int -> Int -> MS.Stream m Int stream !l !r = MS.Stream step l where step x | x < r = return $ MS.Yield x (x + 1) | otherwise = return MS.Done {-# INLINE [0] step #-} {-# INLINE [1] stream #-}