import Control.Monad import Control.Monad.ST import Data.Bool import qualified Data.Bits as Bits import qualified Data.Array.ST as ArrST import qualified Data.Array.Unboxed as ArrU thirdRoot :: Float -> Float thirdRoot n = fst $ until (uncurry(==)) (\(_, x0) -> (x0,((n - 1.0) * x0 + 3.0 / x0 ** (n - 1.0)) / n)) (3.0, 3.0 / n) sieveUA :: Int -> ArrU.UArray Int Bool sieveUA top = ArrST.runSTUArray $ do let m = (top-1) `div` 2 r = floor . sqrt $ fromIntegral top + 1 sieve <- ArrST.newArray (1,m) True forM_ [1..r `div` 2] $ \i -> do isPrime <- ArrST.readArray sieve i when isPrime $ do forM_ [2*i*(i+1), 2*i*(i+2)+1..m] $ \j -> do ArrST.writeArray sieve j False return sieve primesToUA :: Int -> [Int] primesToUA top = 2 : [i*2+1 | (i,True) <- ArrU.assocs $ sieveUA top] main :: IO () main = readLn >>= putStrLn . solver solver :: Int -> String solver n = bool "NO" "YES" $ func1 n func1 :: Int -> Bool func1 n = iter n 0 ps where ps = primesToUA 10000 iter res p [] | p >= 3 || (p == 2 && millerRabin p) = True | p == 1 = not $ millerRabin res | res >= 10 ^ 12 = let xxx = round $ thirdRoot $ fromIntegral res in if even xxx then millerRabin $ xxx - 1 else millerRabin xxx | otherwise = False iter i j (l:ls) | i < 2 = j >= 3 | j >= 3 = True | i `mod` l == 0 = iter (func2 i l) (j + func3 i l) ls | otherwise = iter i j ls func2 :: Int -> Int -> Int func2 n mo | n `mod` mo == 0 = func2 (n `div` mo) mo | otherwise = n func3 :: Int -> Int -> Int func3 n mo = iter n mo 0 where iter i j k | i `mod` j == 0 = iter (i `div` j) j (k + 1) | otherwise = k millerRabin :: Int -> Bool millerRabin n | n <= 1 = False | n == 2 || n == 3 || n == 5 || n == 7 = True | even n = False | otherwise = mrCheck $ fromIntegral n powMod :: Integer -> Integer -> Integer -> Integer powMod b e m = loop 1 (b `mod` m) e where loop res base pxe | pxe <= 0 = res | otherwise = let res' = if pxe `mod` 2 == 1 then (res * base) `mod` m else res pxe' = Bits.shift pxe (-1) base' = (base * base) `mod` m in loop res' base' pxe' factoringPowers :: Integer -> (Integer, Integer) factoringPowers n = loop (n - 1) 0 where loop d s | even d = loop (d `div` 2) (s + 1) | otherwise = (s, d) mrCheck :: Integer -> Bool mrCheck p | p < 2047 = loop [2] | p < 9080191 = loop [31,73] | p < 4759123141 = loop [2,7,61] | p < 1122004669633 = loop [2,13,23,1662803] | p < 2152302898747 = loop [2,3,5,7,11] | p < 341550071728321 = loop [2,3,5,7,11,13,17] | p < 3825123056546413051 = loop [2,3,5,7,11,13,17,19,23] | p < 9223372036854775808 = loop [2,325,9375,28178,450775,9780504,1795265022] | otherwise = loop [ 2 .. min (p - 1) (floor $ 2 * (log p')^(2 :: Int)) ] where p' = fromIntegral p :: Double (s, d) = factoringPowers p loop [] = True loop (a:as) | (powMod a d p) /= 1 && powLoop 0 = False | otherwise = loop as where powLoop r | r < s = (powMod a (2 ^ r * d) p) /= (p - 1) && powLoop (r + 1) | otherwise = True