import Control.Monad (forM_,when)
import qualified Data.ByteString.Char8 as BS
import Data.Int
import Data.Word

import Data.Array.IArray
import Data.Array.MArray
import qualified Data.Array.Unboxed as U
import Data.Array.ST (runSTUArray)
import Debug.Trace

sqr :: Double -> Double
sqr = sqrt

isqrt :: Integral a => a -> Int
isqrt n = floor $ sqrt $ (fromIntegral n :: Double)

isSquare :: Int64 -> Maybe Int
isSquare n = if r'*r' == n then Just r else Nothing
  where r = isqrt n
        r' = fromIntegral r

-- create a prime sieve array
mkSieve :: Int -> U.UArray Int Word8
mkSieve n = runSTUArray $ do
  arr <- newArray (0,n) 0
  writeArray arr 1 1
  let end = isqrt n
  forM_ [2..end] $ \a -> do
    x <- readArray arr a
    when (x == 0) $ do
      forM_ [a*a,a*a+a..n] $ \b -> do
        writeArray arr b 1
  return arr

isprime :: Int -> Bool
isprime n | n <= 11 = n `elem` [2,3,5,7,11]
isprime n | n `rem` 2 == 0 = False
isprime n = all (\m -> n `rem` m /= 0) [3,5..end]
  where end = isqrt n

wheel2357 :: [Int]
wheel2357 = 2:4:2:4:6:2:6:4:2:4:6:6:2:6:4:2:6:4:6:8:4:2:4:2:4:8:6:4:6:2:4:6:2:6:6:4:2:4:6:2:6:4:2:4:2:10:2:10:wheel2357
spin (x:xs) n = n : spin xs (n + x)

wheel' = 2 : 3 : 5 : 7 : spin wheel2357 11

isprime2 :: Int -> Bool
isprime2 n | n <= 11 = n `elem` [2,3,5,7,11]
isprime2 n = all (\m -> n `rem` m /= 0) $ takeWhile (<= end) wheel'
  where end = isqrt n

tprime :: (Int -> Bool) -> Int64 -> Bool
tprime ptest 1 = False
tprime ptest n =
  case isSquare n of
    Nothing -> False
    Just r  -> ptest r

(-->) = flip fmap

readInt :: BS.ByteString -> Integer
readInt s = case BS.readInteger s of
              Nothing    -> error "bad integer"
              Just (n,_) -> n

main = do
  n <- getLine --> read
  let _ = n :: Int
  nums <- BS.getLine --> BS.words --> map (fromIntegral . readInt)
  let sieve = mkSieve 1000000
  let ptest k = sieve ! k == 0
  forM_ nums $ \n -> do
    let b = tprime ptest n
    putStrLn $ if b then "YES" else "NO"

import Control.Applicative
import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as C
import Data.Maybe
import Data.Char

squ x =  x==y*y && isprime y
	where y = ceiling $ sqrt $ fromIntegral x

primes = [2,3,5,7,11] ++ (filter isprime [13,15..] )
isprime 1 = False
isprime 2 = True
isprime x = all (>0) [mod x y| y<- takeWhile (<=(ceiling (sqrt (fromIntegral x)))) primes]

main= do
	n  <-  getLine
	s<- map (fst.fromJust.C.readInteger). C.words <$> C.getLine   ::IO [Integer]
	putStrLn $ intercalate "\n" $ map (\z-> if squ z   then "YES" else "NO") s

import Data.Int (Int64)
import Data.Set (Set, fromList, member)

primes :: [Int64]
primes = 2 : sieve primes [3..]
  where
    sieve (p:ps) xs =
      let (h,t) = span (< p*p) xs
      in  h ++ sieve ps (filter ((/=0).(`mod`p)) t)

primesUpTo :: Int64 -> Set Int64
primesUpTo n = fromList $ takeWhile (<=n) primes

process :: [Int64] -> [Bool]
process xs = map f xs
    where
        f x = if x_sqrt^2 == x
                then x_sqrt `member` ps
                else False
            where x_sqrt = floor.sqrt.fromIntegral $ x
        ps = primesUpTo.floor.sqrt.fromIntegral $ maximum xs

readInt :: String -> Int64
readInt = read

printBool :: Bool -> IO ()
printBool b = do
    putStrLn $ ["NO","YES"] !! fromEnum b

main :: IO ()
main = do
    n <- fmap readInt getLine
    xs <- fmap (map readInt.words) getLine
    mapM_ printBool $ process xs

import Data.List
import Data.Set hiding (map)
import Data.Int

ordminus :: Ord a => [a] -> [a] -> [a]
ordminus x [] = x
ordminus [] y = y
ordminus (x:xs) (y:ys) | x < y  = x : ordminus xs (y:ys)
                       | x == y = ordminus xs ys
                       | x > y  = ordminus (x:xs) ys

ordunion :: Ord a => [a] -> [a] -> [a]
ordunion x [] = x
ordunion [] y = y
ordunion (x:xs) (y:ys) |  x < y = x : ordunion xs (y:ys)
                       |  y < x = y : ordunion (x:xs) ys
                       | x == y = x : ordunion xs ys

foldi            :: (a -> a -> a) -> a -> [a] -> a
foldi f z []     = z
foldi f z (x:xs) = f x (foldi f z (pairs f xs))

pairs            :: (a -> a -> a) -> [a] -> [a]
pairs f (x:y:t)  = f x y : pairs f t
pairs f t        = t

primes :: [Int64]
primes = 2 : 3 : ([5,7..] `ordminus`
                     foldi (\(x:xs) -> (x:) . ordunion xs) []
                          [[p*p, p*p+2*p..] | p <- tail primes])

setP = fromList (take 80000 primes)

isT :: Int64 -> Bool
isT v = let
  sqr = floor $ sqrt $ fromIntegral v
  in sqr*sqr==v && member (fromIntegral sqr) setP

yn :: Bool -> String
yn True = "YES"
yn False = "NO"

main = do
  n <- getLine
  xs <- getLine
  putStr $ unlines $ map (yn.isT.read) $ take (read n) (words xs)

{-# LANGUAGE FlexibleContexts, ScopedTypeVariables, BangPatterns #-}
{-# OPTIONS_GHC -O2 #-}

import Control.Applicative
import Control.Monad hiding ((<$!>))
import Control.Monad.ST.Safe
import Control.Monad.State hiding ((<$!>))
-- import Data.Array
import Data.Array.IArray
import Data.Array.IO.Safe
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Array.Unboxed
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Builder as BB
import Data.Char
import qualified Data.Foldable as F
import Data.Function
import Data.Graph
import Data.Int
import Data.List
import Data.List.Split
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.Ratio
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
-- import Data.Tree
import Data.Tuple
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
-- import Debug.Trace
import System.IO

-- getInts = fmap (map read . words) getLine
getInts = unfoldr (B.readInteger . B.dropWhile isSpace) <$> B.getLine

main = do
  getLine
  xs <- map fromIntegral <$> getInts :: IO [Int64]

  let
    primes = [a | a <- [2..10^3], all ((/= 0) . mod a) [2..a-1]] :: [Int64]

    check 1 = False
    check x = isSqrt && isPrime
      where
        y = truncate $ sqrt $ fromIntegral x
        isSqrt = y*y == x
        isPrime = all (\p -> y `mod` p /= 0) $ filter (\p -> p*p <= y) primes

  putStrLn $ unlines $ map ((\x -> if x then "YES" else "NO") . check) xs

{-# OPTIONS_GHC -O #-}
{-# LANGUAGE BangPatterns #-}
import Data.Int
import Data.Array.Unboxed
import Control.Monad.ST
import Control.Monad
import Data.Set hiding (map)
import qualified Data.Map as M

main = do
    n <- fmap read getLine
    xs <- fmap (take n . map read . words) getLine :: IO [Int64]
    putStr.unlines $ map f $ go xs (M.singleton 1 False) []

go [] _ acc = reverse acc
go (x:xs) mem acc =
    case M.lookup x mem of
        Just v  -> go xs mem $ v : acc
        Nothing -> (go xs $ M.insert x b mem) $ b : acc
            where b = isinset x

isinset x = x `member` primesSqr
f True = "YES";f False = "NO"

primesSqr = fromList $ map (^2) primesList
primesList = primesToA.floor.(+1).sqrt $ 1e12
primesToA m = sieve 3 (array (3,m) [(i,odd i) | i<-[3..m]]
                        :: UArray Int64 Bool)
  where
    sieve p a
      | p*p > m   = 2 : [i | (i,True) <- assocs a]
      | a!p       = sieve (p+2) $ a//[(i,False) | i <- [p*p, p*p+2*p..m]]
      | otherwise = sieve (p+2) a

import Data.Int
import Data.Char
import qualified Data.List as L
import qualified Data.Array as A

solve :: [Int64] -> [String]
solve queries = map (boolToString . isTPrime) queries
    where
        boolToString True = "YES"
        boolToString False = "NO"
        isTPrime n = let m = floor (sqrt (fromIntegral n)) in
                           m * m == n && isPrimeMemo m
        isPrimeMemo m = primeArr A.! m
        primeArr = A.listArray (0,1001000) [ isPrime n | n <- [0..1001000]]
        isPrime 0 = False
        isPrime 1 = False
        isPrime n = and . map (\p -> n `mod` p /= 0) . takeWhile (\p -> p*p <= n) $ primes
        primes = 2:filter isPrimeMemo [3,5..]

main :: IO ()
main = do getLine
          queries <- (getLine >>= return . map readInt64 . words)
          putStr . unlines $ solve queries

readInt64 :: String -> Int64
readInt64 s = L.foldl' (\e c -> (fromIntegral . digitToInt $ c) + 10 * e) 0 s

import Data.List
import Data.Tree
import Data.Int
import Data.Maybe
import Data.Char
import Data.Functor
import Data.Bits
import Control.Monad
import Control.Monad.ST.Safe
import Control.Arrow
import Data.Array
import Data.Array.ST.Safe
import Data.STRef
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.ByteString.Char8 as C
main=   readsolveprint
readsolveprint::IO()
readsolveprint = getLine>>= \ns-> (solve.concat =<< forM [1 ..1] ( \i -> map (fst.fromJust.C.readInteger).C.words <$> C.getLine))
solve (xs) = forM_ xs $ \i-> let (n,f)= properFraction $ sqrt $ fromIntegral i in if (f==0) && (isPrime2 n) then putStrLn "YES" else putStrLn "NO"
primes = fst $ break (>1000) $ filterPrime [2..]
  where filterPrime (p:xs) =
          p : filterPrime [x | x <- xs, rem x p /= 0]
isPrime2 1 = False
isPrime2 2 = True
isPrime2 x =let (b1,b2) =  break (\a -> x `mod` a==0 ) (take ( floor $ sqrt $ fromIntegral x) primes) in if b2==[] then True else False

{-# OPTIONS_GHC -O #-}
{-# LANGUAGE BangPatterns #-}
import Data.Int
import Data.Array.Unboxed
import Control.Monad.ST
import Control.Monad
import Data.Set hiding (map)
import qualified Data.Map as M

magic = 1000

main = do
    n <- fmap read getLine
    xs <- fmap (take n . map read . words) getLine :: IO [Int64]
    putStr.unlines $ map f $ go xs (M.singleton 1 False) []
    --go xs (M.singleton 1 False)

go [] _ acc = reverse acc
go (x:xs) mem acc =
    case M.lookup x mem of
        Just v  -> go xs mem $ v : acc
        Nothing -> (go xs $ M.insert x b mem) $ b : acc
            where b = isinset x

isinset x = x `member` primesSqr

f True = "YES";f False = "NO"

pmagic = take magic primesList
_magicth = last pmagic

isPrime n
    | n > _magicth = case or . map ((==0).(n`mod`)) $ pmagic of
                        False -> n `member` primesSet
                        True  -> False
    | True        = n `elem` pmagic
isTPrime 4 = True
isTPrime 9 = True
isTPrime n
    | n`mod`2 == 0  = False
    | n`mod`3 == 0  = False
    | n`mod`24 /= 1 = False
    | n>999983^2    = False
    | True          = sq*sq == n && isPrime sq
        where sq = round.sqrt.fromIntegral $ n

primesSet = fromList $ drop magic $ primesList
primesSqr = fromList $ map (^2) primesList
primesList = primesToA.floor.(+1).sqrt $ 1e12
primesToA m = sieve 3 (array (3,m) [(i,odd i) | i<-[3..m]]
                        :: UArray Int64 Bool)
  where
    sieve p a
      | p*p > m   = 2 : [i | (i,True) <- assocs a]
      | a!p       = sieve (p+2) $ a//[(i,False) | i <- [p*p, p*p+2*p..m]]
      | otherwise = sieve (p+2) a

{-# LANGUAGE FlexibleInstances #-}
module Main (printShit, printShitV, readShit, main) where
import Data.ByteString.Builder
import qualified Data.ByteString.Lazy.Char8 as BS
import Data.Maybe
-- Meat

solve :: [Integer] -> [BS.ByteString]
solve = map (\x -> if tPrime x then yes else no)
    where yes = BS.pack "YES"
          no = BS.pack "NO"

tPrime :: Integer -> Bool
tPrime 1 = False
tPrime n = sq * sq == n && isPrime sq
    where  sq = round $ sqrt (fromIntegral n::Double)

primes :: [Integer]
primes = filter isPrime [2..]

isPrime :: Integer -> Bool
isPrime 2 = True
isPrime n = 0 `notElem` fmap (n `rem`) (takeWhile (\p -> p*p <= n) primes)

-- Shit
main :: IO ()
main = do
        [_, xs] <- readShit
        printShitV $ solve xs

class Shit a where
    printShit :: a -> IO ()
    printShitV :: a -> IO ()
    readShit :: IO a
    printShitV = printShit

instance  Shit [Int] where
    printShit = putStrLn . unwords . fmap show
    printShitV = putStrLn . unlines . fmap show
    readShit = fmap (fmap read . words) getLine

instance  Shit [Integer] where
    printShit = putStrLn . unwords . fmap show
    printShitV = putStrLn . unlines . fmap show
    readShit = fmap (fmap read . words) getLine

instance Shit [[Int]] where
    printShit = putStrLn . unlines . fmap (unwords . fmap show)
    readShit = fmap (fmap (fmap read . words) . lines) getContents

instance Shit [[Integer]] where
    printShit  = printShit . (fmap.fmap)  (toLazyByteString . integerDec)
    readShit = fmap (fmap.fmap $ fromInteger.fst.fromJust. BS.readInteger) readShit

instance Shit [String] where
    printShit = putStrLn. unwords
    printShitV = putStrLn . unlines
    readShit = fmap lines getContents

instance Shit [BS.ByteString] where
    printShit = BS.putStrLn. BS.unwords
    printShitV = BS.putStrLn . BS.unlines
    readShit = fmap BS.lines BS.getContents

instance Shit [[BS.ByteString]] where
    printShit = BS.putStrLn . BS.unlines . fmap BS.unwords
    readShit = fmap (fmap (fmap BS.words) BS.lines) BS.getContents

{-# OPTIONS_GHC -O #-}
{-# LANGUAGE BangPatterns #-}
import Data.Int
import Data.Array.Unboxed
import Control.Monad.ST
import Control.Monad
import Data.Set hiding (map)
import qualified Data.Map as M

main = do
    n <- fmap read getLine
    xs <- fmap (take n . map read . words) getLine :: IO [Int64]
    mapM_ (putStrLn.f.isinset) xs
    --putStr.unlines $ map f $ go xs (M.singleton 1 False) []
{-
go [] _ acc = reverse acc
go (x:xs) mem acc =
    case M.lookup x mem of
        Just v  -> go xs mem $ v : acc
        Nothing -> (go xs $ M.insert x b mem) $ b : acc
            where b = isinset x
  -}
isinset x = x `member` primesSqr
f True = "YES";f False = "NO"

primesSqr = fromList $ map (^2) primesList
primesList = primesToA.floor.(+1).sqrt $ 1e12
primesToA m = sieve 3 (array (3,m) [(i,odd i) | i<-[3..m]]
                        :: UArray Int64 Bool)
  where
    sieve p a
      | p*p > m   = 2 : [i | (i,True) <- assocs a]
      | a!p       = sieve (p+2) $ a//[(i,False) | i <- [p*p, p*p+2*p..m]]
      | otherwise = sieve (p+2) a

import Char (ord)
import Data.Set (fromList, member)
import Monad (liftM)

isPrime :: Integer -> Bool
isPrime n  =  all (\x -> mod n x /= 0) $ takeWhile (\x -> x * x <= n) primes

primes :: [Integer]
primes  =  2 : [n | n <- [3,5..], isPrime n]

solve :: [Integer] -> [Bool]
solve xs  =  map isSqrPrime xs
  where
    set  =  fromList . takeWhile (<= 10^6) $ primes
    isSqrPrime 1  =  False
    isSqrPrime x
      | x' * x' == x  =  member x' set
      | otherwise  =  False
      where
        x'  =  round . sqrt . fromIntegral $ x

(<?>) :: a -> a -> Bool -> a
(<?>) a b p  =  if p then a else b

parseLine :: String -> [Integer]
parseLine []  =  []
parseLine (c:s)
  | c == ' '  =  parseLine s
  | otherwise  =  parseLine' (f c) s
  where
    f c  =  fromIntegral (ord c - ord '0')
    parseLine' a []  =  [a]
    parseLine' a (c:s)
      | c == ' '  =  a : parseLine s
      | otherwise  =  parseLine' (10 * a + f c) s

main :: IO ()
main  =  getLine >> liftM parseLine getLine >>= mapM_ (putStrLn . ("YES" <?> "NO")) . solve

import Control.Applicative
import Data.List
import qualified Data.ByteString.Char8 as C
import Data.Maybe
import qualified Data.HashMap.Strict as H

--isprime 4 =True
--isprime x = all (>0)[ mod sx i|i<-[2]++[3,5..ssx+1]]
--     where sx = floor $ sqrt $ fromIntegral x
--           ssx = floor $ sqrt $ fromIntegral sx
primes ::[Integer]
primes = [2,3,5,7] ++ [x|x<-[11,13..],isprime x]

isprime::Integer->Bool
isprime x = and [mod x y/=0 |y<-takeWhile (<=xx) primes ]
  where xx = floor $ sqrt $ fromIntegral x

isrprime::Integer->Bool
isrprime s = isprime r
  where r = floor $ sqrt $ fromIntegral s

nosquare x = sx*sx /=x
     where sx = floor $ sqrt $ fromIntegral x

process 1 ="NO"
process s | nosquare s = "NO"
          | isrprime s = "YES"
          | otherwise = "NO"

process1::[Integer]->H.HashMap Integer String->[String]->[String]
process1 q m a | q==[] = reverse a
               | H.lookup (head q) m == Nothing = process1 (tail q) (H.insert (head q) (process (head q) ) m)  ((process (head q )):a)
               | otherwise = process1 (tail q) m ((fromJust(H.lookup (head q) m)):a)

main=do
    n<-     read  <$> getLine::IO Integer
    q<-    map ( fst.fromJust.C.readInteger) <$> C.words <$> C.getLine::IO [Integer]
    mapM_ putStrLn $   process1 q H.empty []

{-# OPTIONS -O2 -optc-O3 #-}
import Control.Arrow ((***))
import Control.Monad (forM_, when)
import Control.Monad.ST (ST)
import Data.Array.ST (STUArray, runSTUArray, newArray, readArray, writeArray)
import Data.Array.Unboxed (UArray, (!), assocs)

isqrt :: (Integral a, Integral b) => a -> b
isqrt = floor . sqrt . fromIntegral

sieveToN :: Int -> UArray Int Bool
sieveToN n = runSTUArray sieve
  where
    sieve = do
      a <- newArray (2, n) True :: ST s (STUArray s Int Bool)
      forM_ [4, 6 .. n] $ \j ->
        writeArray a j False
      forM_ [3, 5 .. isqrt n] $ \i -> do
        ai <- readArray a i
        when ai $
          forM_ [i * i, i * (i + 2) .. n] $ \j ->
            writeArray a j False
      return a

primeTab :: UArray Int Bool
primeTab = sieveToN 1000000

isPrime :: Integral a => a -> Bool
isPrime n = n >= 2 && primeTab!fromIntegral n

main = do
  getLine
  getLine >>= putStrLn . unlines . map (gao . read) . words
  where
    gao n = let m = isqrt n in if m * m == n && isPrime m then "YES" else "NO"

main=getContents>>=mapM_(putStrLn.h.read).tail.words
h n|n==m*m&&f m="YES"|1>0="NO" where m=round$sqrt$fromIntegral n
g=2:filter f[3,5..]
f n=n>1&&foldr(\p r->p*p>n||n`mod`p/=0&&r)True g

import qualified Data.ByteString.Char8 as B
import Data.Maybe (fromJust)

main :: IO ()
main = B.getContents >>= mapM_ (putStrLn . yesno . solve . fst . fromJust . B.readInteger) . tail . B.words

solve :: Integer -> Bool
solve n = n == m * m && isPrime m
  where m = round (sqrt (fromIntegral n) :: Double)

yesno :: Bool -> String
yesno True = "YES"
yesno _ = "NO"

primes :: Integral a => [a]
primes = 2 : filter isPrime [3,5..]

isPrime :: Integral a => a -> Bool
isPrime n = n > 1 && foldr (\p r -> p * p > n || n `mod` p /= 0 && r) True primes

main :: IO ()
main = getContents >>= mapM_ (putStrLn . yesno . solve . read) . tail . words

solve :: Integer -> Bool
solve n = n == m * m && isPrime m
  where m = round (sqrt (fromIntegral n) :: Double)

yesno :: Bool -> String
yesno True = "YES"
yesno _ = "NO"

primes :: Integral a => [a]
primes = 2 : filter isPrime [3,5..]

isPrime :: Integral a => a -> Bool
isPrime n = n > 1 && foldr (\p r -> p * p > n || n `mod` p /= 0 && r) True primes

main :: IO ()
main = getContents >>= mapM_ (putStrLn . yesno . solve . read) . tail . words

solve :: Integer -> Bool
solve n = let m = (round :: Double -> Integer) $ sqrt $ fromIntegral n in n == m * m && isPrime m

yesno :: Bool -> String
yesno True = "YES"
yesno _ = "NO"

primes :: Integral a => [a]
primes = 2 : filter isPrime [3,5..]

isPrime :: Integral a => a -> Bool
isPrime n = n > 1 && foldr (\p r -> p * p > n || n `mod` p /= 0 && r) True primes

main=getContents>>=mapM_(putStrLn.h.read).tail.words
h n|n==m*m&&f m="YES"|1>0="NO" where m=round$sqrt$fromIntegral n
f n=n>1&&foldr(\p r->p*p>n||n`mod`p/=0&&r)True(2:filter f[3,5..])

import           Control.Monad
import           Control.Monad.ST
import           Data.Array.ST.Safe
import           Data.Int
import           Data.STRef
import qualified Data.Set as S
main = interact $ unlines . map (format . (`S.member` squares)) . map read . tail . words
format True = "YES"
format False = "NO"
squares = runST $ do
  sieve <- newArray (2,10^6) True :: ST s (STUArray s Int64 Bool)
  squares <- newSTRef S.empty
  forM_ [2..10^6] $ \i -> do
    p <- readArray sieve i
    when p $ do
      modifySTRef' squares (S.insert (i^2 :: Int64))
      when (i <= 10^3) $ forM_ [2*i, 3*i..10^6] $ \j -> writeArray sieve j False
  readSTRef squares

{-# LANGUAGE BangPatterns #-}

import Data.Array.Base(unsafeRead,unsafeWrite,unsafeAt)
import Data.Array.ST
import Data.Array.Unboxed
import Data.Bits
import qualified Data.ByteString.Char8 as B
import Control.Monad

readInteger bs = case B.readInteger bs of Just (n,_) -> n

main = B.getContents >>= putStrLn.unlines.solve.map readInteger.tail.B.words

solve :: [Integer] -> [String]
solve xs = map go xs
   where
     !isPrime = sieveOdds 1000000
     go x
       | fromIntegral sqrtx * fromIntegral sqrtx /= x = "NO"
       | x==4 || odd sqrtx && unsafeAt isPrime (sqrtx`shiftR`1) = "YES"
       | otherwise = "NO"
      where
        !sqrtx = bigisqrt x

sieveOdds :: Int -> UArray Int Bool
sieveOdds num = runSTUArray $ do
  let !num2 = num`div`2
  isPrime <- newArray (0,num2) True
  unsafeWrite isPrime 0 False
  forM_ [1..isqrt num`shiftR`1] $ \i-> do
    b <- unsafeRead isPrime i
    when b $ do
      let !p = i`shiftL`1 + 1
          !pp = i*(p+1)
      forM_ [pp,pp+p..num2] $ \j-> do
        unsafeWrite isPrime j False
  return isPrime

isqrt :: Int -> Int
isqrt x = floor.(1e-8+).sqrt $ fromIntegral x

bigisqrt :: Integer -> Int
bigisqrt x = floor.(1e-8+).sqrt $ fromIntegral x

{-# OPTIONS_GHC -O2 #-}

import Data.List
import Data.Word

--nubSorted :: Ord a => [a] -> [a]
--nubSorted [] = []
--nubSorted [x] = [x]
--nubSorted (x1:x2:xs) = if x1 == x2 then nubSorted (x2:xs) else x1 : nubSorted (x2:xs)

isPrime :: Word64 -> Bool
isPrime n | n < 2 = False
          | n == 2 = True
          | even n = False
          | (rootF n) ^ 2 == n = False
          | otherwise = null $ filter (\x -> mod n x == 0) $ takeWhile (\x -> x*x < n) [3, 5..]

rootF = round . sqrt . fromIntegral

isTPrime2 :: Word64 -> Bool
isTPrime2 999966000289 = True
isTPrime2 n =
    let root = rootF n
        in (isPrime $! root) && root * root == n

main = do
    n <- getLine
    xs <- words `fmap` getContents
    mapM_ (\x -> putStrLn (if isTPrime2 (read x) then "YES" else "NO")) xs

{-# LANGUAGE FlexibleContexts, ScopedTypeVariables, BangPatterns #-}
{-# OPTIONS_GHC -O2 #-}

import Control.Applicative
import Control.Monad hiding ((<$!>))
import Control.Monad.ST.Safe
import Control.Monad.State hiding ((<$!>))
-- import Data.Array
import Data.Array.IArray
import Data.Array.IO.Safe
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Array.Unboxed
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Builder as BB
import Data.Char
import qualified Data.Foldable as F
import Data.Function
import Data.Graph
import Data.Int
import Data.List
import Data.List.Split
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.Ratio
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
-- import Data.Tree
import Data.Tuple
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
-- import Debug.Trace
import System.IO

-- getInts = fmap (map read . words) getLine
getInts = unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine

main = do
  getLine
  xs <- map fromIntegral <$> getInts :: IO [Int64]

  let
    primes = [a | a <- [2..10^3], all ((/= 0) . mod a) [2..a-1]]

    check 1 = False
    check x = isSqrt && isPrime
      where
        y = truncate $ sqrt $ fromIntegral x :: Int64
        isSqrt = y*y == x
        isPrime = all (\p -> y `mod` p /= 0) $ filter (\p -> p*p <= y) primes

  putStrLn $ unlines $ map ((\x -> if x then "YES" else "NO") . check) xs

{-# LANGUAGE FlexibleContexts, ScopedTypeVariables, BangPatterns #-}
{-# OPTIONS_GHC -O2 #-}

import Control.Applicative
import Control.Monad hiding ((<$!>))
import Control.Monad.ST.Safe
import Control.Monad.State hiding ((<$!>))
-- import Data.Array
import Data.Array.IArray
import Data.Array.IO.Safe
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Array.Unboxed
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Builder as BB
import Data.Char
import qualified Data.Foldable as F
import Data.Function
import Data.Graph
import Data.Int
import Data.List
import Data.List.Split
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.Ratio
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
-- import Data.Tree
import Data.Tuple
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
-- import Debug.Trace
import System.IO

-- getInts = fmap (map read . words) getLine
getInts = unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine

main = do
  getLine
  xs <- map fromIntegral <$> getInts :: IO [Int64]

  let
    primes = [a | a <- [2..10^3], all ((/= 0) . mod a) [2..a-1]]

    check x = isSqrt && isPrime
      where
        y = truncate $ sqrt $ fromIntegral x
        isSqrt = y*y == x
        isPrime = all (\p -> y `mod` p /= 0) $ filter (\p -> p*p <= y) primes

  putStrLn $ unlines $ map ((\x -> if x then "YES" else "NO") . check) xs

{-# LANGUAGE FlexibleContexts, ScopedTypeVariables, BangPatterns #-}
{-# OPTIONS_GHC -O2 #-}

import Control.Applicative
import Control.Monad hiding ((<$!>))
import Control.Monad.ST.Safe
import Control.Monad.State hiding ((<$!>))
-- import Data.Array
import Data.Array.IArray
import Data.Array.IO.Safe
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Array.Unboxed
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Builder as BB
import Data.Char
import qualified Data.Foldable as F
import Data.Function
import Data.Graph
import Data.Int
import Data.List
import Data.List.Split
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.Ratio
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
-- import Data.Tree
import Data.Tuple
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
-- import Debug.Trace
import System.IO

-- getInts = fmap (map read . words) getLine
getInts = unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine

main = do
  getLine
  xs <- map fromIntegral <$> getInts :: IO [Int64]

  let
    primes = [a | a <- [2..10^3], all ((/= 0) . mod a) [2..a-1]]

    check 1 = False
    check x = isSqrt && isPrime
      where
        y = truncate $ sqrt $ fromIntegral x
        isSqrt = y*y == x
        isPrime = all (\p -> y `mod` p /= 0) $ filter (\p -> p*p <= y) primes

  putStrLn $ unlines $ map ((\x -> if x then "YES" else "NO") . check) xs

import Control.Monad (forM_)
import qualified Data.ByteString.Char8 as BS
import Data.Int

sqr :: Double -> Double
sqr = sqrt

isqrt :: Int64 -> Maybe Int64
isqrt n = if r*r == n then Just r else Nothing
  where r = floor $ (sqr (fromIntegral n) )

tprime :: Int64 -> Bool
tprime n =
  case isqrt n of
    Nothing -> False
    Just r  -> isPrime' r

isPrime' :: Int64 -> Bool
isPrime' n | n `mod` 2 == 0 = n == 2
isPrime' n = all (\m -> n `rem` m /= 0) [3,5..end]
  where end = floor (sqr (fromIntegral n))

allTrue = all id

(-->) = flip fmap

readInt :: BS.ByteString -> Integer
readInt s = case BS.readInteger s of
              Nothing    -> error "bad integer"
              Just (n,_) -> n

main = do
  n <- getLine --> read
  let _ = n :: Int
  nums <- BS.getLine --> BS.words --> map (fromIntegral . readInt)
  forM_ nums $ \n -> do
    putStrLn $ if tprime n then "YES" else "NO"

import Control.Monad (forM_)
import qualified Data.ByteString.Char8 as BS
import Data.Int

sqr :: Double -> Double
sqr = sqrt

isqrt :: Int64 -> Maybe Int64
isqrt n = if r*r == n then Just r else Nothing
  where r = floor $ (sqr (fromIntegral n) )

tprime :: Int64 -> Bool
tprime n =
  case isqrt n of
    Nothing -> False
    Just r  -> isPrime' r

isPrime' :: Int64 -> Bool
isPrime' n | n `mod` 2 == 0 = n == 2
isPrime' n = all (\m -> n `rem` m /= 0) [3,5..end]
  where end = floor (sqr (fromIntegral n))

allTrue = all id

(-->) = flip fmap

readInt :: BS.ByteString -> Integer
readInt s = case BS.readInteger s of
              Nothing    -> error "bad integer"
              Just (n,_) -> n

main = do
  n <- getLine --> read
  let _ = n :: Int
  nums <- BS.getLine --> BS.words --> map (fromIntegral . readInt)
  forM_ nums $ \n -> do
    putStrLn $ (if tprime n then "YES" else "NO") ++ " " ++ show n

import Data.List
import Data.Tree
import Data.Int
import Data.Maybe
import Data.Char
import Data.Functor
import Data.Bits
import Control.Monad
import Control.Monad.ST.Safe
import Control.Arrow
import Data.Array
import Data.Array.ST.Safe
import Data.STRef
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.ByteString.Char8 as C
main=   readsolveprint
readsolveprint::IO()
readsolveprint = getLine>>= \ns-> (solve.concat =<< forM [1 ..1] ( \i -> map (fst.fromJust.C.readInt).C.words <$> C.getLine))
solve::[Int]->IO()
solve (xs) =forM_ xs $ \i-> let (n,f)= properFraction $ sqrt $ fromIntegral i in if (f==0) && (isPrime n) then putStrLn "YES" else putStrLn "NO"
isPrime 1 = False
isPrime 2 = True
isPrime x = foldr (\a b -> b && x `mod` a /=0) True [2..x-1]

import Control.Applicative
import Data.List

isprime x = all (>0)[ mod x i|i<-[2..sx+1]]
     where sx = floor $ sqrt $ fromIntegral x

nosquare x = sx*sx /=x
     where sx = floor $ sqrt $ fromIntegral x
process s | nosquare s = "NO"
          | isprime s = "YES"
          | otherwise = "NO"

main=do
    n<-     read  <$> getLine::IO Integer
    q<-    map read <$> words <$> getLine::IO [Integer]
    mapM_ putStrLn $ map process q

import Control.Applicative
import Data.List

isprime 4 =True
isprime x = all (>0)[ mod sx i|i<-[2..ssx+1]]
     where sx = floor $ sqrt $ fromIntegral x
           ssx = floor $ sqrt $ fromIntegral sx

nosquare x = sx*sx /=x
     where sx = floor $ sqrt $ fromIntegral x

process s | nosquare s = "NO"
          | isprime s = "YES"
          | otherwise = "NO"

main=do
    n<-     read  <$> getLine::IO Integer
    q<-    map read <$> words <$> getLine::IO [Integer]
    mapM_ putStrLn $ map process q

import Control.Applicative
import Data.List
import qualified Data.ByteString.Char8 as C
import Data.Maybe
import qualified Data.HashMap.Strict as H

isprime 4 =True
isprime x = all (>0)[ mod sx i|i<-[2]++[3,5..ssx+1]]
     where sx = floor $ sqrt $ fromIntegral x
           ssx = floor $ sqrt $ fromIntegral sx

nosquare x = sx*sx /=x
     where sx = floor $ sqrt $ fromIntegral x

process 1 ="NO"
process s | nosquare s = "NO"
          | isprime s = "YES"
          | otherwise = "NO"

process1::[Int]->H.HashMap Int String->[String]->[String]
process1 q m a | q==[] = reverse a
               | H.lookup (head q) m == Nothing = process1 (tail q) (H.insert (head q) (process (head q) ) m)  ((process (head q )):a)
               | otherwise = process1 (tail q) m ((fromJust(H.lookup (head q) m)):a)

main=do
    n<-     read  <$> getLine::IO Int
    q<-    map (fromIntegral. fst.fromJust.C.readInteger) <$> C.words <$> C.getLine::IO [Int]
    mapM_ putStrLn $   process1 q H.empty []

import Control.Applicative
import Data.List

isprime x = all (>0)[ mod x i|i<-[2..sx+1]]
     where sx = floor $ sqrt $ fromIntegral x

nosquare x = sx*sx ==x
     where sx = floor $ sqrt $ fromIntegral x
process s | nosquare s = "NO"
          | isprime s = "YES"
          | otherwise = "NO"

main=do
    n<-     read  <$> getLine::IO Integer
    q<-    map read <$> words <$> getLine::IO [Integer]
    mapM_ putStrLn $ map process q

import qualified Data.ByteString.Char8 as B
import Data.Maybe (fromJust)

main :: IO ()
main = B.getContents >>= mapM_ (putStrLn . yesno . solve . fromIntegral . fst . fromJust . B.readInt) . tail . B.words

solve :: Integer -> Bool
solve n = n == m * m && isPrime m
  where m = round (sqrt (fromIntegral n) :: Double)

yesno :: Bool -> String
yesno True = "YES"
yesno _ = "NO"

primes :: Integral a => [a]
primes = 2 : filter isPrime [3,5..]

isPrime :: Integral a => a -> Bool
isPrime n = n > 1 && foldr (\p r -> p * p > n || n `mod` p /= 0 && r) True primes

import Data.List (group)

main :: IO ()
main = getContents >>= mapM_ (putStrLn . yesno) . solve . map read . tail . words

solve :: [Int] -> [Bool]
solve = map ((==3) . numberOfDivisors)

yesno :: Bool -> String
yesno True = "YES"
yesno _ = "NO"

primes :: Integral a => [a]
primes = 2 : filter isPrime [3,5..]

isPrime :: Integral a => a -> Bool
isPrime n = n > 1 && foldr (\p r -> p * p > n || n `mod` p /= 0 && r) True primes

primeFactors :: Integral a => a -> [a]
primeFactors n | n > 1 = go n primes
               | otherwise = []
   where go m ps@(p:t) | p * p > m  = [m]
                       | r == 0     =  p : go q ps
                       | otherwise  =      go m t
                          where (q, r) = quotRem m p
         go _ _ = []

numberOfDivisors :: Integral a => a -> Int
numberOfDivisors = product . map ((+1) . length) . group . primeFactors

import           Control.Monad
import           Control.Monad.ST
import           Data.Array.ST.Safe
import           Data.Int
import           Data.STRef
import qualified Data.Set as S
main = interact $ unlines . map (format . (`S.member` squares)) . map read . tail . words
format True = "YES"
format False = "NO"
squares = runST $ do
  sieve <- newArray (2,10^6) True :: ST s (STUArray s Int64 Bool)
  squares <- newSTRef S.empty
  forM_ [2..10^3] $ \i -> do
    p <- readArray sieve i
    when p $ do
      modifySTRef' squares (S.insert (i^2 :: Int64))
      forM_ [2*i, 3*i..10^6] $ \j -> writeArray sieve j False
  readSTRef squares

import Data.List

nubSorted :: Ord a => [a] -> [a]
nubSorted [] = []
nubSorted [x] = [x]
nubSorted (x1:x2:xs) = if x1 == x2 then nubSorted (x2:xs) else x1 : nubSorted (x2:xs)

isTPrime :: Int -> Bool
isTPrime n =
    let root = (truncate . sqrt . fromIntegral $ n) :: Int
        subr = if root*root == n then 1 else 0
        in 3 == (2 * (length . nubSorted . sort $
            filter (\x -> mod n x == 0) [1..root]) - subr)

isPrime :: Int -> Bool
isPrime n | n < 2 = False
          | n == 2 = True
          | even n = False
          | otherwise = null $ filter (\x -> mod n x == 0) [3,5..root]
    where
        root = (truncate . sqrt . fromIntegral $ n) :: Int

isTPrime2 :: Int -> Bool
isTPrime2 n =
    let root = (truncate . sqrt . fromIntegral $ n) :: Int
        in isPrime root && root * root == n

main = do
    n <- fmap (\x -> read x :: Int) getLine
    xs <- (map read.words) `fmap` getLine
    mapM_ (\x -> putStrLn (if isTPrime2 x then "YES" else "NO")) xs

import Data.List
import Data.Word

isPrime :: Word64 -> Bool
isPrime n | n < 2 = False
          | n == 2 = True
          | even n = False
          | otherwise = null $ filter (\x -> mod n x == 0) $ takeWhile (\x -> x*x <= root) [3,5..root]
    where
        root = (truncate . sqrt . fromIntegral $ n) :: Word64

isTPrime2 :: Word64 -> Bool
isTPrime2 n =
    let root = (truncate . sqrt . fromIntegral $ n) :: Word64
        in isPrime root && (root * root == n)

main = do
    n <- getLine
    xs <- (map read.words) `fmap` getLine
    mapM_ (\x -> putStrLn (if isTPrime2 x then "YES" else "NO")) xs

import Control.Applicative
import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as C
import Data.Maybe
import Data.Char

squ x =  x==y*y && isprime y
	where y = ceiling $ sqrt $ fromIntegral x

primes = [2,3,5,7,11] ++ (filter isprime [13,15..] )
isprime 2 = True
isprime x = all (>0) [mod x y| y<- takeWhile (<=(ceiling (sqrt (fromIntegral x)))) primes]

main= do
	n  <-  getLine
	s<- map (fst.fromJust.C.readInteger). C.words <$> C.getLine   ::IO [Integer]
	putStrLn $ intercalate "\n" $ map (\z-> if squ z   then "YES" else "NO") s

import Data.Int

isPrime :: Int64 -> Bool
isPrime n =
  if n `mod` 2 == 0
    then n == 2
    else not $ any (\d -> n `mod` d == 0) [3,5..(floor.sqrt.fromIntegral) n]

yn :: Bool -> [Char]
yn True = "YES"
yn False = "NO"

isT :: Int64 -> Bool
isT x = let s = floor $ sqrt $ fromIntegral x in s*s==x && isPrime s

main = do
  n <- getLine
  s_xs <- getLine
  putStr $ unlines $ map yn $ map isT $ map read $ words s_xs

{-# OPTIONS_GHC -O2 #-}
import Data.Int
import Data.Array.Unboxed

main = do
    n <- fmap read getLine
    xs <- fmap (take n . map read . words) getLine :: IO [Int64]
    mapM_ (putStrLn.f.is_square) xs

f True = "YES"
f False = "NO"

is_square 1 = False
is_square n = or . map (==n) $ [foldl (*) sq (replicate x sq)| x <- [1..44]]
    where sq = round $ sqrt $ (fromIntegral n::Double)

{-# OPTIONS_GHC -O2 #-}
import Data.Int
import Data.Array.Unboxed

main = do
    n <- fmap read getLine
    xs <- fmap (take n . map read . words) getLine :: IO [Int64]
    mapM_ (putStrLn.f.isTPrime) xs

f True = "YES"
f False = "NO"

is_square 1 = False
is_square n = case filter (==n) $ [foldl (*) (sq x) (replicate (x-1) (sq x)) | x <- [2..40]] of
            [] -> False
            (s:_) -> True
    where sq x = round . (**(1/fromIntegral x)) $ (fromIntegral n::Double)

isTPrime n = length (primeDivs n) == 1
primeDivs n = (filter (\x -> n`mod`x == 0)
                $ takeWhile (\x -> x < (round.(+1).(/2).fromIntegral $ n))
                $ primesList)
--primesArr = listArray (1,227647) primesList :: UArray Int Int64
primesList = (primesToA.round.(+1).sqrt $ 10e12)

primesToA m = sieve 3 (array (3,m) [(i,odd i) | i<-[3..m]]
                        :: UArray Int64 Bool)
  where
    sieve p a
      | p*p > m   = 2 : [i | (i,True) <- assocs a]
      | a!p       = sieve (p+2) $ a//[(i,False) | i <- [p*p, p*p+2*p..m]]
      | otherwise = sieve (p+2) a

{-# OPTIONS_GHC -O2 #-}
import Data.Int
import Data.Array.Unboxed
import Data.Set hiding (map)

main = do
    n <- fmap read getLine
    xs <- fmap (take n . map read . words) getLine :: IO [Int64]
    mapM_ (putStrLn.f.isTPrime) xs

f True = "YES";f False = "NO"

isTPrime 4 = True
isTPrime n
    | n`mod`2 == 0 = False
    | True         = sq*sq == n && isPrime sq
    where sq = floor.sqrt.fromIntegral $ n

isPrime n
    | n `mod` 24 /= 1 = False
    | True = n `member` primesSet

primesSet = fromList (primesToA.floor.(+1).sqrt $ 10e12)

primesToA m = sieve 3 (array (3,m) [(i,odd i) | i<-[3..m]]
                        :: UArray Int64 Bool)
  where
    sieve p a
      | p*p > m   = 2 : [i | (i,True) <- assocs a]
      | a!p       = sieve (p+2) $ a//[(i,False) | i <- [p*p, p*p+2*p..m]]
      | otherwise = sieve (p+2) a

{-# OPTIONS_GHC -O2 #-}
import Data.Int
import Data.Array.Unboxed

main = do
    n <- fmap read getLine
    xs <- fmap (take n . map read . words) getLine :: IO [Int64]
    mapM_ (putStrLn.f.is_square) xs

f True = "YES"
f False = "NO"

is_square 1 = False
is_square n = or . map (==n) $ [foldl (*) (sq x) (replicate (x-1) (sq x)) | x <- [2..40]]
    where sq x = round . (**(1/fromIntegral x)) $ (fromIntegral n::Double)

{-# OPTIONS_GHC -O2 #-}
import Data.Int
import Data.Array.Unboxed

main = do
    n <- fmap read getLine
    xs <- fmap (take n . map read . words) getLine :: IO [Int64]
    mapM_ (putStrLn.f.isTPrime) xs

f True = "YES"
f False = "NO"

is_square 1 = False
is_square n = case filter (==n) $ [foldl (*) (sq x) (replicate (x-1) (sq x)) | x <- [2..40]] of
            [] -> False
            (s:_) -> True
    where sq x = round . (**(1/fromIntegral x)) $ (fromIntegral n::Double)

isTPrime n = or (primeDivs n)
primeDivs n = map (c==) $ primesList
    where c = round.sqrt.fromIntegral $ n
--primesArr = listArray (1,227647) primesList :: UArray Int Int64
primesList = (primesToA.round.(+1).sqrt $ 10e12)

primesToA m = sieve 3 (array (3,m) [(i,odd i) | i<-[3..m]]
                        :: UArray Int64 Bool)
  where
    sieve p a
      | p*p > m   = 2 : [i | (i,True) <- assocs a]
      | a!p       = sieve (p+2) $ a//[(i,False) | i <- [p*p, p*p+2*p..m]]
      | otherwise = sieve (p+2) a

{-# OPTIONS_GHC -O2 #-}
import Data.Int
import Data.Array.Unboxed

main = do
    n <- fmap read getLine
    xs <- fmap (take n . map read . words) getLine
    mapM_ (putStrLn.f) xs

f :: Int64 -> String
f n
 | (round.sqrt.fromIntegral $ n)^2 == n = "YES"
 | True = "NO"

{-# OPTIONS_GHC -O2 #-}
import Data.Int
import Data.Array.Unboxed

main = do
    n <- fmap read getLine
    xs <- fmap (take n . map read . words) getLine :: IO [Int64]
    mapM_ (putStrLn.f.is_square) xs

f True = "YES"
f False = "NO"

is_square 1 = False
is_square n = sq * sq == n
    where sq = floor $ sqrt $ (fromIntegral n::Double)

{-# OPTIONS_GHC -O2 #-}
import Data.Int
import Data.Array.Unboxed
import Data.Set hiding (map)

main = do
    let
        primesSet = fromList primesList
        primesList = primesToA.floor.(+1).sqrt $ 10e12
        isPrime n
            | not . or . map ((==0).(n`mod`)) $ take 1000 primesList = False
            | True = n `member` primesSet
        isTPrime 4 = True
        isTPrime 9 = True
        isTPrime n
            | n`mod`2 == 0  = False
            | n`mod`24 /= 1 = False
            | True          = sq*sq == n && isPrime sq
            where sq = round.sqrt.fromIntegral $ n
    n <- fmap read getLine
    xs <- fmap (take n . map read . words) getLine :: IO [Int64]
    mapM_ (putStrLn.f.isTPrime) xs

f True = "YES";f False = "NO"

isPrime n
    |not. or . map ((==0).(n`mod`)) $ take 1000 primesList = False
    | True = n `member` primesSet

primesSet = fromList primesList
primesList = primesToA.floor.(+1).sqrt $ 10e12
primesToA m = sieve 3 (array (3,m) [(i,odd i) | i<-[3..m]]
                        :: UArray Int64 Bool)
  where
    sieve p a
      | p*p > m   = 2 : [i | (i,True) <- assocs a]
      | a!p       = sieve (p+2) $ a//[(i,False) | i <- [p*p, p*p+2*p..m]]
      | otherwise = sieve (p+2) a

{-# OPTIONS_GHC -O2 #-}
import Data.Int

main = do
    n <- fmap read getLine :: IO Int64
    xs <- fmap (map read . words) getLine
    mapM_ (putStrLn.sln) xs

sln p =
    case (fromIntegral $ j*j)>b && b*b==a && a>1.0 of
        True -> "YES"
        False -> "NO"
      where
        c' acc
            | (round b)`mod`acc == 0  = acc
            | fromIntegral (acc*acc)<=b            = acc
            | True                  = c' (acc+1)
        a = fromIntegral p
        b = sqrt a :: Double
        j = (c' 2) :: Int64

{-# OPTIONS_GHC -O2 #-}
import Data.Int
import Data.Array.Unboxed

main = do
    n <- fmap read getLine
    xs <- fmap (take n . map read . words) getLine :: IO [Int64]
    mapM_ (putStrLn.f.isTPrime) xs

f True = "YES"
f False = "NO"

is_square 1 = False
is_square n = case filter (==n) $ [foldl (*) (sq x) (replicate (x-1) (sq x)) | x <- [2..40]] of
            [] -> False
            (s:_) -> True
    where sq x = round . (**(1/fromIntegral x)) $ (fromIntegral n::Double)

isTPrime n = length (primeDivs n) == 2
primeDivs n = filter (\x -> n`mod`x == 0) primesList
--primesArr = listArray (1,227647) primesList :: UArray Int Int64
primesList = 2 : (primesToA.round.(+1).sqrt $ 10e12)

primesToA m = sieve 3 (array (3,m) [(i,odd i) | i<-[3..m]]
                        :: UArray Int64 Bool)
  where
    sieve p a
      | p*p > m   = 2 : [i | (i,True) <- assocs a]
      | a!p       = sieve (p+2) $ a//[(i,False) | i <- [p*p, p*p+2*p..m]]
      | otherwise = sieve (p+2) a

{-# OPTIONS_GHC -O2 #-}
import Data.Int
import Data.Array.Unboxed

main = do
    n <- fmap read getLine
    xs <- fmap (take n . map read . words) getLine :: IO [Int64]
    mapM_ (putStrLn.f.is_square) xs

f True = "YES"
f False = "NO"

is_square 1 = False
is_square n = or . map (==n) $ [foldl (*) sq (replicate x sq)| x <- [1..44]]
    where sq = floor $ sqrt $ (fromIntegral n::Double)

{-# OPTIONS_GHC -O2 #-}
import Data.Int
import Data.Array.Unboxed

main = do
    n <- fmap read getLine
    xs <- fmap (take n . map read . words) getLine :: IO [Int64]
    mapM_ (putStrLn.f.isTPrime) xs

f True = "YES"
f False = "NO"

is_square 1 = False
is_square n = case filter (==n) $ [foldl (*) (sq x) (replicate (x-1) (sq x)) | x <- [2..40]] of
            [] -> False
            (s:_) -> True
    where sq x = round . (**(1/fromIntegral x)) $ (fromIntegral n::Double)

isTPrime n = length (primeDivs n) == 3
primeDivs n = 1 : filter (\x -> n`mod`x == 0) primesList
--primesArr = listArray (1,227647) primesList :: UArray Int Int64
primesList = (primesToA.round.(+1).sqrt $ 10e12)

primesToA m = sieve 3 (array (3,m) [(i,odd i) | i<-[3..m]]
                        :: UArray Int64 Bool)
  where
    sieve p a
      | p*p > m   = 2 : [i | (i,True) <- assocs a]
      | a!p       = sieve (p+2) $ a//[(i,False) | i <- [p*p, p*p+2*p..m]]
      | otherwise = sieve (p+2) a

{-# OPTIONS_GHC -O2 #-}
import Data.Int
import Data.Array.Unboxed
import Data.Set hiding (map)

main = do
    n <- fmap read getLine
    xs <- fmap (take n . map read . words) getLine :: IO [Int64]
    mapM_ (putStrLn.f.isTPrime) xs

f True = "YES";f False = "NO"

isTPrime 4 = True
isTPrime n
    | n`mod`2 == 0  = False
    | n`mod`24 /= 1 = False
    | True          = sq*sq == n && isPrime sq
    where sq = floor.sqrt.fromIntegral $ n

isPrime = (`member` primesSet)

primesSet = fromList (primesToA.floor.(+1).sqrt $ 10e12)

primesToA m = sieve 3 (array (3,m) [(i,odd i) | i<-[3..m]]
                        :: UArray Int64 Bool)
  where
    sieve p a
      | p*p > m   = 2 : [i | (i,True) <- assocs a]
      | a!p       = sieve (p+2) $ a//[(i,False) | i <- [p*p, p*p+2*p..m]]
      | otherwise = sieve (p+2) a

{-# OPTIONS_GHC -O2 #-}
import Data.Int
import Data.Array.Unboxed

main = do
    n <- fmap read getLine
    xs <- fmap (take n . map read . words) getLine :: IO [Int64]
    mapM_ (putStrLn.f.is_square) xs

f True = "YES"
f False = "NO"

is_square n = sq * sq == n
    where sq = floor $ sqrt $ (fromIntegral n::Double)

{-# LANGUAGE FlexibleInstances #-}
module Main (printShit, printShitV, readShit, main) where

-- Meat

solve :: [Integer] -> [String]
solve = map (\x -> if isSquare x && x /= 1 then "YES" else "NO")

isSquare :: Integer -> Bool
isSquare n = sq * sq == n
    where sq = floor $ sqrt (fromIntegral n::Double)

-- Shit
main :: IO ()
main = do
        [_, xs] <- readShit
        printShitV $ solve xs

class Shit a where
    printShit :: a -> IO ()
    printShitV :: a -> IO ()
    readShit :: IO a
    printShitV = printShit

instance  Shit [Int] where
    printShit = putStrLn . unwords . fmap show
    printShitV = putStrLn . unlines . fmap show
    readShit = fmap (fmap read . words) getLine

instance  Shit [Integer] where
    printShit = putStrLn . unwords . fmap show
    printShitV = putStrLn . unlines . fmap show
    readShit = fmap (fmap read . words) getLine

instance Shit [[Int]] where
    printShit = putStrLn . unlines . fmap (unwords . fmap show)
    readShit = fmap (fmap (fmap read . words) . lines) getContents

instance Shit [[Integer]] where
    printShit = putStrLn . unlines . fmap (unwords . fmap show)
    readShit = fmap (fmap (fmap read . words) . lines) getContents

instance Shit [String] where
    printShit = putStrLn. unwords
    printShitV = putStrLn . unlines
    readShit = fmap lines getContents


--
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DatatypeContexts #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- {-# LANGUAGE Strict #-}
{-# LANGUAGE TupleSections #-}
{-# OPTIONS_GHC -O3 -funbox-strict-fields -fvia-C -optc-O3 #-}

import Control.Arrow ((>>>))
import Control.Monad (
    MonadPlus (mplus),
    foldM_,
    forM_,
    guard,
    join,
    replicateM,
    replicateM_,
 )
import qualified Control.Monad as M
import qualified Control.Monad.ST.Strict as ST
import qualified Control.Monad.State.Strict as S
import qualified Control.Monad.Trans.Class as T

import qualified Data.Array as A
import qualified Data.Array.MArray.Safe as MA
import qualified Data.Array.IO.Safe as IOA
import qualified Data.Array.ST.Safe as STA
import qualified Data.Array.Unboxed as UA

import Data.Bits
import qualified Data.ByteString.Char8 as B8
import Data.Char
import Data.Int

import Data.Function (on)
import Data.Functor ((<&>))
import Data.Bifunctor (bimap)
import Data.Maybe (fromJust, fromMaybe, isJust)
import qualified Data.Maybe as MB

import qualified Data.IntMap as IM
import qualified Data.IntSet as IS
import qualified Data.List   as L
import Data.List (
    elemIndices,
    find,
    findIndices,
    intersect,
    isPrefixOf,
    nub,
    sort,
    sortOn,
 )
import qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.Ix as Ix
import qualified Data.Ratio as R

import Control.Monad.Cont (MonadIO (liftIO))
import Data.IORef (modifyIORef, newIORef, readIORef)
import GHC.IO.Handle (hDuplicateTo)
import System.IO (
    IOMode (ReadMode, WriteMode),
    openFile,
    stdin,
    stdout,
    hFlush
 )
import System.Posix.Internals (puts)
import Text.Printf (printf)
import qualified Data.Array.IO.Safe as UOA

-- import Debug.Trace (trace, traceM, traceShowM)
-- debug = flip trace
--
-- tracing t x = trace (t ++ " = " ++ show x) x
-- debugging x t = tracing t x

-- debug x t = x
-- trace t x = x
-- tracing t x = x
-- debugging x t = x

readIntB8 :: B8.ByteString -> Int
readIntB8 = B8.readInt >>> (fst . fromJust)

readIntB8s :: B8.ByteString -> [Int]
readIntB8s = B8.words >>> map (B8.readInt >>> (fst . fromJust))

readIntegerB8 :: B8.ByteString -> Integer
readIntegerB8 = B8.readInteger >>> (fst . fromJust)

readIntegerB8s :: B8.ByteString -> [Integer]
readIntegerB8s = B8.words >>> map (B8.readInteger >>> (fst . fromJust))

modifyArray :: (MA.MArray a e m, MA.Ix i) => a i e -> i -> (e -> e) -> m ()
modifyArray arr ix f = do
    value <- MA.readArray arr ix
    MA.writeArray arr ix $ f value

putsYesNo :: Bool -> IO ()
putsYesNo value = do
    let result :: String =
            if value
                then "YES"
                else "NO"
    printf "%s\n" result

{- END OF GENERAL -}

solve :: Int -> Int -> [Int] -> [(Int, Int)] -> [Bool]
solve n k stations queries = map query' queries
    where
        minStationIndices :: IM.IntMap Int
        minStationIndices = IM.fromListWith min $ L.zip stations [1 .. n]

        maxStationIndices :: IM.IntMap Int
        maxStationIndices = IM.fromListWith max $ L.zip stations [1 .. n]

        query' :: (Int, Int) -> Bool
        query' (from, to) = fromMaybe False $ do
            minFromIndex <- IM.lookup from minStationIndices
            maxToIndex <- IM.lookup to maxStationIndices
            return $ minFromIndex <= maxToIndex

main :: IO ()
main = do
    t <- B8.getLine <&> readIntB8
    replicateM_ t $ do
        B8.getLine
        [n, k] <- B8.getLine <&> readIntB8s
        stations <- B8.getLine <&> readIntB8s
        queries <- replicateM k $ do
            [from, to] <- B8.getLine <&> readIntB8s
            return (from, to)
        let answers = solve n k stations queries
        forM_ answers putsYesNo

{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Control.Monad.State
import Data.Bool
import qualified Data.ByteString.Char8 as C
import Data.Containers.ListUtils
import Data.List
import qualified Data.Map.Lazy as M
import Data.Maybe

solve :: ([Integer], [(Integer, Integer)]) -> [Bool]
solve (us, qs) = map solve' qs
 where
  us' = nubOrd us
  usrev = reverse us
  n = length us

  fs = M.fromList $ zip usrev [n - 1, n - 2 ..]
  ls = M.fromList $ zip us [0 ..]

  solve' (a, b) = case (<=) <$> fs M.!? a <*> ls M.!? b of
    Just True -> True
    _ -> False

main :: IO ()
main = C.interact $ C.unlines . map (output . solve) . runScanner (numberOf input)

input :: Scanner ([Integer], [(Integer, Integer)])
input = do
  n <- int
  k <- int
  us <- n >< integer
  qs <- k >< ((,) <$> integer <*> integer)
  pure (us, qs)

output :: [Bool] -> C.ByteString
output = C.intercalate "\n" . map (bool "NO" "YES")

type Scanner = State [C.ByteString]

runScanner :: Scanner a -> C.ByteString -> a
runScanner = runScannerWith C.words

runScannerWith :: (C.ByteString -> [C.ByteString]) -> Scanner a -> C.ByteString -> a
runScannerWith sp sc = evalState sc . sp

bstr :: Scanner C.ByteString
bstr = get >>= \case s : ss -> put ss >> pure s

read' :: (C.ByteString -> Maybe (a, C.ByteString)) -> Scanner a
read' q = fst . fromJust . q <$> bstr

int :: Scanner Int
int = read' C.readInt

integer :: Scanner Integer
integer = read' C.readInteger

(><) :: Int -> Scanner a -> Scanner [a]
(><) = replicateM

numberOf :: Scanner a -> Scanner [a]
numberOf = (int >>=) . flip (><)

module Main where

import Data.List
import Data.Maybe
import qualified Data.IntMap.Strict as M

main = do
  tStr <- getLine
  let t = read tStr :: Int
  input <- getContents
  let problems = take t $ sep $ lines input
  let sols = problems >>= (map format . solve . parse)
  mapM_ putStrLn sols

sep [] = []
sep (_:x:y:xs) = (y,h):(sep t)
  where (h,t) = splitAt (read $ last $ words x) xs

parse :: (String, [String]) -> ([Int],[(Int,Int)])
parse (y,h) = (us,qs)
  where us = map read $ words y
        qs = map parseQ $ h
        parseQ = first2 . (map read) . words
        first2 (a:b:_) = (a,b)

format b
  | b = "YES"
  | otherwise = "NO"

solve (us,qs) = map (solve1 fs) qs
  where fs = M.fromListWith phi $ zip us (map dupe [0..])
        dupe x = (x,x)
        phi (x1,y1) (x2,y2) = (min x1 x2, max x1 x2)

solve1 fs (a,b)
  | Just (i,_) <- s, Just (_,j) <- t = i < j
  | otherwise = False
  where [s,t] = map (fs M.!?) [a,b]

--
-- Michael V. Antosha
-- 2022
-- Michael.Antosha@gmail.com
--

{-# LANGUAGE Safe                #-}
{-# LANGUAGE ImportQualifiedPost #-}

{-# language NoMonomorphismRestriction #-}

{-# options_ghc -O3                              #-}

{-# options_ghc -Weverything                     #-}
{-# options_ghc -Werror                          #-}

{-# options_ghc -Wno-safe                        #-}     -- no warning if inferred as safe
{-# options_ghc -Wno-all-missed-specialisations  #-}     -- "Could not specialise..." when -O1
{-# options_ghc -Wno-missing-import-lists        #-}
{-# options_ghc -Wno-missing-local-signatures    #-}

import Prelude               hiding (getLine)
import Control.Applicative
import Control.Arrow
import Control.Monad
import Data.ByteString.Char8 qualified as B
import Data.Char
import Data.List             qualified as L
import Data.Map.Strict       qualified as Map
import Data.Maybe

ri = readInts <$> B.getLine :: IO [Int]
  where
    readInts  = L.unfoldr ((second dropSpace <$>) . B.readInt) . dropSpace
    dropSpace = B.dropWhile isSpace

showYN False = "NO"
showYN True  = "YES"

main :: IO ()
main = do
    [t] <- ri
    tcs <- replicateM t $ do
        []      <-              ri
        [_n,k]  <-              ri
        us      <-              ri
        qs      <- replicateM k ri
        return (us, qs)
    mapM_ (putStr . unlines . map showYN . solve) tcs

solve (us, qs) = map reachable qs
  where
    rt = Map.fromListWith minMax . (`zip`dinds) $ us

    (!?) = (Map.!?)
    reachable [a,b] = fromMaybe False $ liftA2 isOrdered (rt!?a) (rt!?b)
    reachable _     = error "Pair expected in query"

    isOrdered (leMin,_leMax) (_riMin,riMax) = (leMin <= riMax)

inds = [0..] :: [Int]
dinds = map (id &&& id) inds

minMax (a,b) (c,d) = (min a c, max b d)


main :: IO ()
main = do
  n <- fmap read getLine
  putStrLn $ unwords $ map show $ take n [n ..]

import Control.Applicative((<$>))

main :: IO ()
main = unwords <$> map show <$> (\x -> take x $ [x..]) <$> read <$> getLine >>= putStrLn

import Data.List (intercalate)

main :: IO ()
main  =  do
    n  <-  readLn
    putStrLn $ intercalate " " $ map show [n + 1 .. 2 * n]

{-# LANGUAGE ScopedTypeVariables #-}

import Text.Printf
import Data.List
import Data.Functor
import Data.Function
import Control.Monad
import Data.Maybe
import qualified Data.Set as Set
import qualified Data.Array.ST.Safe as A
import Control.Monad.ST.Safe
import Prelude
import qualified Data.ByteString.Char8 as B8

readInt :: String -> Int
readInt = Prelude.read

readB8Int :: B8.ByteString -> Int
readB8Int = fst . fromJust . B8.readInt

solve :: Int -> [Int]
solve n = take n [(n + 1) ..]

main :: IO ()
main = do
    n <- readB8Int <$> B8.getLine
    let answer = solve n
    forM_ answer $ printf "%d "

main :: IO ()
main = readLn >>= putStrLn . unwords . map show . solve

solve :: Integer -> [Integer]
solve n = [n .. 2 * n - 1]

main=interact$unwords.map show.f.read
f n=[n..2*n-1]

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns, ViewPatterns, TupleSections #-}

import Control.Applicative
import Control.Monad
import Control.Monad.ST
import Data.Array.Base
import Data.Array.ST (runSTUArray)

main = do
  n <- readLn
  putStr.unwords.map show $ take n primes

primes :: [Int]
primes =  2:[2*i+1|i<-[1.. 10000000 `quot`2], unsafeAt isPrime i]
   where
     !lim = (10000000 + 0xfffff * 0xfffff -1)`quot` 0xfffff * 0xfffff
     !isPrime = runSTUArray $ do
       isp <- newArray (0,lim`quot`2) True :: ST s (STUArray s Int Bool)
       forM_ [0, 0xfffff ..lim - 0xfffff] $ \l-> do
         let !l2 = l`quot`2
             !r  = l + 0xfffff
             !r2 = r`quot`2
         forM_ [1..intSqrt r`quot`2] $ \i-> do
           flg <- unsafeRead isp i
           when flg $ do
             let !p = 2*i+1
                 !ip1=i*(p+1)
                 go !i = when (i<r2) $ do
                   unsafeWrite isp i False
                   go $ i+p
             go $ ((l2-ip1+p-1)`quot`p)*p + ip1
       return isp

intSqrt :: Int -> Int
intSqrt x = floor . sqrt $ fromIntegral x

import Control.Applicative
import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as C

readInt :: C.ByteString -> Int
readInt = fst . fromJust . C.readInt

getInts :: IO [Int]
getInts = map readInt . C.words <$> C.getLine

getInt :: IO Int
getInt = readInt <$> C.getLine

main :: IO ()
main = do
  n <- getInt
  putStrLn $ unwords $ map show $ take n [n..]

#!/usr/bin/env runghc
main = do
  c <- getContents
  putStr . unlines . solve . lines $ c

solve::[String]->[String]
solve (n:ns) = [unwords . map show . take (read n) $ p]
p = 2:f [3] [3,5..]
    where f (x:xs) ys = let (ps, qs) = span (< x^2) ys
                        in  ps ++ f (xs ++ ps) [z | z <- qs, mod z x /= 0]

module Main where

main :: IO ()
main = do
  n <- fmap read getLine :: IO Int
  putStrLn $ rush n

large :: Int
large = 1000000

rush :: Int -> String
rush n = (unwords . map show) [(large - n  + 1)..large]


import Control.Arrow ((>>>))
import Data.List (sort)

main :: IO ()
main =
  interact $
  lines >>>
  drop 1 >>> map (words >>> map read) >>> process >>> map show >>> unlines

process :: [[Int]] -> [Int]
process [] = []
process (_:xs:xss) = solve xs : process xss

solve :: [Int] -> Int
solve = minimum . foldl upd (replicate 201 0) . sort

upd dp x = scanl1 min . zipWith (+) (inf : dp) . map (abs . (x -)) $ [0 ..] {- -}

inf = 10 ^ 9

import Control.Arrow ((>>>))
import Data.List (sort)

main :: IO ()
main =
  interact $
  lines >>>
  drop 1 >>> map (words >>> map read) >>> process >>> map show >>> unlines

process :: [[Int]] -> [Int]
process [] = []
process (_:xs:xss) = solve xs : process xss

solve :: [Int] -> Int
solve = minimum . foldl upd (replicate 201 0) . sort
  where
    upd dp x = scanl1 min . zipWith (+) (inf : dp) . map (abs . (x -)) $ [0 ..]
    inf = 10 ^ 9


{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
import           Control.Applicative
import           Control.Monad
import           Data.List
import           Data.Tuple
import           Data.Int
import           Data.Char
import           Data.Function
import           Data.Array.Unboxed
-- import           Data.ByteString (ByteString)
-- import qualified Data.ByteString.Char8 as C
-- import           Data.ByteString.Char8 ()
-- import           Data.Vector.Unboxed ((//), (++), (!), (!?))
-- import qualified Data.Vector.Unboxed as U
-- import           Data.IntMap.Strict (IntMap)
-- import qualified Data.IntMap.Strict as IntMap
-- import           Data.Sequence ((|>), (<|), (><),ViewR((:>)), ViewL((:<)))
-- import qualified Data.Sequence as S

myread = fromIntegral::Int -> Int64
main = do
  s <- map (myread . digitToInt) <$> getLine
  print $ solve s

solve :: [Int64] -> Int64
solve xs = go (reverse xs) 0 (pred . myread $! length xs) where
  go [x] v _ = v + divisible x
  go (x:y:xs) v l = go (y:xs) (v + divisible x + if (x+10*y)`mod`4==0 then l else 0::Int64) (pred l)
  divisible x = if x`mod`4==0 then 1::Int64 else 0::Int64

import qualified Data.ByteString.Char8 as B
import Control.Applicative
import Data.Char

main = do
    nums' <- B.unpack <$> B.getLine
    let nums = filter isDigit nums'
    let
        l1 :: Integer
        l1 = fromIntegral . length . filter (\c -> c == '0' || c == '4' || c == '8') $ nums
        idxs _ [] = []
        idxs _ [_] = []
        idxs idx (a:b:xs)
            | (read $ [a, b]) `mod` 4 == 0 = idx:(idxs (idx+1) (b:xs))
            | otherwise = idxs (idx+1) (b:xs)
        fidx = idxs 1 nums
        l2 :: Integer
        l2 = sum fidx
    putStrLn . show $ l1+l2

{-
instructions go here...
-}

module Main where
import Data.Char (ord)

solve :: String -> Integer
solve number = fst3 $ foldl aggr (0,0,0) number
  where aggr (total,pos,last2) digit = (total',pos',last2')
          where total' = total + totalInc
                pos' = pos + 1
                totalInc = if divisibleBy4 last2'
                             then   1                 -- the whole number ...xy up to pos'
                                  + max 0 (pos' - 2)  -- all prefixes of the xy number
                                  + if divisibleBy4 digit' && pos' >= 2
                                      then 1 -- number y
                                      else 0
                             else if divisibleBy4 digit'
                               then 1
                               else 0
                last2' = (last2 `mod` 10) * 10 + digit'
                digit' = fromIntegral $ ord digit - ord '0'
        fst3 (x,_,_) = x
        divisibleBy4 x = x `mod` 4 == 0

main :: IO ()
main = do
  number <- getLine
  putStrLn $ show $ solve number

{-
5
58         -> 8
581
5810       -> 0
58104      -> 58104, 8104, 104, 04, 4
581043
5810438    -> 8
58104381
581043817
5810438174 -> 4
-}

{-# OPTIONS_GHC -O2 #-}

import Data.List (tails)
import Data.Char (ord)
import Data.Int (Int64)

main = do
  s <- getLine

  let
    r = sum $ map f $ zip (init $ tails $ reverse s) (reverse [1..length s])
      where
        f :: (String, Int) -> Int64
        f ([a], i) = if (ord a - 48) `mod` 4 == 0 then 1 else 0
        f ((a:b:_), l) =
          (if (ord a - 48) `mod` 4 == 0 then 1 else 0) +
            (if ((ord b - 48) * 10 + ord a - 48) `mod` 4 == 0 then (fromIntegral l) - 1 else 0)

  print r

import Data.List
main=interact$show.f.map ((read::String->Int).(:[])).reverse.head.lines
f::[Int]->Integer
f a=p a 0 (toInteger(length a))
p::[Int]->Integer->Integer->Integer
p [a] b _=if mod a 4 == 0 then (b+1) else b
p (a:b:c) d e=if mod a 4 == 0 then (if mod (a+b*10) 4 == 0 then p (b:c) (d+e) (e-1) else p (b:c) (d+1) (e-1))else(if mod (a+b*10) 4 == 0 then p (b:c) (d+e-1) (e-1) else p (b:c) d (e-1))

import Data.List
main=interact$show.f.map (read.(:[])).reverse.head.lines
f::[Int]->Integer
f a=p a 0 (toInteger(length a))
p [a] b _=if mod a 4 == 0 then (b+1) else b
p (a:b:c) d e=if mod a 4 == 0 then (if mod (a+b*10) 4 == 0 then p (b:c) (d+e) (e-1) else p (b:c) (d+1) (e-1))else(if mod (a+b*10) 4 == 0 then p (b:c) (d+e-1) (e-1) else p (b:c) d (e-1))

import qualified Data.ByteString.Char8 as B
import Control.Applicative

main = interact $ show . sol . head . words

sol nums = l1+l2
    where
    l1 :: Integer
    l1 = fromIntegral . length . filter (\c -> c == '0' || c == '4' || c == '8') $ nums
    idxs _ [] = []
    idxs _ [_] = []
    idxs idx (a:b:xs)
        | (read $ [a, b]) `mod` 4 == 0 = idx:(idxs (idx+1) (b:xs))
        | otherwise = idxs (idx+1) (b:xs)
    fidx = idxs 1 nums
    l2 :: Integer
    l2 = sum fidx

import Data.List
main=interact$show.f.map read .group.reverse.head.lines
f::[Int]->Integer
f a=p a 0 (toInteger(length a))
p::[Int]->Integer->Integer->Integer
p [a] b _=if mod a 4 == 0 then (b+1) else b
p (a:b:c) d e=if mod a 4 == 0 then (if mod (a+b*10) 4 == 0 then p (b:c) (d+e) (e-1) else p (b:c) (d+1) (e-1))else(if mod (a+b*10) 4 == 0 then p (b:c) (d+e-1) (e-1) else p (b:c) d (e-1))

import qualified Data.ByteString.Char8 as B
import Control.Applicative

main = do
    nums <- B.unpack <$> B.getLine
    let
        l1 :: Integer
        l1 = fromIntegral . length . filter (\c -> c == '0' || c == '4' || c == '8') $ nums
        idxs _ [] = []
        idxs _ [_] = []
        idxs idx (a:b:xs)
            | (read $ [a, b]) `mod` 4 == 0 = idx:(idxs (idx+1) (b:xs))
            | otherwise = idxs (idx+1) (b:xs)
        fidx = idxs 1 nums
        l2 :: Integer
        l2 = sum fidx
    putStrLn . show $ l1+l2

import qualified Data.ByteString.Char8 as B
import Control.Applicative

main = do
    nums <- B.unpack <$> B.getLine
    let
        l1 = length . filter (\c -> c == '0' || c == '4' || c == '8') $ nums
        idxs _ [] = []
        idxs _ [_] = []
        idxs idx (a:b:xs)
            | (read $ [a, b]) `mod` 4 == 0 = idx:(idxs (idx+1) (b:xs))
            | otherwise = idxs (idx+1) (b:xs)
        fidx = idxs 1 nums
        l2 = sum fidx
    putStrLn . show $ l1+l2


import Data.Char

fromLeft ('1':rest) = '1':(fromLeft rest)
fromLeft ('0':rest) = '1':rest

update :: String -> Char -> String
update roomstates 'L'   = fromLeft roomstates
update roomstates 'R'   = reverse . fromLeft . reverse $ roomstates
update roomstates charN = leftPart ++ ['0'] ++ rightPart
                            where
                                n = ord(charN) - ord('0')
                                (splitedl, splitedr) = splitAt n roomstates
                                rightPart = tail splitedr
                                leftPart = splitedl

main :: IO ()
main = do
    getLine
    eventSeries <- getLine
    putStrLn $ foldl update "0000000000" eventSeries

import Data.List

data Event = EnterLeft | EnterRight | Leave Int
data RoomStatus = Free | Occupied deriving (Eq)
type HotelStatus = [RoomStatus]

instance Show RoomStatus where
    show Free = "0"
    show Occupied = "1"

showHotelStatus :: HotelStatus -> String
showHotelStatus [] = ""
showHotelStatus (x:xs) = (show x) ++ (showHotelStatus xs)

getEvent :: Char -> Event
getEvent ch
    | ch == 'L' = EnterLeft
    | ch == 'R' = EnterRight
    | otherwise = Leave (read [ch])

tryToOccupy :: (RoomStatus, Bool) -> (RoomStatus, Bool)
tryToOccupy original@(status, needToOccupy)
    | status == Free && needToOccupy = (Occupied, False)
    | otherwise = original

change :: Event -> HotelStatus -> HotelStatus
change EnterLeft status =
    let pairs = scanl (\(_, needed) roomStatus -> tryToOccupy (roomStatus, needed)) (Occupied, True) status
    in map fst (tail pairs)
change EnterRight status =
    let pairs = scanr (\roomStatus (_, needed) -> tryToOccupy (roomStatus, needed)) (Occupied, True) status
    in map fst (init pairs)
change (Leave roomNumber) status = map (\(num, roomStatus) -> if num == roomNumber then Free else roomStatus) (zip [0..] status)

solve :: [Event] -> HotelStatus
solve events =
    let hotelStatus = take 10 (repeat Free)
    in foldl (\status event -> change event status) hotelStatus events

main = do
    getLine
    events <- (map getEvent) <$> getLine
    putStrLn . showHotelStatus . solve $ events

import Control.Applicative
import Control.Monad
import Data.List
import Data.Maybe
import Data.Array
import Data.Char

updl x = (takeWhile (==1) x) ++[1] ++ (tail (dropWhile (==1) x))
updr x = reverse $ updl $ reverse x
upd x a  = (take a1 x) ++ [0] ++ (drop (a1+1) x)
	where a1 = digitToInt a

process x [] = x
process x (a:as) | a=='L' = process (updl x) as
                 | a=='R' = process (updr x) as
		 | otherwise = process (upd x a) as

main = do
		n<-getLine
		x<-getLine
		putStrLn $ concat $ map show $ process [0,0,0,0,0,0,0,0,0,0] x


{-# LANGUAGE LambdaCase #-}

import Control.Monad (foldM, forM, forM_, replicateM)
import Control.Monad.ST (ST, runST)
import Control.Monad.State (State, evalState, get, put)
import Data.Array.ST (STUArray, newArray, readArray, writeArray)
import qualified Data.ByteString.Lazy.Char8 as C
import Data.List (groupBy, sort, sortBy)
import Data.Maybe (fromJust)
import Data.Ord (comparing)

main :: IO ()
main = C.interact $ runScanner (C.pack <$> testCase)

testCase :: Scanner String
testCase = do
  n <- int
  k <- int
  cs <- (n * k) >< int
  return . unlines $ do
    (a, b) <- solve n k cs
    return $ show a ++ " " ++ show b

solve :: Int -> Int -> [Int] -> [(Int, Int)]
solve n k =
  map (\(l, r, _) -> (l, r))
    . sortOn thd3
    . pick
    . sortOn snd3
    . map (\((c, l), (_, r)) -> (l, r, c))
    . concatMap adjacents
    . groupOn fst
    . sort
    . (`zip` [1 ..])
  where
    nk = n * k
    fmax = 1 + (n - 1) `div` (k - 1)
    pick xs = runST $ do
      fs <- newArray (1, nk) 0 :: ST s (STUArray s Int Int)
      cs <- newArray (1, n) False :: ST s (STUArray s Int Bool)
      foldM' [] xs $ \ranges (l, r, c) -> do
        done <- readArray cs c
        mf <- maximum <$> forM [l .. r] (readArray fs)
        if not done && mf + 1 <= fmax
          then do
            writeArray cs c True
            forM_ [l .. r] $ \i -> do
              v <- readArray fs i
              writeArray fs i (v + 1)
            return $ (l, r, c) : ranges
          else do
            return ranges

adjacents :: [a] -> [(a, a)]
adjacents xs = zip xs (tail xs)

groupOn :: Eq b => (a -> b) -> [a] -> [[a]]
groupOn p = groupBy (\u v -> p u == p v)

sortOn :: Ord b => (a -> b) -> [a] -> [a]
sortOn = sortBy . comparing

foldM' :: (Foldable t, Monad m) => b -> t a -> (b -> a -> m b) -> m b
foldM' b ta f = foldM f b ta

fst3 :: (a, b, c) -> a
fst3 (a, _, _) = a
snd3 :: (a, b, c) -> b
snd3 (_, b, _) = b
thd3 :: (a, b, c) -> c
thd3 (_, _, c) = c

-------------------------- Template ------------------------------------------
type Scanner = State [C.ByteString]

runScanner :: Scanner a -> C.ByteString -> a
runScanner = runScannerWith C.words

runScannerWith :: (C.ByteString -> [C.ByteString]) -> Scanner a -> C.ByteString -> a
runScannerWith t s = evalState s . t

bstr :: Scanner C.ByteString
bstr = get >>= \case s : ss -> put ss >> return s

int :: Scanner Int
int = fst . fromJust . C.readInt <$> bstr

times, (><) :: Int -> Scanner a -> Scanner [a]
times = replicateM
(><) = times

{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE Strict #-}

import Control.Applicative (liftA2)
import Control.Monad (foldM, foldM_, forM, forM_, guard, replicateM, unless, when)
import Control.Monad.ST (ST, runST)
import Control.Monad.State (State, evalState, get, gets, put)
import Data.Array.ST (STUArray, newArray, readArray, writeArray)
import qualified Data.ByteString.Lazy.Char8 as C
import Data.List (groupBy, sort, sortBy)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Debug.Trace (trace)

main :: IO ()
main = C.interact $ runScanner (C.pack <$> testCase)

testCase :: Scanner String
testCase = do
  n <- int
  k <- int
  cs <- (n * k) >< int
  return . unlines $ do
    (a, b) <- solve n k cs
    return $ show a ++ " " ++ show b

solve :: Int -> Int -> [Int] -> [(Int, Int)]
solve n k =
  map (\(l, r, _) -> (l, r))
    . sortOn thd3
    . pick
    . sortOn snd3
    . map (\((c, l), (_, r)) -> (l, r, c))
    . concatMap adjacents
    . groupOn fst
    . sort
    . (`zip` [1 ..])
  where
    nk = n * k
    fmax = 1 + (n - 1) `div` (k - 1)
    pick xs = runST $ do
      fs <- newArray (1, nk) 0 :: ST s (STUArray s Int Int)
      cs <- newArray (1, n) False :: ST s (STUArray s Int Bool)
      foldM' [] xs $ \ranges (l, r, c) -> do
        done <- readArray cs c
        mf <- maximum <$> forM [l .. r] (readArray fs)
        if not done && mf + 1 <= fmax
          then do
            writeArray cs c True
            forM_ [l .. r] $ \i -> do
              v <- readArray fs i
              writeArray fs i (v + 1)
            return $ (l, r, c) : ranges
          else do
            return ranges

adjacents xs = zip xs (tail xs)
groupOn p = groupBy (\u v -> p u == p v)
sortOn = sortBy . comparing

foldM' b ta f = foldM f b ta

fst3 (x, _, _) = x
snd3 (_, y, _) = y
thd3 (_, _, z) = z

debug a = trace (show a) a

-------------------------- Template ------------------------------------------
type Scanner = State [C.ByteString]

runScanner :: Scanner a -> C.ByteString -> a
runScanner = runScannerWith C.words

runScannerWith :: (C.ByteString -> [C.ByteString]) -> Scanner a -> C.ByteString -> a
runScannerWith t s = evalState s . t

peek :: Scanner C.ByteString
peek = gets head

bstr :: Scanner C.ByteString
bstr = get >>= \case s : ss -> put ss >> return s

str :: Scanner String
str = C.unpack <$> bstr

int :: Scanner Int
int = fst . fromJust . C.readInt <$> bstr

integer :: Scanner Integer
integer = read . C.unpack <$> bstr

double :: Scanner Double
double = read . C.unpack <$> bstr

decimal :: Int -> Scanner Int
decimal p = round . ((10 ^ p) *) <$> double

numberOf :: Scanner a -> Scanner [a]
numberOf s = int >>= flip replicateM s

many :: Scanner a -> Scanner [a]
many s = get >>= \case [] -> return []; _ -> (:) <$> s <*> many s

till :: (C.ByteString -> Bool) -> Scanner a -> Scanner [a]
till p s = do
  t <- peek
  if p t
    then return []
    else (:) <$> s <*> till p s

times :: Int -> Scanner a -> Scanner [a]
times = replicateM

(><) = times

two, three, four :: Scanner a -> Scanner [a]
[two, three, four] = map times [2 .. 4]

pair :: Scanner a -> Scanner b -> Scanner (a, b)
pair = liftA2 (,)

import Control.Monad
import Control.Monad.State
import Data.Array
import Data.List
import qualified Data.IntMap as IM
import qualified Data.IntSet as IS
import qualified Data.Sequence as Seq

solve :: IO ()
solve = do
    [n, k] <- readMany :: IO [Int]
    xa <- listArray (1, n * k) <$> readMany
    let lim = (n + k - 2) `div` (k - 1)
        go :: IS.IntSet -> Int -> IM.IntMap Int -> State (Seq.Seq Int) [(Int, Int, Int)]
        go pend i prv
            | IS.null pend          = return []
            | i == n * k + 1        = go pend 1 IM.empty
            | x `IS.notMember` pend = go pend (i + 1) prv
            | otherwise             = go' $ x `IM.lookup` prv
          where
            x = xa!i
            prv' = IM.insert x i prv
            go' Nothing  = go pend (i + 1) prv'
            go' (Just j) = do
                m <- gets $ maximum . (<$> [j..i]) . Seq.index
                if m >= lim
                    then go pend (i + 1) prv'
                    else do
                        mapM_ (modify . Seq.adjust' (+1)) [j..i]
                        ((x, j, i):) <$> go (x `IS.delete` pend) (i + 1) prv
        rs = evalState (go (IS.fromList [1..n]) 1 IM.empty) $ Seq.fromFunction (n * k + 1) $ const 0
    putStr $ unlines $ map (\(_, i, j) -> show i ++ " " ++ show j) $ sort rs

readMany :: Read a => IO [a]
readMany = map read . words <$> getLine

main :: IO ()
main = solve

import qualified Data.ByteString.Lazy.Char8 as P
import qualified Data.ByteString.Builder as B
import System.IO

import Control.Monad
import Control.Monad.Trans.State
import Control.Monad.Trans.Class
import Data.List
import Data.Semigroup

import Data.Array.Unboxed
import Data.Function

ceilDiv n k = div (n + k - 1) k

main :: IO ()
main = (P.getContents >>=) . evalStateT $ do
  [n, k] <- readInts
  c <- readInts
  let
    cr :: Array Int (UArray Int Int)
    cr = fmap (listArray (1, k) . ($ [])) $ accumArray (.) id (1, n) $ do
      (i, ci) <- zip [1 ..] c
      pure (ci, (i :))
    chunkSize = ceilDiv n (k - 1)
    extract :: ([Int], [Int]) -> Int -> ([Int], [Int])
    extract (_, is) j = splitAt chunkSize $ sortOn (\i -> cr ! i ! j) is

    vals :: [[Int]]
    vals = map fst $ tail $ scanl extract ([], [1 .. n]) [2 .. k]
    ans :: Array Int (Int, Int)
    ans = array (1, n) $ do
      (j, is) <- zip [2 ..] vals
      i <- is
      pure (i, (cr ! i ! (j - 1), cr ! i ! j))
  forM_ (elems ans) $ \(ai, bi) -> putInts [ai, bi]

type SIO = StateT P.ByteString IO

readLine :: SIO P.ByteString
readLine = do
  (out, next) <- P.break (<= '\r') <$> get
  put $ P.dropWhile (<= '\r') next
  pure out

readInts :: SIO [Int]
readInts = do
  currLine <- readLine
  pure [x | Just (x, _) <- P.readInt <$> P.words currLine]

putInts :: [Int] -> SIO ()
putInts li = let
  outBuilder = foldr (<>) (B.char7 '\n')
    $ intersperse (B.char7 ' ') (map B.intDec li)
  in  lift $ B.hPutBuilder stdout outBuilder


import qualified Data.List as L
import qualified Data.Map as M
import qualified Data.Set as S

import qualified Data.Array as A

import qualified Control.Monad.State.Strict as State
import Control.Monad.ST

import Data.Bits(xor, (.&.), (.|.))
import qualified Data.Array.MArray as MA
import Data.Array.ST(runSTArray)

import Data.Array((!))
import Control.Monad(replicateM, foldM)
import Data.Maybe

numOcc :: Ord a => [a] -> M.Map a Integer
numOcc (x:xs) = let m = numOcc xs in case M.lookup x m of
	Just i -> M.insert x (i+1) m
	Nothing -> M.insert x 1 m
numOcc [] = M.empty

numOcc' :: A.Ix a => (a, a) -> [a] -> A.Array a Integer
numOcc' size list = runSTArray $ do
	array <- MA.newArray size 0
	let addElem action a = do
		i <- MA.readArray array a
		MA.writeArray array a (i+1)
	foldM addElem () list
	return $ array

insertWithList :: Ord a => a -> b -> M.Map a [b] -> M.Map a [b]
insertWithList a b m = case M.lookup a m of
	Just l -> M.insert a (b:l) m
	Nothing -> M.insert a [b] m

deleteWithList :: Ord a => a -> M.Map a [b] -> M.Map a [b]
deleteWithList a m = case M.lookup a m of
	Just (x:y:xs) -> M.insert a (y:xs) m
	Just [x] -> M.delete a m

solve :: Integer -> Integer -> [Integer] -> [Integer] -> Integer
solve na nb a b = div result 2 where
	deg_a = numOcc' (0, na) a

	deg_b = numOcc' (0, nb) b

	n = toInteger $ length a

	edges = zip a b

	sum_deg_b = foldr (+) 0 deg_b

	result = sum $ fmap (\(a, b) -> sum_deg_b + 1 - (deg_a ! a) - (deg_b ! b)) edges

plotResult i = print i

main = do
	t <- getLine >>= return . read :: IO Integer
	replicateM (fromInteger t) $ do
		[na, nb, _] <- fmap ((fmap read) . words) getLine :: IO [Integer]
		a <- fmap ((fmap read) . words) getLine :: IO [Integer]
		b <- fmap ((fmap read) . words) getLine :: IO [Integer]
		plotResult $ solve na nb a b

-- Trying to use only ByteString I/O with a state monad this contest

import Control.Monad
import Control.Monad.Trans.State.Lazy
import qualified Data.ByteString.Lazy.Char8 as P

import Data.Int
import Data.List

getSLine :: Monad m => StateT P.ByteString m P.ByteString
getSLine = do
  (out, next) <- P.break (== '\n') <$> get
  put $ P.tail next
  pure out

readInts :: Monad m => StateT P.ByteString m [Int]
readInts = do
  currLine <- getSLine
  pure [x | Just (x, _) <- P.readInt <$> P.words currLine]

-- There has to be a combinator for this. But where?
outLine :: P.ByteString -> StateT P.ByteString IO ()
outLine v = StateT $ \s -> ((), s) <$ P.putStrLn v

main :: IO ()
main = (P.getContents >>=) $ evalStateT $ do
  [t] <- readInts
  replicateM_ t $ do
    [a, b, k] <- readInts
    ai <- readInts
    bi <- readInts
    let
      nc2 :: Int -> Int64
      nc2 x = let  y = fromIntegral x  in  div (y * (y-1)) 2
      excl :: [Int] -> Int64
      excl li = sum $ [nc2 $ length s | s <- group $ sort li]
      ans :: Int64
      ans = nc2 k - sum (excl <$> [ai, bi])
    outLine . P.pack $ show ans


import Data.List

main = do
  t <- getLine
  p <- getLine
  a <- fmap (map read . words) getLine
  print $ f 0 (length t - length p) (zip [1..] t) p (sort $ zip a [1..])

f l r _ _ _ | l == r = l
f l r t p a =
  if h t' p
  then f m r t p a
  else f l (m - 1) t p a
  where m = div (l + r + 1) 2
        t' = g t a m

g t [] _ = t
g ((i,j):t) ((x,y):a) m | y > m  = g ((i,j):t) a m
                        | i == x = g t a m
                        | i < x  = (i,j):g t ((x,y):a) m
                        | i > x  = (i,j):g t a m

h _ [] = True
h [] _ = False
h ((_,j):t) (p:q) | p == j = h t q
                  | p /= j = h t (p:q)

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns      #-}
{-# LANGUAGE CPP               #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Safe              #-}
{-# LANGUAGE TupleSections     #-}

import           Control.Applicative
import           Control.Monad
import           Control.Monad.State.Strict
import           Data.Array.Unboxed
import           Data.Bits
import           Data.Bool
import qualified Data.ByteString.Char8      as B
import           Data.Char
import           Data.Function
import           Data.Int
import qualified Data.IntMap.Strict         as IM
import qualified Data.IntSet                as IS
import           Data.List
import           Data.List.Split
import qualified Data.Map.Strict            as M
import           Data.Monoid
import           Data.Ord
import qualified Data.Set                   as S
import           Data.Tuple
import           Data.Word

readInt :: B.ByteString -> Int
readInt bs=case B.readInt bs of{Just(n,_)->n;_->error$"readInt error : bs = "++show bs;}
readInteger :: B.ByteString -> Integer
readInteger bs=case B.readInteger bs of{Just(n,_)->n;_->error$"readInteger error : bs = "++show bs;}

main :: IO ()
main = do
  t <- getLine
  p <- getLine
  ps <- map readInt.B.words <$> B.getLine
  print $ solve t p ps

solve :: String -> String -> [Int] -> Int
solve t p ps = upperBound predicate 0 (length t)
  where
    predicate i = go 1 t p
      where
        !invalid = IS.fromList $ take i ps
        go i (x:xs) (y:ys)
          | x == y && IS.notMember i invalid = go (i+1) xs ys
          | otherwise = go (i+1) xs (y:ys)
        go _ _ [] = True
        go _ _ _ = False

upperBound :: (Integral i) => (i -> Bool) -> i -> i -> i
upperBound p low high = go low high
   where
     go !low !high
       | high <= low = low
       | p mid       = go mid high
       | otherwise   = go low (mid - 1)
      where
        mid = (low + high + 1) `quot` 2
{-# INLINE upperBound #-}


import Data.List
import Data.Array

main = do
    [n, m, k] <- fmap (map (read::String->Int) . words) getLine
    v <- fmap (map (read::String->Int) . words) getLine

    let ans = binarySearch m k v n (-1)
    putStrLn $ show $ n-ans

binarySearch:: Int -> Int -> [Int] -> Int -> Int -> Int
binarySearch m k v l r
    | l == r+1 = l
    | otherwise = if pack m k (drop mid v)
                      then binarySearch m k v mid r
                      else binarySearch m k v l mid
                  where mid = (l + r) `div` 2

pack:: Int -> Int -> [Int] -> Bool
pack m k v = (snd $ foldl' f (k, m-1) v) >= 0
    where f (x,y) z
              | z > k = (0, -1)
              | z > x = (k-z, y-1)
              | otherwise = (x-z, y)

main = do
    [n, m, k] <- fmap (map (read::String->Int) . words) getLine
    v <- fmap (map (read::String->Int) . words) getLine

    let ans = binarySearch m k v n (-1)
    putStrLn $ show $ n-ans

binarySearch:: Int -> Int -> [Int] -> Int -> Int -> Int
binarySearch m k v l r
    | l == r+1 = l
    | otherwise = if pack m k (drop mid v)
                      then binarySearch m k v mid r
                      else binarySearch m k v l mid
                  where mid = (l + r) `div` 2

pack:: Int -> Int -> [Int] -> Bool
pack m k v = (snd $ foldl f (k, m-1) v) >= 0
    where f (x,y) z
              | z > k = (0, -1)
              | z > x = (k-z, y-1)
              | otherwise = (x-z, y)


import Control.Monad (forM_)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8
import Data.List (sort)
import Data.Maybe (fromJust)

readInt = fst . fromJust . BS8.readInt

solve :: [Int] -> Int
solve xs@(x : _) = go xs 0 x
  where
    go (x : xs) delta best = do
      let y = x + delta
      go xs (delta - y) (max best y)
    go [] delta best = best
solve [] = -1

main = do
  n <- readInt <$> BS.getLine
  forM_ [1 .. n] $ \_ -> do
    _ <- BS.getLine
    xs <- sort . map readInt . BS8.words <$> BS.getLine
    print $ solve xs

import Control.Monad (replicateM_)
import qualified Data.ByteString.Char8 as B
import Data.List (sort)
import Data.Maybe (fromJust)

main :: IO ()
main = do
  t <- fst . fromJust . B.readInt <$> B.getLine
  replicateM_ t $ do
    _ <- B.getLine
    a <- sort . map (fst . fromJust . B.readInt) . B.words <$> B.getLine :: IO [Int]
    print $ maximum $ zipWith (-) a (0 : a)

import Control.Monad
import Data.Int
import Data.List
import Data.Maybe
import qualified Data.ByteString.Char8 as C

solve :: IO ()
solve = do
    ~[n] <- readInts
    xs <- (0:) . sort <$> readInts
    print $ maximum $ zipWith (-) (tail xs) xs

readInts :: IO [Int]
readInts = map (fst . fromJust . C.readInt) . C.words <$> C.getLine

main :: IO ()
main = flip replicateM_ solve . head =<< readInts

import Control.Monad
import Data.Int
import Data.List
import Data.Maybe
import qualified Data.ByteString.Char8 as C

import Debug.Trace

solve :: IO ()
solve = do
    ~[n] <- readInts
    xs <- readInt64s
    let go [_]          best = best
        go (x:xs@(y:_)) best = go xs $! (y - x) `max` best
    print $ go (0 : sort xs) minBound

readInts :: IO [Int]
readInts = map (fst . fromJust . C.readInt) . C.words <$> C.getLine

readInt64s :: IO [Int64]
readInt64s = map (fromInteger . fst . fromJust . C.readInteger) . C.words <$> C.getLine

main :: IO ()
main = flip replicateM_ solve . head =<< readInts

import Control.Monad (replicateM_)
import qualified Data.ByteString.Char8 as B
import Data.List (sort)
import Data.Maybe (fromJust)

main :: IO ()
main = do
  t <- read <$> getLine
  replicateM_ t $ do
    _ <- getLine
    a <- sort . map (fst . fromJust . B.readInt) . B.words <$> B.getLine :: IO [Int]
    print $ maximum $ zipWith (-) a (0 : a)


{-# OPTIONS_GHC -O2 #-}

import qualified Data.ByteString.Char8 as DBC
import qualified Data.ByteString.Builder as DBB
import qualified Control.Monad as CM
import qualified Data.List as DL
import Debug.Trace
import System.IO

trisum :: Integral a => a -> a
trisum r = r*(r+1)`div`2

solve :: IO ()
solve = do
  ~[k,x] <- readv
  let ans = binarySearch (exhaust k $ min x $ trisum $ 2*k-1) 0 (2*k-1)
  print ans

exhaust :: Integer -> Integer -> Integer -> Bool
exhaust k x m = let -- do m messages use up at least x emotes
  total = if m<=k then trisum m else trisum k + trisum (k-1) - trisum (2*k-1-m)
  in
    --traceShow (k,x,m,total) $
    total >= x

binarySearch :: (Integer -> Bool) -> Integer -> Integer -> Integer
binarySearch can lo hi   -- Assumption: f <$> [lo,hi] === [False,True]. Suggestion: Start with hi = n+1
  | lo + 1 == hi = hi
  | otherwise = if can mid then binarySearch can lo mid else binarySearch can mid hi
                where  mid = lo + (hi-lo) `div` 2

printv :: [Int] -> IO ()
printv xs = DBB.hPutBuilder stdout $ mconcat [DBB.intDec x <> DBB.char8 ' ' | x <- xs] <> DBB.char8 '\n'

readv :: IO [Integer]
readv = map (maybe 0 fst . DBC.readInteger) <$> (DBC.words <$> DBC.getLine)

main :: IO ()
main = do
  --hSetBuffering stdout NoBuffering
  ~[nTc] <- readv
  CM.replicateM_ (fromIntegral nTc) solve

import Data.List (delete)

main :: IO ()
main = interact processInput

processInput :: String -> String
processInput input = output
    where
        tests = drop 1 $ lines input
        answers = map processTest tests
        output = unlines answers

processTest :: String -> String
processTest input = output
    where
        [triangle_height, emotes_before_ban] = map read $ words input
        result = howManyMessagesCanSendBeforeBan triangle_height emotes_before_ban
        output = show result

howManyMessagesCanSendBeforeBan :: Integer -> Integer -> Integer
howManyMessagesCanSendBeforeBan triangle_height emotes_before_ban = result_messages_n
    where
        result_messages_n = binarySearch 0 (2 * triangle_height - 1)
        binarySearch l r
            | l == r
                = mid
            | emotesNumberByMessagesNumber mid triangle_height >= emotes_before_ban
                = binarySearch l mid
            | otherwise
                = binarySearch (mid + 1) r
            where
                mid = (l + r) `div` 2

emotesNumberByMessagesNumber :: Integer -> Integer -> Integer
emotesNumberByMessagesNumber messages_n triangle_height = emotes_n
    where
        emotes_n = if messages_n < triangle_height
                   then messages_n * (messages_n + 1) `div` 2
                   else max_emotes_n - messages_before_max * (messages_before_max + 1) `div` 2
        max_emotes_n = triangle_height * (triangle_height + 1) `div` 2 * 2 - triangle_height
        messages_before_max = 2 * triangle_height - 1 - messages_n


import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C

import Data.Maybe
import Control.Monad
import Data.List (nub, reverse, group, sort)
import Data.Array

--solve :: Int -> Int -> Int
solve n s as
    | sum as < s = -1
    | sum as == s = 0
    | otherwise =
        let sm  = sum as
            psL = array (0, n - 1) $ zip [0..n-1] (scanl1 (+) as)
            -- psL = scanl1 (+) as
            -- psR = scanl1 (+) (reverse as)
            psR = array (0, n - 1) $ zip [0..n-1] (scanl1 (+) (reverse as))
        in minimum $ map (test sm s as psL psR) [0..length as]
        -- Сумму можно достичь
        -- 1. Перебрать l
        -- 2. Подобрать оптимальную r
        -- 3. Выбрать лучший ответ среди всех переборов

-- Перебор
--test :: Int -> [Int] -> [Int] -> Int -> Int
test sm s as psL psR cntL
    | cntL == 0 =
        -- нужно взять 0 элементов слева
        let idxR = binarySearch psR (sm - s)
        in idxR + 1
    | otherwise =
        -- я взял cntL элементов слева
        -- теперь мне нужно взять какое-то количество элементов справа
        let idxR = binarySearch psR (sm - s - psL!(cntL - 1))
        in cntL + idxR + 1

-- Search in xs index i such that (xs !! i) >= x
-- lowerBound :: [Int] -> Int -> Int
binarySearch xs x
    | x <= 0 = -1 -- то есть ничего брать не нужно
    | xs ! 0 >= x = 0
    | otherwise = helper xs x 0 (length xs)
    where
        helper xs x l r
            | l == r - 1   = r
            | xs ! mid < x = helper xs x mid r
            | otherwise    = helper xs x l mid
            where mid = div (l + r) 2

read = do
    s <- C.getLine

    let a = map (fst . fromJust . C.readInt) (C.words s)

    s <- C.getLine

    print $ solve (a !! 0) (a !! 1) (map (fst . fromJust . C.readInt) (C.words s))

main = do
   t <- C.getLine

   replicateM_ (fst $ fromJust $ C.readInt t) Main.read

import Data.Array (Array, listArray, (!))

readArr :: IO [Int]
readArr = (map (\s -> read s :: Int)) <$> (words <$> getLine)

calcSmH :: [Int] -> Int -> [Int]
calcSmH [] _ = []
calcSmH (0: xs) i = calcSmH xs (i + 1)
calcSmH (1: xs) i = (i: calcSmH xs (i + 1))

calcSm :: [Int] -> [Int]
calcSm a = (0: calcSmH a 1)

calcH :: Array Int Int -> Array Int Int -> Int -> Int -> Int
calcH p s 0 j = p ! 0 + s ! j
calcH p s i j = min (p ! i + s ! j) (calcH p s (i - 1) (j + 1))

calc :: Array Int Int -> Array Int Int -> Int -> Int
calc p s d = calcH p s d 0

solve :: Int -> IO ()
solve 0 = pure ()
solve i = do
  (n: k: _) <- readArr
  a <- readArr
  let s = sum a
  let (pr, sf) = (listArray (0, s - 1) (calcSm a), listArray (0, s - 1) (calcSm $ reverse a))
  let d = s - k
  if d < 0
    then print $ -1
    else print $ calc pr sf d
  solve $ i - 1

main :: IO ()
main = do
  n <- readLn :: IO Int
  solve n

import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C

import Data.Maybe
import Control.Monad
import Data.List (nub, reverse, group, sort)
import Data.Array

solve :: Int -> Int -> [Int] -> Int
solve n s as
    | sum as < s = -1
    | sum as == s = 0
    | otherwise =
        let sm  = sum as
            psL = array (0, n - 1) $ zip [0..n-1] (scanl1 (+) as)
            psR = array (0, n - 1) $ zip [0..n-1] (scanl1 (+) (reverse as))
        in minimum $ map (test sm s psL psR) [0..length as]

test :: (Ord a, Num a) => a -> a -> Array Int a -> Array Int a -> Int -> Int
test sm s psL psR cntL
    | cntL == 0 =
        let idxR = binarySearch psR (sm - s)
        in idxR + 1
    | otherwise =
        let idxR = binarySearch psR (sm - s - psL!(cntL - 1))
        in cntL + idxR + 1

binarySearch :: (Ord a, Num a) => Array Int a -> a -> Int
binarySearch xs x
    | x <= 0 = -1
    | xs ! 0 >= x = 0
    | otherwise = helper xs x 0 (length xs)
    where
        helper xs x l r
            | l == r - 1   = r
            | xs ! mid < x = helper xs x mid r
            | otherwise    = helper xs x l mid
            where mid = div (l + r) 2

read :: IO()
read = do
    s <- C.getLine

    let a = map (fst . fromJust . C.readInt) (C.words s)

    s <- C.getLine

    print $ solve (a !! 0) (a !! 1) (map (fst . fromJust . C.readInt) (C.words s))

main :: IO()
main = do
   t <- C.getLine

   replicateM_ (fst $ fromJust $ C.readInt t) Main.read


solve :: String -> Int
solve s  =  maximum d - minimum d
  where
    d  =  scanl f 0 s
    f x '-'  =  x - 1
    f x '+'  =  x + 1

main :: IO ()
main  =  getLine >>= print . solve

main=interact$show.f 0 0 0
f l h cur ('+':rest) = f l (max(cur+1)h) (cur+1) rest
f l h cur ('-':rest) = f (min(cur-1)l) h (cur-1) rest
f l h _ _ = h+abs l

solve :: String -> Int
solve  =  maximum . map abs . scanl f 0
  where
    f x '-'  =  x - 1
    f x '+'  =  x + 1

main :: IO ()
main  =  getLine >>= print . solve


{-# OPTIONS_GHC -O2 -optc-O3 -optc-ffast-math -funfolding-use-threshold=16 -fexcess-precision -funbox-strict-fields #-}
module Main where
import qualified Data.ByteString.Char8 as B
import Data.Int
import Data.List

main = do
  [n, k] <- fmap readInts B.getLine
  B.putStr $ B.unwords $ map (B.pack . show) $ solve n k

solve n k
  | s > n = [-1]
  | otherwise = (\g -> let s' = n `div` g in map (*g) $ [1,2..k-1] ++ [s' - k * (k - 1) `div` 2]) $ maybe undefined id $ find (\i -> n `rem` i == 0) $ reverse $ takeWhile (<= n `div` s) $ divs n
  where s = k  * (k + 1) `div` 2 :: Integer

divs x = sort $ concatMap (\i -> [i, x `div` i]) $ filter (\i -> x `rem` i == 0) [1..1 + floor (sqrt $ fromIntegral x)]

readInt = maybe undefined (fromIntegral . fst) . B.readInteger
readInts = fmap readInt . B.words

import Control.Monad
import Control.Applicative
import Text.Printf

solve :: Integer -> Integer -> [Integer]
solve n k
 | kk2 > n = [-1]
 | otherwise = map ((*) a) [1..k-1] ++ [ee]
        where kk2 = k*(k+1)`div`2
              lim = n`div`kk2
              a = run 1 0
              run i acc
               | i > lim = acc
               | i*i > n = acc
               | n`mod`i == 0 = let other = n`div` i
                                    in run (i+1) (max i (if (other <= lim) then max other acc else acc))
               | otherwise = run (i+1) acc
              ee = n - (k*(k-1))`div`2*a

main :: IO ()
main = do
        (n:k:_) <- map read . words <$> getLine
        forM_ (solve n k) $ \i -> do
            printf "%d " i
        printf "\n"

{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE ViewPatterns, ScopedTypeVariables #-}

import qualified Data.ByteString as B (ByteString, getLine)
import qualified Data.ByteString.Char8 as C (readInteger, words)
import qualified Data.Maybe as M (fromJust)
import qualified Data.Int as I (Int64)
import qualified Data.List as L (intercalate)

readInt :: B.ByteString -> I.Int64
readInt = fromIntegral . fst . M.fromJust . C.readInteger

run n sk 0 = -1
run n sk i | n `mod` i == 0 && n `div` i >= sk = i | otherwise = run n sk (i - 1)

runx n sk i | i > mnx = -1 where mnx = min n 100000
runx n sk i | r == 0 && n `mod` d == 0 && n `div` d >= sk = d
            | otherwise = runx n sk (i + 1) where
    (d, r) = n `divMod` i

main = do
    (map readInt . C.words -> [n, k]) <- B.getLine
    let sk = (1 + k) * k `div` 2
        gh = max (runx n sk 1) (run n sk (min n 100000))
        gi = map (gh *) ([1..k - 1] ++ [n `div` gh - sk + k])
    putStrLn $ if k > 200000 || gh == -1 then "-1" else (L.intercalate " " . (map show)) gi

{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE ViewPatterns, ScopedTypeVariables #-}

import qualified Data.ByteString as B (ByteString, getLine)
import qualified Data.ByteString.Char8 as C (readInteger, words)
import qualified Data.Maybe as M (fromJust)
import qualified Data.Int as I (Int64)
import qualified Data.List as L (intercalate)

readInt :: B.ByteString -> I.Int64
readInt = fromIntegral . fst . M.fromJust . C.readInteger

run n sk 0 = -1
run n sk i | n `mod` i == 0 && n `div` i >= sk = i | otherwise = run n sk (i - 1)

runx n sk i | i > min (n - 1) 100000 = -1
runx n sk i | n `mod` i == 0 && n `mod` (n `div` i) == 0 && n `div` (n `div` i) >= sk = n `div` i | otherwise = runx n sk (i + 1)

main = do
    (map readInt . C.words -> [n, k]) <- B.getLine
    let sk = (1 + k) * k `div` 2
        gr = runx n sk 1
        gh = if gr /= -1 then gr else run n sk (min (n - 1) 100000)
        gi = map (gh *) ([1..k - 1] ++ [n `div` gh - sk + k])
    putStrLn $ if gh == -1 then "-1" else (L.intercalate " " . (map show)) gi

{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE ViewPatterns, ScopedTypeVariables #-}

import qualified Data.ByteString as B (ByteString, getLine)
import qualified Data.ByteString.Char8 as C (readInteger, words)
import qualified Data.Maybe as M (fromJust)
import qualified Data.Int as I (Int64)
import qualified Data.List as L (intercalate)

readInt :: B.ByteString -> I.Int64
readInt = fromIntegral . fst . M.fromJust . C.readInteger

run n sk 0 = -1
run n sk i | n `mod` i == 0 && n `div` i >= sk = i | i == n = -1 | otherwise = run n sk (i - 1)

main = do
    (map readInt . C.words -> [n, k]) <- B.getLine
    let sk = (1 + k) * k `div` 2
        gr = run n sk n
        gh = if gr /= -1 then gr else run n sk (min (n - 1) 100000)
        gi = map (gh *) ([1..k - 1] ++ [n `div` gh - sk + k])
    putStrLn $ if gh == -1 then "-1" else (L.intercalate " " . (map show)) gi