lucky n = all (\i -> elem i "47") $ show n

-- Snippet: divisors
divisors1 i n
    | n == i * i = [i]
    | n <  i * i = []
    | mod n i == 0 = i : (div n i) : divisors1 (i + 1) n
    | otherwise = divisors1 (i + 1) n

divisors = divisors1 1

main = do
    n <- readLn
    putStrLn $ if any lucky $ divisors n then "YES" else "NO"

import Control.Applicative
import Data.Char
import Data.List

main= do
 	s<-read <$> getLine ::IO Int
	putStrLn $ if (any (\z-> mod s z==0) [4,7,44,47,74,77,444,447,477,744,747,774,777]) then "YES" else "NO"

{-
A. Lucky Division
==================
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

Petya calls a number almost lucky if it could be evenly divided by some lucky number. Help him find out if the given number n is almost lucky.

Input
------
The single line contains an integer n (1 ≤ n ≤ 1000) — the number that needs to be checked.

Output
------
In the only line print "YES" (without the quotes), if number n is almost lucky. Otherwise, print "NO" (without the quotes).

Sample test(s)
---------------
input
47
output
YES

input
16
output
YES

input
78
output
NO

-}
import Data.List (nub, sort, findIndices)
import Data.Map (toList, fromListWith)
import Control.Applicative ((<$>), (<*>))
import Control.Monad (when, forM_, replicateM)
import Data.Array.ST (newArray, readArray, writeArray, runSTUArray)
import Data.Array.Unboxed (UArray, assocs)

sieve :: Int -> UArray Int Bool
sieve n = runSTUArray $ do
    let maxP = floor . sqrt $ fromIntegral n
    sieveTF <- newArray (2, n) True
    forM_ [2..maxP] $ \p -> do
      isPrime <- readArray sieveTF p
      when isPrime $ do
        forM_ [p*p, p*p+p .. n] $ \q -> do
          writeArray sieveTF q False
    return sieveTF

primesTo :: Int -> [Int]
primesTo n
  | n < 2     = []
  | otherwise = [i | (i,True) <- assocs $ sieve n]

cartesianProduct :: [[a]] -> [[a]]
cartesianProduct = foldr (\xs acc -> (:) <$> xs <*> acc) [[]]

tally :: Ord a => [a] -> [(a, Int)]
tally xs = toList $ fromListWith (+) [(x, 1)| x <- xs]

factorInteger :: Int -> [(Int, Int)]
factorInteger 0 = [(0, 1)]
factorInteger 1 = [(1, 1)]
factorInteger n = tally $ factor n
  where
    ps = (primesTo . round . sqrt . fromIntegral) n
    factor 1 = []
    factor p = k : factor (p `div` k)
      where
        ds = dropWhile (\q -> p `mod` q /= 0) ps
        k = if null ds then p else head ds

divisors :: Int -> [Int]
divisors 1 = [1]
divisors n = sort [product xs | xs <- cartesianProduct factors]
  where factors = [ map (n^) [0..pow] | (n, pow) <- factorInteger n ]

----

isAlmostLucky :: Int -> Bool
isAlmostLucky x = isLucky x ||
                  ( (not . null) luckyDs &&
                    any (\k -> x `mod` k == 0) luckyDs )
  where luckyDs = filter isLucky $ divisors x

isLucky :: Int -> Bool
isLucky x = s == "47" || s == "4" || s == "7"
  where s = (sort . nub . show) x

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

main = do
  input <- getLine
  let x = read input
  (putStrLn . boolToStr . isAlmostLucky) x

main = do
  m <- readLn
  if (allu m) then putStrLn "YES" else putStrLn "NO"
lu :: Int -> Bool
lu x = and $ map (not. (`elem` (show x))) ['1','2','3','5','6','8','9','0']
st :: Int -> [Int]
st x = filter lu [1..(floor . fromIntegral $ x)]
allu :: Int -> Bool
allu x = or $ map (\y -> mod x y == 0) (st x)

lucky = filter (\x -> all (\c -> c == '4' || c == '7') $ show x) $ [1..2000]
main = interact $ (\x -> if any (\y -> mod x y == 0)  lucky then "YES\n" else "NO\n").read

factorize :: Int -> [Int]
factorize = factorize' 1
    where
        factorize' a x
            | a * a > x  = []
            | factor a x = [a, x `div` a] ++ factorize' (a + 1) x
            | otherwise  = factorize' (a + 1) x
        factor a x = x `mod` a == 0

main :: IO ()
main = do
    condition <- any (all (\x -> '4' == x || '7' == x) . show) . factorize . (read :: String -> Int) <$> getLine
    putStrLn $ if condition then "YES" else "NO"

isLucky = all (`elem` ['4','7']) . show
divs n = n : filter ((== 0) . mod n) [2 .. n `div` 2]
yesno b = if b then "YES" else "NO"
main = interact $ yesno . any isLucky . divs . read

import Data.List
import Data.Char

getInt :: IO Int
getInt = readLn

readInts :: IO [Int]
readInts = fmap (map read.words) getLine

main = do
      n <- getInt
      putStrLn (if (final n == True) then "YES" else "NO")

final :: Int -> Bool
final n = foldl' (\acc x -> if n `mod` x == 0 then True else acc) False (before n)

before :: Int -> [Int]
before n = takeWhile (<=n) gen47s

gen47s :: [Int]
gen47s = 4:7:map create [1..]
  where create n = fn (gen47s!!n)
        fn k
          | k == 0 = 4
          | k `mod` 10 == 7 = 4 + (fn(k `div` 10)*10)
          | k `mod` 10 == 4 = k - 4 + 7

import Control.Monad
import Control.Applicative
main = getLine >>= putStrLn.answer.solve.read

answer cond = if cond then "YES" else "NO"
solve i = elem 0 $ map (mod i) $ filter (all (flip elem "47").show) [1..1000]

luckys :: Int -> [String]
luckys 0 = []
luckys n = [x:xs | xs <- []:luckys (n-1), x <- "47"]

process :: Int -> Bool
process n = any (\x -> (n `mod` x) == 0) . map readInt $ luckys d
    where d = length . show $ n

readInt :: String -> Int
readInt = read

main :: IO ()
main = do
    n <- fmap readInt getLine
    putStrLn $ ["NO","YES"] !! fromEnum (process n)

main = (readLn :: IO Integer) >>= putStrLn . s

s :: Integer -> String
s n | any (all (`elem` "47") . show) [t | t <- [1..n], n `mod` t == 0] = "YES"
    | otherwise = "NO"

toInt :: String -> Int
toInt = read

isAlmostLucky :: Int -> String
isAlmostLucky n	= 	if any (\x -> ( n `rem` x ) == 0 ) ( map (toInt) [ s | let l = ["", "4", "7"], x <- l, y <- l, z <- l, let s = x ++ y ++ z, length s > 0 ] )
					then "YES"
					else "NO"

main = do
	str <- getLine
	putStrLn $ isAlmostLucky $ toInt str

almostLucky :: Int -> String
almostLucky x
    | any lucky divisors  = "YES"
    | otherwise = "NO"
    where divisors = [y | y <- [1..x],  x `rem` y == 0]

lucky :: Int -> Bool
lucky = all (`elem` "47") . show

solve :: String -> String
solve = almostLucky. read . head . words

main :: IO ()
main = interact solve

luckynum = [x|x <- [1..1000], all (`elem` "47").show $ x]
f x = if or [x `mod` y == 0| y<-luckynum] then "YES" else "NO"
main = interact $ f.read

luckies :: [Int]
luckies = map read stream
    where
      stream = ["4", "7"] ++ [y ++ x | y <- stream, x <- ["4", "7"]]

solve x =
    or $ map ((== 0) . (mod x)) divisors
    where
    divisors = takeWhile (<= x) luckies

pprint True = "YES"
pprint False = "NO"

parse = read

main = (putStrLn . pprint . solve . parse =<< getContents)

data Answer = YES | NO deriving (Show)

happy :: [Integer]
happy = [4, 7, 44, 47, 74, 77, 444, 447, 474, 477, 744, 747, 774, 777]

getAnswer :: Integer -> Answer
getAnswer n = if length [x | x <- happy, n `mod` x == 0] > 0
              then YES
              else NO

main = do
    n <- getLine
    print $ getAnswer (read n)

import Control.Monad

factors :: Int -> [Int]
factors n = concat $ [ [i,n `div` i] | i <- [1..truncate $ sqrt $ fromIntegral n], n `mod` i == 0]

isLucky n = all (`elem` "47") rep
  where
    rep = show n

almostLucky n = any isLucky $ factors n

main = do
  aa <- readLn :: IO Int
  putStrLn $ if almostLucky aa then "YES" else "NO"

module Main (main)
    where

divisors :: Int -> [Int]
divisors n = concatMap (\x -> [x, n `div` x]) .
             filter (\x -> n `mod` x == 0) .
             takeWhile (\x -> x*x <= n) $ [1..]

main :: IO ()
main = putStrLn . toYesNo . isAlmostLucky =<< readLn
    where isAlmostLucky = any isLucky . divisors
          isLucky = all (\d -> d == '4' || d == '7') . show
          toYesNo True  = "YES"
          toYesNo False = "NO"

isLuckyDigit :: Int -> Bool
isLuckyDigit c = c `elem` [4, 7]

isLuckyNumber :: Int -> Bool
isLuckyNumber = and . map isLuckyDigit . digits

digits' :: Int -> [Int]
digits' 0 = []
digits' n = cifra : digits' resto
  where cifra = n `rem` 10
        resto = n `div` 10

digits :: Int -> [Int]
digits = reverse . digits'

isAlmostLucky :: Int -> Bool
isAlmostLucky n
  | isLuckyNumber n  = True
  | divisibleLucky n = True
  | otherwise = False

divisibleLucky :: Int -> Bool
divisibleLucky n = or $ map (\x -> n `rem` x == 0) (luckies n)

squareRoot :: Int -> Int
squareRoot = floor . sqrt . fromIntegral

luckies :: Int -> [Int]
luckies n = [j | j <- [1..n], isLuckyNumber j]

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

main = interact (answer . isAlmostLucky . (\x -> read x :: Int))

main=interact $ isAlmostHappy.read
base = [4,7,47,74,44,77,444,447,477,777,774,744,747,474]

isAlmostHappy n = if or (map (\x->mod n x ==0) base) then "YES" else "NO"

isLucky :: Int -> Bool
isLucky 0 = False
isLucky 4 = True
isLucky 7 = True
isLucky x
  | xmod == 4    = isLucky xdiv
  | xmod == 7    = isLucky xdiv
  | otherwise    = False
    where xmod = x `mod` 10
          xdiv = x `div` 10

isHalfLucky :: Int -> Int -> Bool
isHalfLucky y x
  | x == y                      = False
  | isLucky y && x `mod` y == 0 = True
  | isLucky x                   = True
  | otherwise                   = isHalfLucky (y + 1) x

main = getLine >>= return . isHalfLucky 1 . read >>= \inp -> putStrLn $ if inp then "YES" else "NO"

luckyList = [4, 7] ++ concat [ [x*10+4, x*10+7] | x <- luckyList ]

solve x = any (\y -> x `mod` y == 0) $ takeWhile (<=x) luckyList

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

main = interact $ (++"\n") . showYN . solve . read

-- vim: set expandtab:

isLucky num
 | mod num 4 == 0 = True
 | mod num 7 == 0 = True
 | mod num 44 == 0 = True
 | mod num 47 == 0 = True
 | mod num 77 == 0 = True
 | mod num 444 == 0 = True
 | mod num 447 == 0 = True
 | mod num 474 == 0 = True
 | mod num 477 == 0 = True
 | mod num 744 == 0 = True
 | mod num 747 == 0 = True
 | mod num 774 == 0 = True
 | mod num 777 == 0 = True
 | otherwise = False

main = do
  cv <- getLine
  let conv = (read cv)::Int
  let ans = (isLucky conv)
  if ans
    then putStrLn "YES"
    else putStrLn "NO"

import Data.Char
import Data.List

luck x xs = [c | c <-xs, x `mod` c == 0]

isLuck x
    | null $ luck x [4,7,44,47,74,77,444,447,474,477,744,747,774,777] = False
    | otherwise = True

solve x
    | isLuck x  = "YES"
    | otherwise = "NO"

main = do
    s <- getLine
    putStrLn $ solve $ read s

isLucky :: Int -> Bool
isLucky x = (== []) . filter (\x -> x /= '4' && x /= '7') . show $ x

divisors :: Int -> [Int]
divisors n = filter (\x -> n `rem` x == 0) $ [1 .. (n - 1)]

main = do
    line <- getLine
    let n = (read :: String -> Int) line
    let luckyDivisors = map isLucky (divisors n)

    putStrLn (if (isLucky n) then "YES" else if (any (== True) luckyDivisors) then "YES" else "NO")

main = do
    n <- readLn

    let
        t = ['4', '7']
        ls = [4, 7] ++
            map read [[a, b] | a <- t, b <- t] ++
            map read [[a, b, c] | a <- t, b <- t, c <- t]

        l a = any (\l -> a `mod` l == 0) ls

    putStrLn $ if l n then "YES" else "NO"

main = do
	a <- getLine
	if lucky $ read a
		then putStrLn "YES"
		else putStrLn "NO"

lucky :: (Integral a) => a -> Bool
lucky x
	| check x = True
	| any (lucky) $ [y | y <- [1..x-1], mod x y == 0] = True
	| otherwise = False

check :: (Integral a) => a -> Bool
check x
	| x == 0 = True
	| (let y = mod x 10 in y == 4 || y == 7) && (check $ div x 10) = True
	| otherwise = False

main = do
	a <- getLine
	if lucky $ read a
		then putStrLn "YES"
		else putStrLn "NO"

lucky :: (Integral a) => a -> Bool
lucky x
	| check x = True
	| any (lucky) $ [y | y <- [1..x-1], x `mod` y == 0] = True
	| otherwise = False

check :: (Integral a) => a -> Bool
check x
	| x == 0 = True
	| (let y = x `mod` 10 in y == 4 || y == 7) && (check $ x `div` 10) = True
	| otherwise = False

main = interact (lucky.read)
lucky x
	| all (`elem` "47") . show $ x = "YES"
	| any ((=="YES").lucky) $ [y | y <- [1..x-1], mod x y == 0] = "YES"
	| otherwise = "NO"

-- Lucky
isLuck n | (n == 0) = True
         | (is) = isLuck (div n 10)
		 | otherwise = False
	where m = mod n 10
	      is = ((m == 4)||(m == 7))

isAnswer n = foldr (||) False (map (isLuck) [i | i <- [4 .. n], (mod n i == 0)])

main = do
			n <- readLn :: IO Int
			if (isAnswer n) then putStrLn "YES"
			                else putStrLn "NO"

{-# LANGUAGE BlockArguments #-}

import Control.Arrow
import Data.List

main = do { x <- getLine
          ; putStrLn $ (read >>> solve) x
          }

luckies :: [Int]
luckies = [4, 7, 44, 47, 74, 77, 444, 447, 474, 477, 744, 747, 774, 777]

solve :: Int -> String
solve x = let b = foldr (\y a -> a || x `mod` y == 0) False luckies
          in if b then "YES" else "NO"

import Data.List
f x = filter (\x -> (show x) `intersect` "12356890" == []) [1..x]
main = do
	str <- getLine
	if (filter (\a -> (read str) `mod` a == 0) (f (read str))) /= [] then putStrLn "YES" else putStrLn "NO"

module Main where
import Data.Char

lst :: [Int]
lst = [x | x <- [1..1000], isLucky x]

isLucky :: Int -> Bool
isLucky x = let s = show x
            in all (flip elem ['4', '7']) s

main :: IO ()
main = interact $ solve . lines

solve :: [String] -> String
solve s = let n = read (head s) :: Int
          in
            case any (\x -> n `mod` x == 0) lst of
              True -> "YES"
              False -> "NO"

import Data.Set

main = do
    aux <- getLine
    let n = read aux :: Int
    let xs = [x | x <- [4,5..n], lucky x, mod n x == 0]
    let ans = if (length xs > 0) then "YES" else "NO"
    putStrLn ans

mkUnique = toList . fromList

lucky n = (length (mkUnique (show n)) == 2) && ((((mkUnique (show n)) !! 0 == '4') && (mkUnique (show n) !! 1 == '7')) || ((mkUnique (show n) !! 0 == '7') && ((mkUnique (show n) !! 1 == '4')))) || ((length (mkUnique (show n)) == 1) && ((head (mkUnique (show n))  == '4') || (head (mkUnique (show n)) == '7')))

good :: Int -> Bool
good a
	| a == 0     = True
	| (((a `mod` 10 == 4) || (a `mod` 10 == 7)) && (good (a `div` 10))) = True
    | otherwise = False

allright :: [Int] -> Bool
allright [] = False
allright (x:xs)
	| ( (allright xs) || (good x) ) = True
	| otherwise = False

gen :: Int -> Int -> [Int]
gen a n
	| ((a>0) && (n `mod` a) == 0 ) = (gen (a-1) n) ++ [a]
	| ((a>0) && ((n `mod` a) > 0)) = gen (a-1) n
	| otherwise = []

lucky :: Int -> Bool
lucky a
	| allright (gen a a) = True
	| otherwise = False

handle :: [String] -> [[String]]
handle [n]
	| lucky (read n :: Int) = [["YES"]]
	| otherwise = [["NO"]]

main :: IO ()
main = interact $ unlines . map unwords . handle . words

import Control.Applicative

luckys :: Int -> [Int]
luckys n = drop 1 $ gen 0
    where gen x | x > n     = []
                | otherwise = x : gen (x * 10 + 4) ++ gen (x * 10 + 7)

main = do
  n <- read <$> getLine
  putStrLn $ case any (\x -> n `mod` x == 0) $ luckys n of
               True  -> "YES"
               False -> "NO"

main :: IO ()
main = do s <- getLine
--       	  print (stoi s)
       	  putStr (getAnswer (stoi s) )
--	       print isLucky (stoi s)

stoi ::String -> Int
--stoi = foldr (\x y -> 10*(digitToInt x)+(digitToInt y)) 0
stoi [] = 0
stoi x = digitToInt(last x)+10*(stoi (take ((length x) -1)  x))

digitToInt::Char -> Int
digitToInt x = case x of
	   '0' -> 0
	   '1' -> 1
	   '2' -> 2
	   '3' -> 3
	   '4' -> 4
	   '5' -> 5
	   '6' -> 6
	   '7' -> 7
	   '8' -> 8
	   '9' -> 9
	   _ -> 0

getAnswer ::Int -> String
getAnswer a |(isAlmostLucky a) = "YES"
	    | otherwise = "NO"
isAlmostLucky ::Int -> Bool
isAlmostLucky a = any (\x -> (isLucky x) && (a `mod` x == 0)) [4..a]
isLucky ::Int -> Bool
isLucky 0 = True
isLucky a | ((a`mod`10)/=4)&&((a`mod`10)/=7) = False
	  | otherwise = isLucky (a `div` 10)

main :: IO ()
main = do s <- getLine
       	  putStr (getAnswer (stoi s))

stoi ::String -> Int
stoi = foldl (\x y -> 10*x+(digitToInt y)) 0

digitToInt::Char -> Int
digitToInt x = case x of
	   '0' -> 0
	   '1' -> 1
	   '2' -> 2
	   '3' -> 3
	   '4' -> 4
	   '5' -> 5
	   '6' -> 6
	   '7' -> 7
	   '8' -> 8
	   '9' -> 9
	   _ -> 0

getAnswer ::Int -> String
getAnswer a |(isAlmostLucky a) = "YES"
	    | otherwise = "NO"
isAlmostLucky ::Int -> Bool
isAlmostLucky a = any (\x -> (isLucky x) && (a `mod` x == 0)) [4..a]
isLucky ::Int -> Bool
isLucky 0 = True
isLucky a | ((a`mod`10)/=4)&&((a`mod`10)/=7) = False
	  | otherwise = isLucky (a `div` 10)

main = readsolveprint
readsolveprint::IO()
readsolveprint= interact $ solve. head . lines
solve xs | any (==0) $ map (\x-> mod (read xs) x) $ takeWhile (<1000) f ="YES"
          | otherwise = "NO"
f::[Int]
f= map read $ concat $ tail $ iterate (\x->(map ("4"++) x) ++ (map("7"++) x)) [""]

import Data.Char

h :: Int -> String
h x = if length xs == 0 then "NO"
 else "YES"
 where
  xs = filter (\a -> mod x a == 0) (creL 4)

creL :: Int -> [Int]
creL 0 = [4,7]
creL x = [a+10*b | a <-[4,7], b <- creL (x-1)]++(creL (x-1))

main = do
 x <- getLine
 y <- readIO x::IO Int
 putStrLn (h y)

isHappyChar :: Char -> Bool
isHappyChar '4'  =  True
isHappyChar '7'  =  True
isHappyChar _    =  False

isHappyNumber :: Num a => a -> Bool
isHappyNumber n  =  all isHappyChar (show n)

happyNumbers :: [Int]
happyNumbers = filter isHappyNumber [0..1000]

solve :: Int -> Bool
solve n = any (\x -> mod n x == 0) happyNumbers

printAns :: Bool -> IO ()
printAns True   =  putStrLn "YES"
printAns False  =  putStrLn "NO"

main :: IO ()
main = readLn >>= printAns . solve

-- Codeforces 122A

import Data.List

main :: IO ()
main = getLine >>= putStrLn . solve . read where
    solve n = case find (\x -> n `mod` x == 0) $ map read $ lucky 3 ["4", "7"] of {
        Nothing -> "NO";
        Just _ -> "YES";
    } where
        lucky 1 xs = xs
        lucky n xs = k ++ [x++y | x <- xs, y <- k] where k = lucky (n-1) xs

import Control.Applicative

lucky = takeWhile (<=1000) ln

ln = 4 : 7 : concatMap f ln
  where f x = [x*10+4,x*10+7]

main = do
  n <- read <$> getLine :: IO Int
  putStrLn $
    if any (\x -> rem n x == 0) lucky then "YES" else "NO"

import Data.Function

arr = [4, 7]
len = length arr

main = do
	n <- getInt
	putStrLn $ if isLucky n || isAlmostLucky n then "YES" else "NO"

getInt::IO Int
getInt = do
	line <- getLine
	return $ read line

isLucky::Int -> Bool
isLucky n = fix (\fn curr ->
					if curr == 0
						then True
					else if (not $ curr `mod` 10 == 4) && (not $ curr `mod` 10 == 7)
						then False
					else fn $ curr `div` 10) n

isAlmostLucky::Int -> Bool
isAlmostLucky n = do
	any (\digits -> isAlmostLuckyForPlace n digits) [1..3]

isAlmostLuckyForPlace::Int -> Int -> Bool
isAlmostLuckyForPlace n digits = do
	let numbersQtd = len ^ digits
	let nums = map (\index -> calcNumber digits index) [0..numbersQtd]
	any (\num -> n `mod` num == 0) nums

calcNumber::Int -> Int -> Int
calcNumber digits index = foldr (\digit res -> arr !! (index `div` (len ^ digit) `mod` len) * (10 ^ digit) + res) 0 [0..(digits - 1)]

import Data.Function

main = do
  n <- readInt
  putStrLn $ if isLucky n || isAlmostLucky n then "YES" else "NO"

readInt::IO Int
readInt = do
  line <- getLine
  return $ read line

isLucky::Int -> Bool
isLucky n = fix (\fn candidate res -> do
  let rem = candidate `mod` 10
  if candidate == 0 then res
  else if rem == 4 || rem == 7 then fn (candidate `div` 10) True
  else False) n  False

isAlmostLucky::Int -> Bool
isAlmostLucky n = fix (\fn candidate ->
  if candidate > n then False
  else if n `mod` candidate == 0 && isLucky candidate then True
  else fn $ candidate + 1) 4

main = do
    x <- fmap read getLine :: IO Int
    let l = map show $ x : [y | y<-[1..(x `div` 2)], x `mod` y == 0]
    putStrLn $ if any (all (\x -> x=='4' || x=='7')) l then "YES" else "NO"

isLucky :: Int -> Bool
isLucky x = filter (\y -> y /= '4' && y /= '7') (show x) == []

-- first argument is the upper bound
luckyNumbers :: Int -> [Int]
luckyNumbers bound =
  filter isLucky [1..bound]

almostLucky :: Int -> Bool
almostLucky n =
  or $ map (\x -> n `mod` x == 0) (luckyNumbers n)

main :: IO ()
main = do
  x <- getLine
  let almostLuckyStr =
        if almostLucky $ read x then
          "YES"
        else
          "NO"
    in putStrLn $ almostLuckyStr

getInt :: IO Int
getInt = readLn

lucky [] = True
lucky (x:xs) = ((x == 4) || (x == 7)) && lucky (xs)

digs 0 = []
digs x = x `mod` 10 : digs (x `div` 10)

divs a = [x | x <- a : [1 .. quot a 2], (mod a x == 0) && lucky (digs (x))]

solve a = putStrLn $ if length (divs a) == 0 then "NO" else "YES"
main = getInt >>= solve

import Control.Monad
import Data.Array
import Data.Char
import Data.List
import qualified Data.ByteString.Char8 as B
getList :: Read a => IO [a]
getList = fmap (map (read . B.unpack) . B.words) B.getLine
getIntList :: IO [Int]
getIntList = fmap (unfoldr (B.readInt . B.dropWhile isSpace)) B.getLine

islucky :: Int -> Bool
islucky 0 = True
islucky x = (x `mod` 10 == 4 || x `mod` 10 == 7) && islucky (x `div` 10)

solve :: Int -> Bool
solve x = any ((==0) . (x `mod`)) . filter islucky $ [1..x]

main :: IO ()
main = do
        n <- readLn
        putStrLn $ if solve n then "YES" else "NO"

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

main::IO ()
main=do
    a<-(read <$> getLine) :: IO Int
    let b = [4,7,44,47,74,77,444,447,474,477,744,747,774,777]
    putStrLn $ if (any (\z-> mod a z==0) b) then "YES" else "NO"

calc n = if (0 `elem` (map (\x -> n `mod` x) (filter g [1,2..n]))) then 0 else 1
    where g x = not(True `elem` (map (\t -> t `elem` (show x)) "12356890"))
main = interact $ ((words "YES NO") !!) . calc . read

main :: IO ()
main = readLn >>= putStrLn . solve

solve :: Int -> String
solve = yesno . any isLucky . factors

isLucky :: Int -> Bool
isLucky = all (`elem`"47") . show

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

factors :: Integral t => t -> [t]
factors n = [m | m <- [1..n], n `mod` m == 0]

import Prelude hiding (readList)
import Control.Monad
import Control.Applicative
import Data.List

readList :: Read a => IO [a]
readList = fmap (fmap read . words) getLine

readList' :: Read a => a -> IO [a]
readList' _ = readList

readInt :: IO Int
readInt = head <$> readList' (undefined::Int)

showList :: Show a => [a] -> IO ()
showList = putStrLn . unwords . map show

skipLine :: IO ()
skipLine = void $ getLine

count :: (a -> Bool) -> [a] -> Int
count f = length . filter f

divUp :: Integral a => a -> a -> a
divUp x y
  | x `mod` y /= 0 = x `div` y + 1
  | otherwise      = x `div` y

isHappy :: Int -> Bool
isHappy n = all (`elem` "47") (show n)

main :: IO ()
main = do
  n <- readInt
  putStrLn $
    if (any ((==0) . (n `mod`)) $ filter isHappy [1..n])
    then "YES"
    else "NO"

luckys = [4, 7, 47, 74, 447, 474, 477, 747, 774]

lucky :: Int -> String
lucky n = if null l
             then "NO"
             else "YES"
  where l = filter (\p -> rem n p == 0) luckys

main = do
  n <- getLine
  putStrLn . lucky . read $ n

import Data.List
main = interact (\s -> if почтиСчастливое $ read s then "YES" else "NO")
почтиСчастливое n = any (\x -> n `mod` x == 0 && счастливое x) [1..n]
счастливое = all (\x -> x == '7' || x == '4') . show

import Control.Monad
import Control.Applicative
import Data.List

getNums :: (Read a,Num a) => IO [a]
getNums = map read. words <$> getLine

luckies = [4,7,44,47,74,77,444,447,474,477,744,747,774,777]

isAlmostLucky n = Nothing /= (find (\x -> 0==n`mod`x) luckies)

main = do
  b <-isAlmostLucky <$> read <$> getLine
  putStrLn $ if b then "YES" else "NO"

-- Vicfred
-- https://codeforces.com/problemset/problem/122/A

islucky :: Int -> Bool
islucky 0 = True
islucky x = (x `mod` 10 == 4 || x `mod` 10 == 7) && islucky (x `div` 10)

solve :: Int -> Bool
solve x = any ((==0) . (x `mod`)) . filter islucky $ [1..x]

main = do
    n <- readLn
    putStrLn $ if solve n then "YES" else "NO"

module Main where
import Data.List
import Control.Applicative
isLucky :: Int -> Bool
isLucky num = case find (\digit -> digit /= '4' && digit /= '7') $ show num of Just _ -> False; Nothing -> True

buildLuckyNumbers :: Int -> [Int]
buildLuckyNumbers to = filter isLucky [4..to]

main = do
  num <- (read :: String -> Int) <$> getLine
  putStrLn $ case find (\ln -> num `mod` ln == 0) $ buildLuckyNumbers num of Just _ -> "YES"; Nothing -> "NO"

p True = "YES"
p False = "NO"
isLucky =  all (`elem` "74") . show
divs n = n : filter ((== 0) . mod n) [2 .. n `div` 2]
main = interact $ p . any isLucky . divs . read

listLuckyDivider = [x |x <-[1..1000], luckyDividerTwo x]

luckyDividerOne :: Int -> Bool
luckyDividerOne x = not ((filter (\y -> x `mod` y == 0) listLuckyDivider) == [])

luckyDividerTwo :: Int -> Bool
luckyDividerTwo 0 = True
luckyDividerTwo x
    | (((x `mod` 10) == 4) || ((x `mod` 10) == 7))  = luckyDividerTwo (x `div` 10)
    | otherwise = False

main :: IO()
main = do
    a <- getLine
    if luckyDividerOne (read a :: Int)
    then putStrLn "YES"
    else
        if luckyDividerTwo (read a :: Int)
            then putStrLn "YES"
            else putStrLn "NO"

isLucky :: Int -> Bool
isLucky = all (`elem` "47") . show

solve :: Int -> String
solve n = loop 2
 where
  loop x
    | n < x                       = "NO"
    | n `rem` x == 0 && isLucky x = "YES"
    | otherwise                   = loop (x + 1)

main :: IO ()
main = readLn >>= putStrLn . solve

main = do
    n <- fmap read getContents
    putStrLn $ if n `divBy` luckyNumbers then "YES" else "NO"
    where luckyNumbers = [4, 7, 44, 47, 74, 77, 444, 447, 474, 477, 744, 747, 774, 777]
          divBy x = any (==0) . map (x `rem`)

luckys :: [Int]
luckys = [4, 7, 44, 47, 74, 77, 444, 447, 474, 477, 744, 747, 774, 777]

main :: IO ()
main    = do    n <- fmap read getLine
                putStrLn $ if solve n luckys
                                then "YES"
                                else "NO"
    where
        solve n = any (== 0) . map (mod n) . takeWhile (<= n)

dividor n i l =
    if i>n then l
    else if (mod n i) == 0 then dividor n (i+1) (i:l) else dividor n (i+1) l

isLucky n_ = let n = show n_ in all (\c -> c=='4'||c=='7') n

solve n = if (any (isLucky) (dividor n 1 [])) then "YES" else "NO"

main = getLine >>= putStrLn.solve.(read::String -> Int)

main=readLn>>=putStrLn.solve
solve n|any(\n->all(`elem`"47")(show n))[x|x<-[1..n],n`mod`x==0]="YES"|otherwise="NO"

main = interact solve

solve :: String -> String
solve input = unlines $ result : []
    where result = check n
          n = read $ lines input !! 0

check :: Int -> String
check n
    | any isLucky [x | x <- [1..n], n `mod` x == 0] = "YES"
    | otherwise = "NO"

isLucky :: Int -> Bool
isLucky n = all (\c -> c == '4' || c == '7') (show n)

f :: Int -> String
f n | null op = "YES"
    | n `rem` 4 == 0 = "YES"
    | n `rem` 7 == 0 = "YES"
    | check n = "YES"
    | otherwise = "NO"
    where op = filter (`notElem` "47") $ show n

check n = any (=="YES") $ map f factors
    where factors = [x | x <- [1..n-1], n `rem` x == 0]

main = interact $ f . read . head . words

module Main (main) where

import System
import Data.Char
import Data.List
import Control.Monad
import Control.Applicative
solve :: Int -> String
solve n = if any (\x -> n `mod` x == 0) [4,7,47,74,447,474,744,774,747,477]
            then "YES" else "NO"
main = interact$solve.read.head.words

-- | Return all combinations of a sequence of values.
combinations :: [a] -> [[a]]
combinations coins = concat [combs i coins | i <- [1 .. length coins]]
  where combs 0 _      = [[]]
        combs _ []     = []
        combs k (x:xs) = map (x:) (combs (k-1) xs) ++ combs k xs

-- | Takes a list of digits, and converts them back into a positive integer.
unDigits :: Integral n
    => n   -- ^ The base to use.
    -> [n] -- ^ The digits of the number in list form.
    -> n   -- ^ The original number.
unDigits base = foldl (\ a b -> a * base + b) 0

almostLucky :: Integer -> Bool
almostLucky n = or (map (\ x -> n `mod` x == 0) divs)
  where
    divs = map (\ x -> unDigits 10 x) (combinations [4,4,4,7,7,7])

main = do
  str <- getLine
  if almostLucky (read str) then
    putStrLn "YES"
  else
    putStrLn "NO"

isLucky :: Int -> Bool
isLucky n = show n == filter (\c -> c == '4' || c == '7') (show n)

calc :: Int -> String
calc n = if null [i | i <- [1..n], isLucky i, n `mod` i == 0]
         then "NO"
         else "YES"

main = do s <- getLine
          putStrLn $ calc $ read s

main=interact $ show.isAlmostHappy.read
base = [4,7,47,74,44,77,444,447,477,777,774,744,747,474]

isAlmostHappy n = or (map (\x->mod n x ==0) base)

{-# LANGUAGE BlockArguments #-}

import Control.Arrow
import Data.List

main = do { x <- getLine
          ; putStrLn $ (read >>> solve) x
          }

luckies :: [Int]
luckies = [4, 7, 44, 47, 74, 77]

solve :: Int -> String
solve x = let b = foldr (\y a -> a || x `mod` y == 0) False luckies
          in if b then "YES" else "NO"

good :: Int -> Bool
good a
	| (((a `mod` 10 == 4) || (a `mod` 10 == 7)) && (good (a `div` 10))) = True
    | otherwise = False

allright :: [Int] -> Bool
allright [] = False
allright (x:xs)
	| ( (allright xs) || (good x) ) = True
	| otherwise = False

gen :: Int -> Int -> [Int]
gen a n
	| ((a>0) && (n `mod` a) == 0 ) = (gen (a-1) n) ++ [a]
	| ((a>0) && ((n `mod` a) > 0)) = gen (a-1) n
	| otherwise = []

lucky :: Int -> Bool
lucky a
	| allright (gen a a) = True
	| otherwise = False

handle :: [String] -> [[String]]
handle [n]
	| lucky (read n :: Int) = [["YES"]]
	| otherwise = [["NO"]]

main :: IO ()
main = interact $ unlines . map unwords . handle . words

import Control.Applicative
import Debug.Trace

luckys :: Int -> [Int]
luckys n = drop 1 $ gen 0
    where gen x | x > n     = []
                | otherwise = x : gen (x * 10 + 4) ++ gen (x * 10 + 7)

main = do
  n <- read <$> getLine
  putStrLn $ case any (\x -> x `mod` n == 0) $ luckys n of
               True  -> "YES"
               False -> "NO"

main :: IO ()
main = do s <- getLine
       	  print (stoi s)

stoi ::String -> Int
stoi = foldl (\x y -> 10*x+(digitToInt y)) 0

digitToInt::Char -> Int
digitToInt x = case x of
	   '0' -> 0
	   '1' -> 1
	   '2' -> 2
	   '3' -> 3
	   '4' -> 4
	   '5' -> 5
	   '6' -> 6
	   '7' -> 7
	   '8' -> 8
	   '9' -> 9
	   _ -> 0

getAnswer ::Int -> String
getAnswer a |(isAlmostLucky a) = "YES"
	    | otherwise = "NO"
isAlmostLucky ::Int -> Bool
isAlmostLucky a = any (\x -> (isLucky x) && (a `mod` x == 0)) [4..a]
isLucky ::Int -> Bool
isLucky 0 = True
isLucky a | ((a`mod`10)/=4)&&((a`mod`10)/=7) = False
	  | otherwise = isLucky (a `div` 10)

p True = "YES"
p False = "NO"
isLucky =  all (`elem` "74") . show
almostLucky n | isLucky n = True
              | n `mod` 4 == 0 = almostLucky4' $ n `div` 4
              | n `mod` 7 == 0 = almostLucky7' $ n `div` 7
              | otherwise = False
  where
    almostLucky' 1 = True
    almostLucky' n = almostLucky n
    almostLucky4' n | n < 4 = True
                    | otherwise = almostLucky' n
    almostLucky7' m | n < 7 = True
                    | otherwise = almostLucky' n
main = interact $ p . almostLucky . read

p True = "YES"
p False = "NO"
isLucky =  all (`elem` "74") . show
almostLucky n | isLucky n = True
              | n `mod` 4 == 0 = almostLucky $ n `div` 4
              | n `mod` 7 == 0 = almostLucky $ n `div` 4
              | otherwise = False
main = interact $ p . isLucky . read

p True = "YES"
p False = "NO"
isLucky =  all (`elem` "74") . show
almostLucky n | isLucky n = True
              | n `mod` 4 == 0 = almostLucky $ n `div` 4
              | n `mod` 7 == 0 = almostLucky $ n `div` 7
              | otherwise = False
main = interact $ p . almostLucky . read

luckyDividerOne :: Int -> Bool
luckyDividerOne x = (x `mod` 4 == 0) || (x `mod` 7 == 0)

luckyDividerTwo :: Int -> Bool
luckyDividerTwo 0 = False
luckyDividerTwo x
    | (((x `mod` 10) == 4) || ((x `mod` 10) == 7))  = True
    | otherwise = luckyDividerTwo (x `div` 10)

main :: IO()
main = do
    a <- getLine
    if luckyDividerTwo (read a :: Int)
    then putStrLn "YES"
    else
        if luckyDividerOne (read a :: Int)
            then putStrLn "YES"
            else putStrLn "NO"

luckyDividerOne :: Int -> Bool
luckyDividerOne x = (x `mod` 4 == 0) || (x `mod` 7 == 0)

luckyDividerTwo :: Int -> Bool
luckyDividerTwo 0 = True
luckyDividerTwo x
    | (x `mod` 10) == 4 = luckyDividerTwo (x `div` 10)
    | (x `mod` 10) == 7 = luckyDividerTwo (x `div` 10)
    | otherwise = False

main :: IO()
main = do
    a <- getLine
    if luckyDividerOne (read a :: Int)
    then putStrLn "YES"
    else
        if luckyDividerTwo (read a :: Int)
            then putStrLn "YES"
            else putStrLn "NO"

main = interact solve

solve :: String -> String
solve input = unlines $ result : []
    where result = show $ check n
          n = read $ lines input !! 0

check :: Int -> String
check n
    | any isLucky [x | x <- [1..n], n `mod` x == 0] = "YES"
    | otherwise = "NO"

isLucky :: Int -> Bool
isLucky n = all (\c -> c == '4' || c == '7') (show n)

f :: Int -> String
f n | null op = "YES"
    | n `rem` 4 == 0 = "YES"
    | n `rem` 7 == 0 = "YES"
    | otherwise = "NO"
    where op = filter (`notElem` "47") $ show n

main = interact $ f . read . head . words

isLucky :: Int -> Bool
isLucky n = show n == filter (\c -> c == '4' || c == '7') (show n)

calc :: Int -> String
calc n = if null [i | i <- [1..n], isLucky i]
         then "NO"
         else "YES"

main = do s <- getLine
          putStrLn $ calc $ read s

{-
A. Lucky Division
==================
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

Petya calls a number almost lucky if it could be evenly divided by some lucky number. Help him find out if the given number n is almost lucky.

Input
------
The single line contains an integer n (1 ≤ n ≤ 1000) — the number that needs to be checked.

Output
------
In the only line print "YES" (without the quotes), if number n is almost lucky. Otherwise, print "NO" (without the quotes).

Sample test(s)
---------------
input
47
output
YES

input
16
output
YES

input
78
output
NO

-}
import Data.List (nub, sort, findIndices)
import Data.Map (toList, fromListWith)
import Control.Applicative ((<$>), (<*>))
import Control.Monad (when, forM_, replicateM)
import Data.Array.ST (newArray, readArray, writeArray, runSTUArray)
import Data.Array.Unboxed (UArray, assocs)

sieve :: Int -> UArray Int Bool
sieve n = runSTUArray $ do
    let maxP = floor . sqrt $ fromIntegral n
    sieveTF <- newArray (2, n) True
    forM_ [2..maxP] $ \p -> do
      isPrime <- readArray sieveTF p
      when isPrime $ do
        forM_ [p*p, p*p+p .. n] $ \q -> do
          writeArray sieveTF q False
    return sieveTF

primesTo :: Int -> [Int]
primesTo n
  | n < 2     = []
  | otherwise = [i | (i,True) <- assocs $ sieve n]

cartesianProduct :: [[a]] -> [[a]]
cartesianProduct = foldr (\xs acc -> (:) <$> xs <*> acc) [[]]

tally :: Ord a => [a] -> [(a, Int)]
tally xs = toList $ fromListWith (+) [(x, 1)| x <- xs]

factorInteger :: Int -> [(Int, Int)]
factorInteger 0 = [(0, 1)]
factorInteger 1 = [(1, 1)]
factorInteger n = tally $ factor n
  where
    ps = (primesTo . round . sqrt . fromIntegral) n
    factor 1 = []
    factor p = k : factor (p `div` k)
      where
        ds = dropWhile (\q -> p `mod` q /= 0) ps
        k = if null ds then p else head ds

divisors :: Int -> [Int]
divisors 1 = [1]
divisors n = sort [product xs | xs <- cartesianProduct factors]
  where factors = [ map (n^) [0..pow] | (n, pow) <- factorInteger n ]

----

isAlmostLucky :: Int -> Bool
isAlmostLucky x = isLucky x ||
                  ( (not . null) luckyDs &&
                    any (\k -> x `mod` k == 0 && even (x `div` k)) luckyDs )
  where luckyDs = filter isLucky $ divisors x

isLucky :: Int -> Bool
isLucky x = s == "47" || s == "4" || s == "7"
  where s = (sort . nub . show) x

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

main = do
  input <- getLine
  let x = read input
  (putStrLn . boolToStr . isAlmostLucky) x

{-
A. Lucky Division
==================
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

Petya calls a number almost lucky if it could be evenly divided by some lucky number. Help him find out if the given number n is almost lucky.

Input
------
The single line contains an integer n (1 ≤ n ≤ 1000) — the number that needs to be checked.

Output
------
In the only line print "YES" (without the quotes), if number n is almost lucky. Otherwise, print "NO" (without the quotes).

Sample test(s)
---------------
input
47
output
YES

input
16
output
YES

input
78
output
NO

-}
import Data.List (nub, sort)

isAlmostLucky :: Int -> Bool
isAlmostLucky x = isLucky x || (x == sqrtX ^ 2 && isLucky sqrtX)
  where sqrtX = (round . sqrt . fromIntegral) x

isLucky :: Int -> Bool
isLucky x = s == "47" || s == "4" || s == "7"
  where s = (sort . nub . show) x

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

main = do
  input <- getLine
  let x = read input
  (putStrLn . boolToStr . isAlmostLucky) x

main = interact $ (\s -> if all (\c -> c == '4' || c == '7' || c == '\n') s then "YES\n" else "NO\n")

lucky = filter (\x -> all (\c -> c == '4' || c == '7') $ show x) $ [1..2000]
main = interact $ (\x -> if any (\y -> div x y == 0)  lucky then "YES\n" else "NO\n").read

isLucky = all (`elem` ['4','7']) . show
divs n = filter ((== 0) . mod n) [2 .. n `div` 2]
yesno b = if b then "YES" else "NO"
main = interact $ yesno . any isLucky . divs . read


import Data.List

main = getLine >>= print . solve . map read . words

solve =  maximum . map solve_ . cyclelist 14 . cycle

cyclelist 0 _ = []
cyclelist n (x:xs) = take 14 (x:xs) : cyclelist (n - 1) xs

solve_ (x:xs) = sum $ filter even $ ans
    where (d, m) =  x `divMod` 14
          a1 = 0 : replicate m 1 ++ replicate (13 - m) 0
          a2 = replicate 14 d
          ans = zipWith3 (\x y z -> fromIntegral x + fromIntegral y + fromIntegral z) (0:xs) a1 a2

import Data.List

sing :: Integer -> [Integer] -> Integer -> Integer
sing a [] ind = 0
sing 0 b ind = 0
sing a (b:bs) ind = let kol = (quot a 14) + (if rem a 14 > ind then 1 else 0) + b in

            		 (if even kol then kol else 0) + sing a bs (ind + 1)

solve [] am = 0
solve (a:as) b@(bf:bs) = max (solve as (bs ++ [bf])) (sing a (bs ++ [0]) 0)

main :: IO ()
main = do
    sN <- getLine
    let am = map read $ words sN :: [Integer]
    -- print am
    let res = solve am am
    print res

import Control.Applicative
import Data.Int

main = do
  xs <- readLs :: IO [Int64]
  print $ solve xs

solve zs = maximum $ map solve' . take 14 . iterate tail $ zs ++ zs
  where solve' xs = let (y:ys) = take 14 xs
                        q = div y 14
                        r = fromIntegral $ mod y 14
                    in  sum $ filter even $ zipWith (+) (replicate r 1 ++ cycle [0]) $
                          map (+ q) (ys ++ [0])

readLs = map read . words <$> getLine

import Data.List

sing :: Int -> [Int] -> Int -> Int
sing a [] ind = 0
sing 0 b ind = 0
sing a (b:bs) ind = let kol = (quot a 14) + (if rem a 14 > ind then 1 else 0) + b in

            		 (if even kol then kol else 0) + sing a bs (ind + 1)

solve [] am = 0
solve (a:as) b@(bf:bs) = max (solve as (bs ++ [bf])) (sing a (bs ++ [0]) 0)

main :: IO ()
main = do
    sN <- getLine
    let am = map read $ words sN :: [Int]
    -- print am
    let res = solve am am
    print res

import Data.List

sing :: Int -> [Int] -> Int -> Int
sing a [] ind = 0
sing a (b:bs) ind = let kol = (quot a 14) + (if rem a 14 > ind then 1 else 0) + b in

            		 (if even kol then kol else 0) + sing a bs (ind + 1)

solve [] am = 0
solve (a:as) b@(bf:bs) = max (solve as (bs ++ [bf])) (sing a (bs ++ [0]) 0)

main :: IO ()
main = do
    sN <- getLine
    let am = map read $ words sN :: [Int]
    -- print am
    let res = solve am am
    print res

import Control.Applicative
import Data.Int

main = do
  xs <- readLs :: IO [Int]
  print $ solve xs

solve zs = maximum $ map solve' . take 14 . iterate tail $ zs ++ zs
  where solve' xs = let (y:ys) = take 14 xs
                        q = div y 14
                        r = mod y 14
                    in  sum $ filter even $ zipWith (+) (replicate r 1 ++ cycle [0]) $
                          map (+ q) (ys ++ [0])

readLs = map read . words <$> getLine

import Data.List

main = getLine >>= print . solve . map read . words

solve =  maximum . map solve_ . cyclelist 14 . cycle

cyclelist 0 _ = []
cyclelist n (x:xs) = take 14 (x:xs) : cyclelist (n - 1) xs

solve_ (x:xs) = sum $ filter even $ ans
    where (d, m) = x `divMod` 14
          a1 = 0 : replicate m 1 ++ replicate (13 - m) 0
          a2 = replicate 14 d
          ans = zipWith3 (\x y z -> x + y + z) (0:xs) a1 a2


import Data.List( sort, nub)
main = do
    s_ <- getLine
    let shift 0 s = [s]
	shift n (c:cs) =  (c:cs) : shift (n-1) (cs++[c])
    print.length.nub.sort$shift (length s_ - 1) s_

import Data.Set

main :: IO ()
main = do
    input <- getLine
    handler (length input) input

handler l input = handler' l (cycle input) empty
  where
    handler' 0 _ set = print $ size set
    handler' n word set = handler' (n - 1) (tail word) (insert (take l word) set)

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

import qualified Data.ByteString.Char8 as B
readInt = readLn :: IO Int
readInts = map ( fst . fromJust . B.readInt ) . B.words <$> B.getLine

rotate :: String -> String
rotate s = last s : init s

unique :: Ord a => [a] -> [a]
unique = map head . group . sort

cycles :: Int -> String -> [String]
cycles 0 _ = []
cycles n s = s : cycles (n-1) (rotate s)

cyclicShifts :: String -> Int
cyclicShifts s = length $ unique $ cycles (length s) s

main :: IO ()
main = do
    s <- getLine
    print $ cyclicShifts s
    -- print $ cyclicShifts "bbb"
    -- print $ cyclicShifts "yzyz"


main = do
    contents <- getContents
    let (a, b, c, d) = (intsInLine !! 0, intsInLine !! 1, intsInLine !! 2, intsInLine !! 3)
            where intsInLine = map read $ words $ head $ lines contents
    putStrLn $ getResult (a, c) (b, d)

getPoint :: Float -> Float -> Float
getPoint costPoint submitTime = max (3.0 * costPoint / 10.0) (costPoint - costPoint / 250.0 * submitTime)

getResult :: (Float, Float) -> (Float, Float) -> String
getResult (p0, t0) (p1, t1)
    | pointMisha == pointVasya = "Tie"
    | pointMisha > pointVasya = "Misha"
    | otherwise = "Vasya"
        where
            pointMisha = getPoint p0 t0
            pointVasya = getPoint p1 t1

import Control.Applicative
import Data.List

main= do
	[a,b,c,d]<- map read.words <$> getLine ::IO [Float]
	let s1 = max (3.0*a / 10.0) (a- a/250.0*c)
	let s2 = max (3.0*b / 10.0) (b- b/250.0*d)
	putStrLn $ if s1< s2 then "Vasya" else if s1==s2 then "Tie" else "Misha"

import Data.List

cal ls = winner pv pm
  where  xs = map read $ words $ head $ lines ls
         a = head xs
         b = xs !! 1
         c = xs !! 2
         d = xs !! 3
         point p t = maximum [ p * 0.3, p * ( 1 - t / 250)]
         pm = point a c
         pv = point b d

winner v m
 | v == m = "Tie"
 | v > m = "Vasya"
 | otherwise = "Misha"

main = interact cal

main=interact$solve.map read.words
formula p t = max (3*p/10) (p - (p/250)*t)
solve [a,b,c,d]
  | m > v = "Misha"
  | m < v = "Vasya"
  |otherwise = "Tie"
  where m = formula a c
        v = formula b d

import qualified Data.ByteString.Char8 as B2
import Data.Maybe

main = do
	t<-B2.getLine
	let [a,b,c,d]=B2.words t
	putStrLn $ solve (readI a) (readI b) (readI c) (readI d)

readI = fromInteger . fst .fromJust.B2.readInteger
score p t = max (div (3*p) 10) (p-(div p 250)*t)

solve a b c d
 | misha>vasya = "Misha"
 | misha==vasya = "Tie"
 | otherwise = "Vasya"

	where
	      misha = score a c
	      vasya = score b d

import Data.Ratio

main = interact $ solve . map read . words

solve [a,b,c,d] | ff == ss = "Tie"
	        | ff > ss = "Misha"
		| ss > ff = "Vasya"
    where ff = score a c
          ss = score b d

score p t = max (3*p/10) (p-p/250*t)

{-# LANGUAGE FlexibleContexts, ScopedTypeVariables, BangPatterns, OverloadedStrings #-}
{-# 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
  [a, b, c, d] <- map read . words <$> getLine

  let
    f p t = max (3*p/10) (p - p/250*t)

  putStrLn $
    case compare (f a c)  (f b d) of
      LT -> "Vasya"
      EQ -> "Tie"
      GT -> "Misha"

main = interact $ f . map read . words
f [a,c,b,d] = case compare (g a b) (g c d) of
  GT -> "Misha"
  LT -> "Vasya"
  EQ -> "Tie"
g p t = max (3*p/10) (p-p*t/250)

import Data.Char

h :: Int -> Int -> Int -> Int -> String
h a b c d
 | (max (div (3*a) 10) (a- (div a 250) * c)) <  (max (div (3*b) 10) (b- (div b 250) * d))  = "Vasya"
 | (max (div (3*a) 10) (a- (div a 250) * c)) >  (max (div (3*b) 10) (b- (div b 250) * d))  = "Misha"
 | otherwise = "Tie"

--main = do
-- line <- getLine
-- print (h line [])

main = do
 line <- getLine
 let a = (read (takeWhile (/= ' ') line) :: Int)
 let line1 = drop 1 (dropWhile (/= ' ') line)
 let b = (read (takeWhile (/= ' ') line1) :: Int)
 let line2 = drop 1 (dropWhile (/= ' ') line1)
 let c = (read (takeWhile (/= ' ') line2) :: Int)

 let d =  (read (drop 1 (dropWhile (/= ' ') line2)) :: Int)
 putStrLn(h a b c d)

import Control.Applicative
import Data.List

main::IO ()
main=do
  [a,b,c,d]<- map read <$> words <$>  getLine
  let a1 = max (div (3*a) 10) (a -(div a 250)*c)
  let b1 = max (div (3*b) 10) (b -(div b 250)*d)
  putStrLn $ if a1==b1 then "Tie" else if a1 <b1 then "Vasya" else "Misha"

main=interact$f.map read.words
f[a,b,c,d]=case compare(g(a,c))$g(b,d)of GT->"Misha";LT->"Vasya";_->"Tie"
g(p,t)=max(3*p`div`10)(p-p`div`250*t)

import Data.Function (on)

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

solve :: [Integer] -> String
solve [ a, b, c, d ] = case (compare `on` score) (a, c) (b, d) of
                            GT -> "Misha"; LT -> "Vasya"; _ -> "Tie"
solve _ = undefined

score :: (Integer, Integer) -> Integer
score (p, t) = max (3 * p `div` 10) (p - p `div` 250 * t)

main = interact $ solve . map read . words
solve [a,b,c,d] = case compare (score a c) (score b d) of
  GT -> "Misha"
  LT -> "Vasya"
  EQ -> "Tie"
score p t = max (3 * p `div` 10) (p - p * t `div` 250)

import Data.Ratio

main = do
    [a,b,c,d] <- fmap (map read . words) getLine
    putStrLn $ solve a b c d

solve a b c d | fstPoints == sndPoints = "Tie"
              | fstPoints > sndPoints = "Misha"
              | sndPoints > fstPoints = "Vasya"
    where fstPoints = points a c
          sndPoints = points b d

points p t = max (3*p%10) (p%1-p*t%250)

main :: IO ()
main = do
       args <- getLine
       let [a1,a2,a3,a4] = map read $ words args
       printSolve a1 a2 a3 a4

printSolve :: Double -> Double -> Double-> Double -> IO ()
printSolve a1 a2 a3 a4
             | m > v = putStrLn "Misha"
             | v > m = putStrLn "Vasya"
             | otherwise = putStrLn "Tie"
             where m = max (3 * a1 /10) (a1 - a1*a3/250)
                   v = max (3 * a2 /10) (a2 - a2*a4/250)

main = interact $ solve . map read . words
solve [a,b,c,d] = case compare (score a c) (score b d) of
  GT -> "Misha"
  LT -> "Vasya"
  EQ -> "Tie"
score p t = max (3 * p `div` 10) (p * t `div` 250)

main :: IO ()
main = do
       args <- getLine
       let [a1,a2,a3,a4] = map read $ words args
       printSolve a1 a2 a3 a4

printSolve :: Float -> Float -> Float -> Float -> IO ()
printSolve a1 a2 a3 a4
             | m > v = putStrLn "Misha"
             | v > m = putStrLn "Vasya"
             | otherwise = putStrLn "Tie"
             where m = max (0.3 * a1) (a1 - a1*a3/250)
                   v = max (0.3 * a2) (a2 - a2*a4/250)

import Data.List

cal ls = show $ winner pv pm
  where  xs = map read $ words $ head $ lines ls
         a = head xs
         b = xs !! 1
         c = xs !! 2
         d = xs !! 3
         point p t = maximum [ p * 0.3, p * ( 1 - t / 250)]
         pm = point a c
         pv = point b d

winner v m
 | v == m = "Tie"
 | v > m = "Vasya"
 | otherwise = "Misha"

main = interact cal


import Data.List (isPrefixOf, isSuffixOf, reverse)

main :: IO ()
main = do
  str <- getLine
  let len = getPrefLen "CODEFORCES" str + getSuffLen "CODEFORCES" str
  let ans = if len >= length "CODEFORCES"
              then "YES"
              else "NO"
  putStrLn ans

getPrefLen :: String -> String -> Int
getPrefLen (x:xs) (y:ys)
  | x == y    = 1 + getPrefLen xs ys
  | otherwise = 0
getPrefLen _ _ = 0

getSuffLen :: String -> String -> Int
getSuffLen x y = getPrefLen (reverse x) (reverse y)

import Control.Applicative
import Data.List

process s x | isPrefixOf x s = "YES"
            | isSuffixOf x s = "YES"
            | otherwise = if (process1 s x) then "YES" else "NO"

process1 [] _ =False
process1 (s:ss) (x:xx) | s==x = process1 ss xx
		       | otherwise = isSuffixOf (x:xx) (s:ss)

main= do
	s<- getLine
	putStrLn $   process s "CODEFORCES"

--isMake::String -> String -> Bool
isMake s w = any id [ (take a w) ++ (drop b w) == s| a <- [0..(length w)], b<-[a..(length w)]]
main = do
  w <- getLine
  putStrLn $ if isMake "CODEFORCES" w then "YES" else "NO"

import Control.Arrow ((&&&), second)
import Data.List (inits, tails)

main :: IO ()
main = getLine >>= putStrLn . yesno . solve

solve :: String -> Bool
solve = elem "CODEFORCES" . concatMap (uncurry (map . (++)) . second tails) . uncurry zip . (inits &&& tails)

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

import Control.Arrow ((&&&), app)
import Data.List (inits, tails)

main :: IO ()
main = getLine >>= putStrLn . yesno . solve

solve :: String -> Bool
solve = elem "CODEFORCES" . concatMap app . uncurry zip . (map (map . (++)) . inits &&& map tails . tails)

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

substrMatch s t = [(take n s) ++ (drop m s) == t | n <- [0..(length s)], m<- [0..(length s)]]

substr s t = any id (substrMatch s t)

solution s
    | substr s "CODEFORCES" = "YES"
    | otherwise = "NO"

main = do
    s <- getLine
    putStrLn $ solution s

isMake [] w = True
isMake s  [] = null s
isMake s@(hs:ts) (hw:tw) | hs == hw = isMake ts tw
                         | otherwise = isMake s tw
main = do
  w <- getLine
  putStrLn $ if isMake "CODEFORCES" w then "YES" else "NO"

main :: IO ()
main = getLine >>= putStrLn . yesno . solve

solve :: String -> Bool
solve = isContainedIn "CODEFORCES"

isContainedIn :: Eq a => [a] -> [a] -> Bool
isContainedIn xxs@(x:xs) (y:ys) | x == y = isContainedIn xs ys
                                | otherwise = isContainedIn xxs ys
isContainedIn [] _ = True
isContainedIn _ _ = False

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

import Control.Applicative

process _ [] = True
process [] _ = False
process (x:xs) (y:ys) | x==y = process xs ys
	              | otherwise = process xs (y:ys)

main= do
	s<- getLine
	putStrLn $ if process s "CODEFORCES" then "YES" else "NO"

import Control.Applicative

process _ [] = True
process [] _ = False
process (x:xs) (y:ys) | x==y = process xs ys
	              | otherwise = process xs ys

main= do
	s<- getLine
	putStrLn $ if process s "CODEFORCES" then "YES" else "NO"

import Control.Applicative
import Data.List

process _ [] = True
process [] _ = False
process (x:xs) (y:ys) | x==y = process xs ys
	              | otherwise = isInfixOf (y:ys) xs

main= do
	s<- getLine
	putStrLn $ if process s "CODEFORCES" then "YES" else "NO"


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

import Control.Applicative
import Control.Monad
import Control.Monad.ST.Safe
import Control.Monad.State
import Data.Array.IArray
import Data.Array.IO.Safe
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Array.Unboxed
import Data.Char
import Data.Function
-- import Data.Graph
import Data.Int
import Data.List
import Data.List.Split
import Data.Maybe
import Data.Set (Set)
import qualified Data.Set as Set
-- import Data.Tree
import Data.Tuple
import Data.Map (Map)
import qualified Data.Map as Map
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import qualified Data.ByteString.Char8 as B
-- import Debug.Trace

import qualified Data.ByteString.Lazy.Builder as BB

import Data.Monoid
import System.IO

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

main = do
  z <- replicateM 5 getInts

  let
    f :: [Int] -> Int
    f [] = 0
    f as = g as + f (tail as)
      where
        g :: [Int] -> Int
        g [] = 0
        g [x] = 0
        g (a:b:as) = z!!a!!b + z!!b!!a + g as

  print $ maximum $ map f $ permutations [0..4]

import Control.Arrow ((&&&))
import Data.List (permutations)
import Data.Tuple (swap)

main :: IO ()
main = getContents >>= print . solve . map (map read . words) . lines

solve :: [[Int]] -> Int
solve gs = maximum [ sum [ gs !! i !! j | (i, j) <- indices is ] | is <- permutations [0..4] ]

indices :: [a] -> [(a, a)]
indices = uncurry (++) . (id &&& map swap) . concat . takeWhile (not . null)
        . iterate (drop 2) . uncurry zip . (id &&& tail)

import Data.List
import Control.Monad

n = 5

calc2 :: [[Int]] -> [Int] -> Int
calc2 a (b:c:d) = (a !! b !! c) + (a !! c !! b) + (calc2 a d)
calc2 _ _ = 0

calc :: [[Int]] -> [Int] -> Int
calc a (c:b) = (calc2 a (c:b)) + (calc a b)
calc _ _ = 0

solve :: [[Int]] -> Int
solve a = maximum $ map (calc a) $ permutations [0..n-1]

main = do
          s <- replicateM 5 $ (getLine >>= (return . (map read) . words))
          print $ solve s

import Data.List

readNStudents :: Int -> IO [[Int]]
readNStudents n
			| n==0 = return []
			| otherwise = do
				student <- readStudent
				others <- readNStudents (n-1)
				return (student:others)

readStudent :: IO [Int]
readStudent =
		do
			line <- getLine
			let happys = map (read :: String -> Int) $ words line
			return happys

calcStep :: [(Int, [Int])] -> Int
calcStep [] = 0
calcStep list
			| length list < 2 = 0
calcStep (x:y:ys) = (snd x)!!(fst y)+(snd y)!!(fst x) + calcStep ys

calcSteps :: [(Int, [Int])] -> Int
calcSteps [] = 0
calcSteps list@(x:xs) = calcStep list + calcSteps xs

main =
	do
		let count = 5
		students <- readNStudents count
		let zipedStudents = zip [0..] students
		let mutations = permutations zipedStudents
		let happies = map calcSteps mutations
		let result = maximum happies
		putStrLn $ show result

module Main where
import Data.List

main =
	do
		let count = 5
		students <- readNStudents count
		let zipedStudents = zip [0..] students
		let mutations = permutations zipedStudents
		let happies = map calcSteps mutations
		let result = maximum happies
		return result

readNStudents :: Int -> IO [[Int]]
readNStudents n
			| n==0 = return []
			| otherwise = do
				student <- readStudent
				others <- readNStudents (n-1)
				return (student:others)

readStudent :: IO [Int]
readStudent =
		do
			line <- getLine
			let happys = map (read :: String -> Int) $ words line
			return happys

calcStep :: [(Int, [Int])] -> Int
calcStep [] = 0
calcStep list
			| length list < 2 = 0
calcStep (x:y:ys) = (snd x)!!(fst y)+(snd y)!!(fst x) + calcStep ys

calcSteps :: [(Int, [Int])] -> Int
calcSteps [] = 0
calcSteps list@(x:xs) = calcStep list + calcSteps xs


import Control.Monad
import Data.Bool
import Data.Function
import Data.Tuple
import Data.Maybe
import Data.Tree
import Data.Graph
import Data.Array.IArray
import Data.List
import Data.Int
import Data.List.Split
import qualified Data.ByteString.Char8 as B

iogetints :: IO [Int64]
iogetints = getLine >>= (return . map read . words)

freadEdgeList :: IO [Edge]
freadEdgeList = B.getContents >>= (return . pairify . map (fst . fromJust . B.readInt) . B.words)

indexed :: [a] -> [(Int, a)]
indexed = go 0 where
	go :: Int -> [a] -> [(Int, a)]
	go _ [] = []
	go i (x:xs) = (i, x) : (go (succ i) xs)

foldTree :: (a -> [b] -> b) -> Tree a -> b
foldTree f = go where
	go (Node x ts) = f x (map go ts)

pairify :: [a] -> [(a, a)]
pairify [] = []
pairify (x:y:xs) = (x, y) : (pairify xs)

buildTree :: Int -> Vertex -> [Edge] -> Tree Vertex
buildTree n r es = fdfs (-1) r where
	g = accumArray (flip (:)) [] (1, n) $ ap (++) (map swap) $ es :: Graph
	fdfs :: Vertex -> Vertex -> Tree Vertex
	fdfs p v = Node v $ map (fdfs v) $ delete p $ g!v

both :: (a -> b) -> (a, a) -> (b, b)
both f (x, y) = (f x, f y)

solve :: Int64 -> Int64 -> Int64
solve n k = ans where
	le = max (k - n) 1
	re = min (k - 1) n
	len = max (re - le + 1) 0
	ans = len `div` 2

main :: IO ()
main = do
	[n, k] <- iogetints
	print $ solve n k

toint s = (read s) :: Integer

doit n k =
  if n == 1 then
    0
  else
    if n >= k-1 then
      div (k-1) 2
    else
      if n + n-1 < k then
        0
      else
        div (k-1) 2 - (k-1-n)

solve::String -> String
solve ss =
  let n:k:_ = map toint $ words ss in
  (show $ doit n k) ++ "\n"

main = interact solve

import Control.Monad
import Data.Bool
import Data.Function
import Data.Tuple
import Data.Maybe
import Data.Tree
import Data.Graph
import Data.Array.IArray
import Data.List
import Data.Int
import Data.List.Split
import qualified Data.ByteString.Char8 as B

iogetints :: IO [Int64]
iogetints = getLine >>= (return . map read . words)

freadEdgeList :: IO [Edge]
freadEdgeList = B.getContents >>= (return . pairify . map (fst . fromJust . B.readInt) . B.words)

indexed :: [a] -> [(Int, a)]
indexed = go 0 where
	go :: Int -> [a] -> [(Int, a)]
	go _ [] = []
	go i (x:xs) = (i, x) : (go (succ i) xs)

foldTree :: (a -> [b] -> b) -> Tree a -> b
foldTree f = go where
	go (Node x ts) = f x (map go ts)

pairify :: [a] -> [(a, a)]
pairify [] = []
pairify (x:y:xs) = (x, y) : (pairify xs)

buildTree :: Int -> Vertex -> [Edge] -> Tree Vertex
buildTree n r es = fdfs (-1) r where
	g = accumArray (flip (:)) [] (1, n) $ ap (++) (map swap) $ es :: Graph
	fdfs :: Vertex -> Vertex -> Tree Vertex
	fdfs p v = Node v $ map (fdfs v) $ delete p $ g!v

both :: (a -> b) -> (a, a) -> (b, b)
both f (x, y) = (f x, f y)

solve :: Int64 -> Int64 -> Int64
solve n k = ans where
	le = max (k - n) 0
	re = n
	len = max (re - le + 1) 0
	ans = if (even k && (k `div` 2) >= le && (k `div` 2) <= re) then (len - 1) else len

main :: IO ()
main = do
	[n, k] <- iogetints
	print $ solve n k


check :: Int -> (Int, Int, Int) -> Bool
check n (na, nb, nc) =
  if (n + na + 2 * nb + 3 * nc) `mod` 4 == 0
  then
    True
  else
    False

price :: Integer -> Integer -> Integer -> (Int, Int, Int) -> Integer
price a b c (na, nb, nc) = a * (toInteger na) + b * (toInteger nb) + c * (toInteger nc)

cartProd :: [Int] -> [Int] -> [Int] -> [(Int, Int, Int)]
cartProd xs ys zs = [(x, y, z) | x <- xs, y <- ys, z <- zs]

goods :: Int -> [(Int, Int, Int)] -> [(Int, Int, Int)]
goods n xs = filter (check n) xs

gen :: [(Int, Int, Int)]
gen = cartProd [0..10] [0..10] [0..10]

calc :: Int -> Integer -> Integer -> Integer -> Integer
calc n a b c = foldr min ((toInteger (maxBound :: Int)) * 10) (map (price a b c) (goods n gen))

main = do
  cs <- getContents
  let vals = words (head (lines cs))
  let n = read (vals!!0) :: Int
  let a = read (vals!!1) :: Integer
  let b = read (vals!!2) :: Integer
  let c = read (vals!!3) :: Integer
  -- putStrLn (show (goods n gen)) -- debug
  putStrLn (show (calc n a b c))

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}

import Control.Applicative
import Control.Monad
import Control.Arrow
import Data.List
import Data.Maybe
import Data.Char
import qualified Data.ByteString.Char8 as B
import Text.Printf

readInt = ( readLn :: IO Int )
readInts = map ( read :: String -> Int ) . words <$> getLine

getList = map ( fst . fromJust . B.readInt ) . B.words <$> B.getLine

which a b f = if f then a else b
mp [ a, b ] = ( a, b )

main = do
	[ n, a, b, c ] :: [ Integer ] <- map read . words <$> getLine
	print $ minimum $ map snd $ filter ( \( k, _ ) -> k `mod`4 == 0 ) $ return ( n, 0 ) >>= f 1 a >>= f 2 b >>= f 3 c

f n a ( m, b ) = do
	x <- [ 0 .. 3 ]
	return ( m + n * x, b + x * a )

main=interact(show.f.map read.words)
f :: [Integer] -> Integer
f (x:(xs@[a,b,c]))
 | x`mod`4 == 0 = 0
 | x`mod`4 == 3 = foldr min (foldr max 0 xs) [a, b + c, 3*c]
 | x`mod`4 == 2 = foldr min (foldr max 0 xs) [2*a, b, 2*c]
 | otherwise    = foldr min (foldr max 0 xs) [3*a, b+a, c]

module Main (main) where

main :: IO ()
main = do
  l <- getLine
  let n:a:b:c:_ = map read $ words l :: [Integer]
  let t = (-n) `mod` 4
  print $ minimum [y1 * a + y2 * b + y3 * c | y1 <- [0..3], y2 <- [0..3], y3 <- [0..3], (y1+2*y2+3*y3) `mod` 4 == t]

import Control.Applicative

main = do
	[n,a,b,c] <- map read.words <$> getLine
	putStrLn.show $ buyPlan (need n) a b c

need :: Integer -> Integer
need n = 4 - n `mod` 4

buyPlan 4 _ _ _ = 0
buyPlan 1 a b c = foldl min (3*maxV a b c) [a, 3*a+b, b+c,3*c]
buyPlan 2 a b c = foldl min (3*maxV a b c) [a*2, b, 2*c]
buyPlan 3 a b c = foldl min (3*maxV a b c) [a*3,a+b,c,a+2*c]
maxV a b c = foldl max 0 [a,b,c]

import Control.Applicative
import Control.Monad

fun2 0 _ _ _ = 0
fun2 1 a b c = min (min (3*a) (b+a)) c
fun2 2 a b c = min (min (2*a) (2*c)) b
fun2 3 a b c = min (min (b+c) (3*c)) a

main = do [n,a,b,c] <- map read.words <$> getLine
          putStrLn.show $ fun2 (mod n 4) a b c

import Control.Applicative
import Control.Monad

fun2 0 _ _ _ = 0
fun2 1 a b c = min (min (3*a) (b+a)) c
fun2 2 a b c = (2*a) `min` (2*c) `min` b
fun2 3 a b c = (b+c) `min` (3*c) `min` a

main = do [n,a,b,c] <- map read.words <$> getLine
          putStrLn.show $ fun2 (mod n 4) a b c

main=interact(show.f.map read.words)
f :: [Integer] -> Integer
f (x:(xs@[a,b,c]))
 | x`mod`4 == 0 = 0
 | x`mod`4 == 3 = a
 | x`mod`4 == 2 = foldr min (foldr max 0 xs) [2*a, b]
 | otherwise    = foldr min (foldr max 0 xs) [3*a, b+a, c]

main=interact(show.f.map read.words)
f :: [Integer] -> Integer
f (x:[a,b,c])
 | x`mod`4 == 0 = 0
 | x`mod`4 == 3 = a
 | x`mod`4 == 2 = foldr min (4*b) [2*a, b]
 | otherwise    = foldr min (4*b) [3*a, b+a, c]

check :: Int -> (Int, Int, Int) -> Bool
check n (na, nb, nc) =
  if (n + na + 2 * nb + 3 * nc) `mod` 4 == 0
  then
    True
  else
    False

price :: Int -> Int -> Int -> (Int, Int, Int) -> Int
price a b c (na, nb, nc) = a * na + b * nb + c * nc

cartProd :: [Int] -> [Int] -> [Int] -> [(Int, Int, Int)]
cartProd xs ys zs = [(x, y, z) | x <- xs, y <- ys, z <- zs]

goods :: Int -> [(Int, Int, Int)] -> [(Int, Int, Int)]
goods n xs = filter (check n) xs

gen :: [(Int, Int, Int)]
gen = cartProd [0..10] [0..10] [0..10]

calc :: Int -> Int -> Int -> Int -> Int
calc n a b c = foldr min (maxBound :: Int) (map (price a b c) (goods n gen))

main = do
  cs <- getContents
  let vals = words (head (lines cs))
  let n = read (vals!!0) :: Int
  let a = read (vals!!1) :: Int
  let b = read (vals!!2) :: Int
  let c = read (vals!!3) :: Int
  -- putStrLn (show (goods n gen)) -- debug
  putStrLn (show (calc n a b c))

module Main (main) where

main :: IO ()
main = do
  l <- getLine
  let n:a:b:c:_ = map read $ words l :: [Integer]
  let t = (-n) `mod` 4
  print $ minimum [y1 * a + y2 * b + y3 * c | y1 <- [0,1,2,3], y2 <- [0,1,2], y3 <- [0,1], y1+2*y2+3*y3 == t]

module Main (main) where

main :: IO ()
main = do
  l <- getLine
  let n:a:b:c:_ = map read $ words l :: [Integer]
  let t = n `mod` 4
  print $ minimum [y1 * a + y2 * b + y3 * c | y1 <- [0,1,2,3], y2 <- [0,1,2], y3 <- [0,1], y1+2*y2+3*y3 == t]

import Control.Applicative

main = do
	[n,a,b,c] <- map read.words <$> getLine
	putStrLn.show $ buyPlan (need n) a b c

need :: Integer -> Integer
need n = 4 - n `mod` 4

buyPlan 4 _ _ _ = 0
buyPlan 1 a b c = a
buyPlan 2 a b c = if a*2 < b then a*2 else b
buyPlan 3 a b c = foldl min (3*maxV) [a*3,a+b,c] where
	maxV = foldl max 0 [a,b,c]

import Control.Applicative

main = do
	[n,a,b,c] <- map read.words <$> getLine
	putStrLn.show $ buyPlan (need n) a b c

need :: Integer -> Integer
need n = 4 - n `mod` 4

buyPlan 4 _ _ _ = 0
buyPlan 1 a b c = foldl min (3*maxV a b c) [a, 3*a+b, b+c]
buyPlan 2 a b c = foldl min (3*maxV a b c) [a*2, b, 2*c]
buyPlan 3 a b c = foldl min (3*maxV a b c) [a*3,a+b,c,a+2*c]
maxV a b c = foldl max 0 [a,b,c]

import Control.Applicative

main = do
	[n,a,b,c] <- map read.words <$> getLine
	putStrLn.show $ buyPlan (need n) a b c

need :: Int -> Int
need n = 4 - n `mod` 4

buyPlan 4 _ _ _ = 0
buyPlan 1 a b c = a
buyPlan 2 a b c = if a*2 < b then a*2 else b
buyPlan 3 a b c = foldl min (3*maxV) [a*3,a+b,c] where
	maxV = foldl max 0 [a,b,c]

import Control.Applicative

main = do
	[n,a,b,c] <- map read.words <$> getLine
	putStrLn.show $ buyPlan (need n) a b c

need :: Int -> Int
need n = 4 - n `mod` 4

buyPlan 4 _ _ _ = 0
buyPlan 1 a b c = a
buyPlan 2 a b c = if a*2 < b then a*2 else b
buyPlan 3 a b c = foldl min (3*maxV) [a*3,a*2+b,c] where
	maxV = foldl max 0 [a,b,c]

import Control.Applicative

main = do
	[n,a,b,c] <- map read.words <$> getLine
	putStrLn.show $ buyPlan (need n) a b c

need :: Integer -> Integer
need n = 4 - n `mod` 4

buyPlan 4 _ _ _ = 0
buyPlan 1 a b c = foldl min (3*maxV a b c) [a, b, c]
buyPlan 2 a b c = foldl min (3*maxV a b c) [a*2, b,c]
buyPlan 3 a b c = foldl min (3*maxV a b c) [a*3,a+b,2*b,a*2+b,c]
maxV a b c = foldl max 0 [a,b,c]

import Control.Applicative
import Control.Monad

fun2 0 _ _ _ = 0
fun2 1 a b c = min (min (3*a) (b+a)) c
fun2 2 a b c = min (min (2*a) (2*c)) b
fun2 3 a b c = min a (b+c)

main = do [n,a,b,c] <- map read.words <$> getLine
          putStrLn.show $ fun2 (mod n 4) a b c


main = do
    line <- getLine
    let n1::Int
        n2::Int
        [n1, n2, _, _] = map read $words line
    if n1 > n2 then
        putStrLn "First"
    else
        putStrLn "Second"

solve :: Int -> Int -> String
solve n1 n2
    | n1 > n2 = "First"
    | otherwise = "Second"

main = do
  str <- getLine
  let [ n1, n2, _, _ ] = map read $ words str
  putStrLn $ solve n1 n2

readNums :: IO [Int]
readNums = getLine >>= return . map read . words

main = do
  [n1, n2, k1, k2] <- readNums
  putStrLn $ if n1 > n2 then "First" else "Second"

main = do
  [n1, n2, k1, k2] <- fmap (map read . words) getLine :: IO [Int]

  putStrLn $ if n1 > n2 then "First" else "Second"

import Data.List
import Data.Tree
import Data.Int
import Data.Maybe
import Data.Char
import Data.Functor
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 = solve =<< (map (fst.fromJust.C.readInt).C.words <$>C.getLine)
solve [x,y,z,zz] = putStr $ if x>y then "First" else "Second"

main=interact$f.map read.words
f (a:b:c)=if a+0>b then "First" else "Second"

main :: IO ()
main = getLine >>= print . solve . map read . words

data Player = First | Second deriving Show

solve :: [Int] -> Player
solve (n1:n2:_) = if n1 > n2 then First else Second

main = interact $ solve . map read . words
solve[n1,n2,k1,k2]|n1<=(n2::Int)="Second"|otherwise="First"

main = do
  input <- getLine
  let (a:b:_) = map read $ words input :: [Int]
  if a>b then putStrLn "First" else putStrLn "Second"

main = do
    line <- getLine
    let n1:n2:_ = map read (words line) :: [Int]
        result = if n1 > n2 then "First" else "Second"
    putStrLn result

import System.IO
import Control.Monad
import Control.Applicative

main :: IO ()
main = do
    [n1, n2, k1, k2] <- map read <$> words <$> getLine :: IO [Int]
    putStrLn $ if n1 > n2
                 then "First"
                 else "Second"

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

main=do
	[a,b,c,d]<- map read. words <$> getLine  ::IO [Int]
	putStrLn $ if (a<=b) then "Second"
                 else "First"

main = do
  [n1, n2, _, _] <- getLine >>= return. map read. words :: IO [Int]
  putStrLn $ if n1 > n2 then "First" else "Second"

module Main where

main :: IO ()
main = do
    line <- getLine
    let [n1, n2, _, _] = map (\c -> read c :: Int) (words line)
    let winner = case n1 > n2 of
            True -> "First"
            False -> "Second"
    putStrLn winner

module Main where

main = do
  c <- getContents
  let (n1:n2:k1:k2:_) = map (read :: String -> Integer) $ words c
  putStrLn $ solve n1 n2 k1 k2

solve n1 n2 k1 k2
  | n1 == n2   = "Second"
  | n1 >  n2   = "First"
  | n1 <  n2   = "Second"

solve n1 n2 k1 k2
  | n1<=n2 ="Second"
  | n1>n2 = "First"
  where m1= div n1 k1
        m2= div n2 k2

main = do
  d<-getLine
  let [n1,n2,k1,k2]=map read $ words d
  putStrLn $ solve n1 n2 k1 k2

main = do
  line <- getLine
  let
    l = map (read :: String -> Int) $ words line
    a = head l
    b = head $ tail l
    in putStrLn (if a <= b then "Second" else "First")

import Data.Functor
import Control.Applicative
import Control.Monad

determine :: [Int] -> String
determine [n1, n2, k1, k2]
  | n1 > n2 = "First"
  | otherwise = "Second"

main :: IO ()
main = determine <$> map read <$> words <$> getLine >>= putStrLn

solve n1 n2 k1 k2
  | n1<=n2 ="Second"
  | n1>n2 = "First"
  where m1= div n1 k1
        m2= div n2 k2

main = do
  d<-getLine
  let [n1,n2,k1,k2]=map read $ words d
  putStrLn $ show $ solve n1 n2 k1 k2

solve :: Int -> Int -> String
solve n1 n2
    | n1 > n2 = "First"
    | otherwise = "Second"

main = do
  str <- getLine
  let [ n1, n2, _, _ ] = map read $ words str
  print $ solve n1 n2

main = do
  input <- getLine
  let (a:b:_) = map read $ words input :: [Int]
  if a<b then putStrLn "First" else putStrLn "Second"

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

main=do
	[a,b,c,d]<- map read. words <$> getLine  ::IO [Int]
	print $ if (a<=b) then "Second"
                 else "First"


{-# OPTIONS_GHC -O2 #-}
import Data.Int (Int64)
main = interact $ show.solve.map read.words
solve:: [Int64]->Int64
solve [a,b,k] =  go 1 (a*b)
	where   (a',b') = (min a b, max a b)
		go l r
			| l>r  = l
			| s<k  = go (m+1) r
			| otherwise = go l (m-1)
			where m = (l+r) `quot` 2
		      	      s = foldl (\acc x-> acc+min b' (quot m x)) 0 [1..a']

{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE BangPatterns #-}

import Data.Int
import Data.List

main=interact$show.f.map read.words

f :: [Int64] -> Int64
f[n,m,k]
  | n > m = f [m,n,k]
  | otherwise = lowerBound p 1 $ n * m
   where
     p x = go 0 1
      where
        go !acc !i
          | i <= n = go (acc + min m (quot x i)) (i+1)
          | otherwise = k <= acc

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

{-# OPTIONS_GHC -O2 #-}
import Data.Int (Int64)
main = interact $ show.solve.map read.words
solve:: [Int64]->Int64
solve [a,b,k] =  go 1 (a*b)
	where   (a',b') = (min a b, max a b)
		xs=[1..a']
		go l r
			| l>r  = l
			| s<k  = go (m+1) r
			| otherwise = go l (m-1)
			where m = (l+r) `quot` 2
		      	      s = foldl (\acc x-> acc+min b' (quot m x)) 0 xs

{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE BangPatterns #-}

import Data.Int
import Data.List

main=interact$show.f.map read.words

f :: [Int64] -> Int64
f[n,m,k]
  | n > m = f [m,n,k]
  | otherwise = lowerBound p 1 $ n * m
   where
     p x = k <= foldl' (\acc i -> acc + min m (quot x i)) 0 [1..n]

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

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies        #-}
{-# LANGUAGE ViewPatterns        #-}
{-# LANGUAGE BangPatterns        #-}

import           Control.Applicative
import           Control.Arrow
import           Control.Monad
import           Data.Array.IO
import           Data.Array.Unboxed
import qualified Data.ByteString.Char8       as C
import           Data.Char
import qualified Data.Foldable               as F
import           Data.Function
import           Data.Functor
import           Data.Int
import qualified Data.IntMap                 as M
import           Data.IORef
import           Data.List
import           Data.Maybe
import           Data.Monoid
import qualified Data.Sequence               as Seq
import qualified Data.Set                    as S
import           Data.Word
import           Debug.Trace
import           Text.Printf

-- I/O

readInts :: C.ByteString -> [Int]
readInts = map fst . mapMaybe C.readInt . C.words

getInts :: IO [Int]
getInts = readInts <$> C.getLine

readNums :: Num a => C.ByteString -> [a]
readNums = map (fromIntegral . fst) . mapMaybe C.readInteger . C.words

getNums :: Num a => IO [a]
getNums = readNums <$> C.getLine

cast :: (Integral a, Integral b) => a -> b
cast = fromIntegral

-----

main :: IO ()
main = do
  [nn, mm, k] <- getNums

  let go :: Int64 -> Int64 -> Int64
      go !l !r
        | r - l <= 1 = r
        | mv < k = go m r
        | otherwise = go l m
        where
          !m = (l+r)`div`2
          !mv = calc m 1 0

      calc !x !i !acc
        | i > nn = acc
        | otherwise =
          calc x (i+1) $ acc + min mm (x`div`i)

  print (go 0 (nn*mm) :: Int64)

import Data.Int (Int64)
main = interact $ show.solve.map read.words
solve:: [Int64]->Int64
solve [a,b,k] =  go 1 (a*b)
	where   (a',b') = (min a b, max a b)
		go l r
			| l>r  = r
			| s<=k  = go (m+1) r
			| otherwise = go l (m-1)
			where m = (l+r) `quot` 2
		      	      s = sum [min b' $ quot m x | x<-[1..a']]

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies        #-}
{-# LANGUAGE ViewPatterns        #-}
{-# LANGUAGE BangPatterns        #-}

import           Control.Applicative
import           Control.Arrow
import           Control.Monad
import           Data.Array.IO
import           Data.Array.Unboxed
import qualified Data.ByteString.Char8       as C
import           Data.Char
import qualified Data.Foldable               as F
import           Data.Function
import           Data.Functor
import           Data.Int
import qualified Data.IntMap                 as M
import           Data.IORef
import           Data.List
import           Data.Maybe
import           Data.Monoid
import qualified Data.Sequence               as Seq
import qualified Data.Set                    as S
import           Data.Word
import           Debug.Trace
import           Text.Printf

-- I/O

readInts :: C.ByteString -> [Int]
readInts = map fst . mapMaybe C.readInt . C.words

getInts :: IO [Int]
getInts = readInts <$> C.getLine

readNums :: Num a => C.ByteString -> [a]
readNums = map (fromIntegral . fst) . mapMaybe C.readInteger . C.words

getNums :: Num a => IO [a]
getNums = readNums <$> C.getLine

cast :: (Integral a, Integral b) => a -> b
cast = fromIntegral

-----

main :: IO ()
main = do
  [nn, mm, k] <- map cast <$> getInts

  let go :: Int64 -> Int64 -> Int64
      go !l !r
        | r - l <= 1 = r
        | mv < k = go m r
        | otherwise = go l m
        where
          m = (l+r)`div`2
          mv = calc m 1 0

      calc !x !i !acc
        | i > nn = acc
        | otherwise =
          calc x (i+1) $ acc + min mm (x`div`i)

  print (go 0 (nn*mm) :: Int64)


solve [a,b] = b*(b-1) `div` 2  * ( (b*a+1+b+1)*a `div` 2)

main = interact $ show . (`mod` (10^9+7)) .  solve . map read . words

import Control.Applicative

main = do
  [a, b] <- fmap read . words <$> getLine :: IO [Integer]
  let x = b*(b-1)`div`2*(a+a*(a+1)`div`2*b)
  putStrLn.show $ x `mod` 1000000007

main :: IO ()
main = getLine >>= print . solve . map read . words

-- x = p * b + q
-- 1 <= q <= b - 1
-- 1 <= k <= a
-- p = q * k
solve :: [Integer] -> Integer
solve [a, b] = ((a + a * (a + 1) * b `div` 2) * (b * (b - 1) `div` 2)) `mod` 1000000007

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns, TupleSections, OverloadedStrings #-}
import Control.Applicative
import Control.Exception
import Control.Monad
import Control.Monad.ST
import Data.Array.Base
import Data.Array.ST (runSTUArray, STArray, runSTArray)
import Data.Bits
import qualified Data.ByteString.Char8 as B
import Data.Char
import Data.Function
import Data.Int
import Data.List
import qualified Data.Map as M
import qualified Data.Set as S
import Data.STRef
import GHC.Arr (Array, Ix, range, unsafeIndex)
bool :: a -> a -> Bool -> a
bool t f b=if b then t else f
readInt :: B.ByteString -> Int
readInt bs=case B.readInt bs of{Just(n,_)->n;_->error$"readInt error : bs = "++show bs;}
rep, rev :: Monad m => Int -> (Int -> m ()) -> m ()
rep !n f=go 0 where go !i=when(i<n)$f i>>go(i+1)
rev !n f=go(n-1)where go !i=when(i>=0)$f i>>go(i-1)
{-# INLINE rep #-}
{-# INLINE rev #-}
for :: Monad m => Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> m ()) -> m ()
for !i0 p next f=go i0 where go !i=when(p i)$f i>>go(next i)
{-# INLINE for #-}
modifyArray :: (MArray a e m, Ix i) => a i e -> i -> (e -> e) -> m ()
modifyArray a i f=readArray a i>>=writeArray a i.f
{-# INLINE modifyArray #-}
unsafeModify :: (MArray a e m, Ix i) => a i e -> Int -> (e -> e) -> m ()
unsafeModify a i f=unsafeRead a i>>=unsafeWrite a i.f
{-# INLINE unsafeModify #-}

main :: IO ()
main = do
  [a,b] <- map read.words <$> getLine
  print $ solve a b

solve :: Integer -> Integer -> Integer
solve a b = (a*(a+1)*b*(b-1)*b`div`4 + b*(b-1)*a `div` 2) `mod` 1000000007

niceSum :: [Integer] -> Integer
niceSum [a, b] = product [(a+1) * b + 2, a, b, b-1] `div` 4 `mod` 1000000007

main = do
    getLine >>= print . niceSum . fmap read . words

solve [a,b] = b*(b-1) `div` 2  * ( (b*a+1+a+1)*a `div` 2)

main = interact $ show . (`mod` (10^9+7)) .  solve . map read . words

niceSum :: [Integer] -> Integer
niceSum [a, b] = (`mod` 10000007) $ sum [r * (b*k + 1) | r <- [1..b-1], k <- [1..a]]

main = do
    getLine >>= print . niceSum . fmap read . words

niceSum :: [Int] -> Int
niceSum [a, b] = sum [r * (b*k + 1) | r <- [1..b-1], k <- [1..a]]

main = do
    getLine >>= print . niceSum . fmap read . words

nice :: [Integer] -> [Integer]
nice [a, b] = [x | x <- [1..a*b],
    mod x b /= 0,
    mod (quot x b) (mod x b) == 0,
    div (quot x b) (mod x b) >= 1,
    div (quot x b) (mod x b) <= a]

main = getLine >>= print . flip mod (10^9 + 7) . sum . nice . map read . words

niceSum :: [Int] -> Int
niceSum [a, b] = (`mod` 10000007) $ sum [r * (b*k + 1) | r <- [1..b-1], k <- [1..a]]

main = do
    getLine >>= print . niceSum . fmap read . words