{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}

import Data.List
import Data.Array.ST
import Control.Monad.ST
import Data.Array.Unboxed (UArray,(//))
import Control.Monad
import Data.Array (array)
import Data.Array.IArray ((!))

import qualified Data.ByteString.Char8 as BS
import Data.ByteString (ByteString)
import Data.Char

readInt :: IO Int
readInt = do line <- BS.getLine
             let Just (n,_) = BS.readInt line
             return n

parseInts :: Int -> ByteString -> [Int]
parseInts k bs | k <= 0 = []
parseInts k bs = case BS.readInt (BS.dropWhile isSpace bs) of
                   Nothing -> []
                   Just (n,bs') -> n : parseInts (k-1) bs'

type Foo s = ST s (STUArray s Int Int)

countDigits :: ByteString -> UArray Int Int
countDigits bs = runSTUArray $ do
  arr <- newArray (0,9) 0 :: Foo s
  let len = BS.length bs
  let go i | i >= len = return ()
      go i = do let ch = BS.index bs i
                when (ch >= '0' && ch <= '9') $ do
                  let c = digitToInt ch
                  v <- readArray arr c
                  writeArray arr c (v+1)
                  go (i+1)
  go 0
  return arr

common carr d = min (carr!d) (carr!(9-d))
extras carr d = (carr!d) - common carr d

remove carr a = let c = carr!a in carr // [ (a,c-1) ]

test :: ByteString -> IO ()
test bs = do
  let (n1,n2) = solve bs
  putStr "n1: "
  BS.putStrLn $ showNumber n1
  putStr "n2: "
  BS.putStrLn $ showNumber n2

solve :: ByteString -> ( [(Int,Int)], [(Int,Int)] )
solve bs = do
  case best of
    Nothing -> let first = [ (d,counts!d) | d <- [9,8..0], counts!d > 0 ] in (first,first)
    Just a ->
      let b = 10-a
          c1    = remove counts a
          c2    = remove counts b
          comm d = min (c1!d) (c2!(9-d))

          z1 = c1!0 - comm 0
          z2 = c2!0 - comm 9
          z = min z1 z2

          mid1 = [ (d,x) | d <- [0..9],   let x = comm d, x > 0 ]
          mid2 = [ (d,x) | d <- [9,8..0], let x = comm (9-d), x > 0 ]

          lead1 = [ (d,x) | d <- [1..9], let x = c1!d - comm d, x > 0 ] ++ [(0,z1-z)]
          lead2 = [ (d,x) | d <- [1..9], let x = c2!d - comm (9-d), x > 0 ] ++ [(0,z2-z)]

          first   = lead1 ++ mid1 ++ [(a,1)] ++ [(0,z)]
          second  = lead2 ++ mid2 ++ [(b,1)] ++ [(0,z)]
      in (first,second)
  where
    counts = countDigits bs
    cost :: Int -> Int -> Int
    cost a b | counts!a == 0  = 100
             | counts!b == 0  = 100
             | extras counts a == 0  = if extras counts b == 0 then 2 else 1
             | extras counts b == 0  = 1
             | otherwise      = 0
    cost5 = if | counts!5 == 0        -> 100
               | extras counts 5 >= 1 -> 0
               | otherwise            -> 1
    tens = sort $ [ (cost d (10-d),d) | d <- [1..4] ] ++ [ (cost5,5) ]
    best = let (c,d) = head tens in if c > 2 then Nothing else Just d

showNumber :: [(Int,Int)] -> ByteString
showNumber ns = BS.concat $ map (\(d,c) -> BS.replicate c (intToDigit d)) ns

main = do
  bs <- BS.getLine
  let (n1,n2) = solve bs
  BS.putStrLn $ showNumber n1
  BS.putStrLn $ showNumber n2


main :: IO ()
main = interact $ show . solve . map read . words

solve :: [Integer] -> Integer
solve [x, y, m] = f 0 (min x y) (max x y)
  where
  f s x y
    | y >= m = s
    | y <= 0 = -1
    | x >= 0 = seq s1 $ seq xy $ f s1 y xy
    | otherwise = seq ss $ seq xx $ seq yy $ f ss xx yy
    where
    s1 = s + 1
    xy = x + y
    d = -(x `div` y)
    ss = s + d
    xx = min (x + d * y) y
    yy = max (x + d * y) y


import Control.Monad
import Data.List
main = do
    t   <-               readLn :: IO Int
    tcs <- replicateM t (readLn :: IO Int)
    mapM (putStrLn . solve) tcs
solve s = concatMap show digs
  where
    (0, digs) = foldl' next (s,[]) . reverse $ [1..9]
next (s,digs) d | (d > s)    = (s,digs)
                | otherwise  = (s - d, d:digs)


{-# LANGUAGE OverloadedStrings #-}
import qualified Data.ByteString.Char8 as S
import Data.Maybe
import Control.Monad
import Control.Applicative
import Data.List
import Data.Int
import Data.Char
import Data.Bits
import Data.Bool
import Data.Ratio

solve x h m
  | m < 0 = solve x (h - 1) (m + 60)
  | h < 0 = solve x (h + 24) m
  | elem '7' (show $ h * 100 + m) = 0
  | otherwise = 1 + solve x h (m - x)

main = do
  x <- readLn::IO Int
  [h,m] <- (map $ fst . fromJust . S.readInt) . S.words <$> S.getLine
  print $ solve x h m


-- import Debug.Trace

main :: IO ()
main = getLine >>= return . (read :: String -> Int) >>= return . (calc 1 0) >>= print

calc :: Int -> Int -> Int -> Int
calc idx cnt req
  | idx == req+1 = cnt
  | otherwise = calc (idx+1) (dfs idx 1 0 (m `div` 2) + cnt) req
    where
      m = last $ takeWhile (idx >= ) $ map (2^) [0, 1..]

dfs c b a m
  | m == 0 = -- trace (show [c, b, a, m]) $
        if c > b && a + b > c && c + b > a then 1 else 0
  | otherwise = -- trace (show [c, b, a, m]) $
                case curBit of
                  0 -> dfs c (b*2+1) (a*2+1) (m `div` 2) + dfs c (b*2) (a*2) (m `div` 2)
                  1 -> dfs c (b*2+1) (a*2) (m `div` 2) + dfs c (b*2) (a*2+1) (m `div` 2)
  where
    curBit = c `div` m `mod` 2


import Data.List (sort)

process :: [Int] -> [Int]
process = sort

readInt :: String -> Int
readInt = read

main = do
    n <- fmap readInt getLine
    xs <- fmap (map readInt.words) getLine
    putStrLn.unwords.map show $ process xs


import Data.Array
main = interact $ show . solve . map read . words
solve [n,k] = foldl1 (+!) $ iterate f (listArray (1,n) (1 : repeat 0)) !! k where
  f a = accumArray (+!) 0 (1,n) $ concatMap (\(b,i) -> map (flip (,) i) [b,2*b..n]) $ assocs a
a +! b = (a + b) `mod` (10^9+7)


import Data.Array

digits :: Integer -> [Int]
digits 0 = []
digits x = [fromIntegral (x `mod` 10)] ++ digits(x `div` 10)

mylcm :: Int -> Int -> Int
mylcm x 0 = x
mylcm x y = lcm x y

lcms :: Array Int Int
lcms = listArray (0, 47) [2^c2 * 3^c3 * 5^c5 * 7^c7 |
    c2 <- [0..3],
    c3 <- [0..2],
    c5 <- [0..1],
    c7 <- [0..1]]

lcm_index :: Array Int Int
lcm_index = array (1, 2520)
    [(e, i) | (i, e) <- assocs lcms]

memoized_count :: Array (Int, Int, Int) Integer
memoized_count = listArray ((0, 0, 0), (18, 47, 503))
    [f pos l r | pos <- [0..18], l <- [0..47], r <- [0..503]]
        where f pos l r =
                sum [ count (pos-1) nl nr
                    | d <- [0..9],
                      let nl = lcm_index ! mylcm (lcms ! l) d,
                      let nr = (r*10 + d) `mod` (if pos == 0 then 2520 else 504)
                    ]

count :: Int -> Int -> Int -> Integer
count (-1) l r = (if r `mod` (lcms ! l) == 0 then 1 else 0)
count pos l r = memoized_count ! (pos, l, r)

count_s :: [Int] -> Int -> Int -> Int -> Integer
count_s num (-1) l r = (if r `mod` (lcms ! l) == 0 then 1 else 0)
count_s num pos l r =
        sum [ fun (pos-1) nl nr
            | d <- [0..num!!pos],
              let nl = lcm_index ! mylcm (lcms ! l) d,
              let nr = (r*10 + d) `mod` (if pos == 0 then 2520 else 504),
              let fun = if d == num!!pos then count_s num else count
            ]

solve 0 = do
    return ()
solve x = do
    line <- getLine
    let [l, r] = map read $ words line :: [Integer]

    let sr = digits(r)
    let sl = digits(l-1)
    let ansr = count_s sr ((length sr)-1) 0 0
    let ansl = count_s sl ((length sl)-1) 0 0
    print(ansr - ansl)

    solve(x-1)

main = do
    line <- getLine
    let tests = read line :: Int
    solve(tests)


module Main where

import Data.List ( sort )

readChar :: Char -> Int
readChar c = read [c]

toInts :: [Char] -> [Int]
toInts = map readChar

allGoodTickets :: [[Int]]
allGoodTickets = filter isSolution [[x1,x2,x3,x4,x5,x6] | x1 <- [0..9], x2 <- [0..9], x3 <- [0..9], x4 <- [0..9], x5 <- [0..9], x6 <- [0..9]]

diff :: [Int] -> [Int] -> Int
diff a b = sum $ zipWith f a b
  where f a b | a == b = 0
              | otherwise = 1

isSolution :: [Int] -> Bool
isSolution xs = (sum $ take m xs) == (sum $ drop m xs)
  where m = (length xs) `div` 2

main = do
    xs' <- getLine
    let xs = toInts xs'
    let res = minimum . map (diff xs) $ allGoodTickets
    print res


-- @betaveros :: vim:set fdm=marker:
{-# LANGUAGE LambdaCase, NPlusKPatterns, TupleSections #-}
{-# OPTIONS_GHC -fno-warn-unused-imports -fno-warn-missing-signatures #-}
-- import ALL the things! {{{
-- hiding clauses are to allow Data.Foldable's generalizations
import Prelude hiding (mapM, mapM_, sequence, sequence_, foldl, foldl1, foldr, foldr1, and, or, any, all, sum, product, concat, concatMap, maximum, minimum, elem, notElem)
import Control.Applicative
import Control.Arrow
import Control.Exception
import Control.Monad hiding (mapM, mapM_, forM, forM_, sequence, sequence_, msum)
import Control.Monad.ST

import qualified Data.ByteString.Char8 as BS
import Data.ByteString.Char8 (ByteString)
import Data.Bits
import Data.Char
import Data.Either
import Data.Foldable
import Data.Function
import Data.IORef
import Data.List hiding (foldl, foldl', foldl1, foldl1', foldr, foldr1, concat, concatMap, and, or, any, all, sum, product, maximum, minimum, elem, notElem, find)
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.STRef
import Data.String
import Data.Traversable
import Data.Tuple

import qualified Data.Map as Map
import Data.Map (Map)
import qualified Data.Set as Set
import Data.Set (Set)
import qualified Data.Sequence as Seq
import Data.Sequence (Seq, (<|), (|>), (><))

import Debug.Trace
import Text.Printf
import System.IO
-- }}}
-- silly utilities {{{
-- stolen from lens:
a & f = f a
a <&> f = fmap f a
infixl 1 &, <&>

fi :: (Integral a, Num b) => a -> b
fi = fromIntegral
glength :: (Num b) => [a] -> b
glength = genericLength

readInt     = read :: String -> Int
readInteger = read :: String -> Integer
-- (!?) :: (Ord k) => Map k v -> k -> Maybe v
-- (!?) = flip Map.lookup
histogram :: (Ord a, Num b) => [a] -> Map a b
histogram = Map.fromListWith (+) . map (,1)
-- }}}
-- input and output {{{
bsGetLine :: IO ByteString
bsGetLine = fst . BS.spanEnd isSpace <$> BS.getLine

inputInt     = (read <$> getLine) :: IO Int
inputInteger = (read <$> getLine) :: IO Integer
inputDouble  = (read <$> getLine) :: IO Double

inputRow :: (Read a) => IO [a]
inputRow = map read . words <$> getLine
inputInts     = inputRow :: IO [Int]
inputIntegers = inputRow :: IO [Integer]
inputDoubles  = inputRow :: IO [Double]

ssUnwords :: [ShowS] -> ShowS
ssUnwords [] = id
ssUnwords (x:xs) = x . (' ':) . ssUnwords xs

ssUnlines :: [ShowS] -> ShowS
ssUnlines [] = id
ssUnlines (x:xs) = x . ('\n':) . ssUnlines xs

showMany :: (Show a) => [a] -> String
showMany xs = ssUnwords (map shows xs) ""
showMatrix :: (Show a) => [[a]] -> String
showMatrix xs = ssUnlines (map (ssUnwords . map shows) xs) ""

printMany :: (Show a) => [a] -> IO ()
printMany xs = putStrLn (showMany xs)
printMatrix :: (Show a) => [[a]] -> IO ()
printMatrix xs = putStr (showMatrix xs)
-- }}}

main :: IO ()
main = do
	[_,v] <- inputInts
	s <- liftA2 (-) sum maximum <$> inputInts
	putStrLn $ if s <= v then "YES" else "NO"


module Main where

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

readInt = maybe undefined fst . B.readInt
readInts = map readInt . B.words

solve n x = ([[0,1,2],[1,0,2],[1,2,0],[2,1,0],[2,0,1],[0,2,1]]!!(n `mod` 6))!!x

main = do
  n:x:_ <- replicateM 2 (readInt <$> B.getLine)
  print $ solve n x


main = interact solve

solve input = answer
    where
    n::Int
    n = read input

    answer
        | one_or_more (possible n) = "YES"
        | otherwise = "NO"

possible::Int -> [Int]
possible n = mapped
    where
    as = [0..(n `div` 1234567)]
    mapped = (map (\x -> n - (1234567 * x)) as) >>= possible'

possible'::Int -> [Int]
possible' n = mapped
    where
    as = [0..(n `div` 123456)]
    mapped = (map (\x -> n - (123456 * x)) as) >>= possible''

possible''::Int -> [Int]
possible'' n = tuple
    where
    answer = n `mod` 1234 == 0
    tuple
        | answer = [n `div` 1234]
        | otherwise = []

one_or_more [] = False
one_or_more (x:xs) = True


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

main = putStr . evalState app . B.words =<< B.getContents

app = do
  n <- poi
  show `fmap` liftM3 solve poi poi (replicateM n poi)
  where
  pop = state $ \(x:xs) -> (x, xs)
  poi = fmap int pop
  int = maybe undefined fst . B.readInt

solve k x = (k * x +) . sum . drop k . reverse . sort


{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NoMonomorphismRestriction #-}

import Data.Bits
import Data.List
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IM
import Data.IntSet (IntSet)
import qualified Data.IntSet as IS
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
-- import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
{-
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
-}
import Control.Monad
import Control.Monad.ST.Safe
-- import Control.Monad.ST
import Control.Applicative
import Data.Maybe
import Data.Tuple
import Data.Ord
import Control.Monad.State.Strict
import Data.Int
import Data.Word
import Data.STRef
import Data.IORef
import Data.Function
import System.IO
import System.Exit

main :: IO ()
main = do
  [n,v] <- map readInt . words <$> getLine
  let infirst = (v - 1) `min` (n - 1)
      inrest = (n - 1) - infirst
  print $ infirst + shiftR (inrest * (inrest + 1)) 1

rInt :: StateT BSL.ByteString Maybe Int
rInt = StateT $ BSL.readInt . BSL.dropWhile (<'!')

#define D(f,r,d) f::Integral a=>a->d;f=fromIntegral;r::String->d;r=read
#define C(f,r,g,h,d) D(f,r,d);g,h::RealFrac a=>a->d;g=floor;h=ceiling
C(_toInteger_,readInteger,floorInteger,ceilInteger,Integer)
C(toInt,readInt,floorInt,ceilInt,Int)
C(toI8,readI8,floorI8,ceilI8,Int8)
C(toI16,readI16,floorI16,ceilI16,Int16)
C(toI32,readI32,floorI32,ceilI32,Int32)
C(toI64,readI64,floorI64,ceilI64,Int64)
C(toWord,readWord,floorWord,ceilWord,Word)
C(toW8,readW8,floorW8,ceilW8,Word8)
C(toW16,readW16,floorW16,ceilW16,Word16)
C(toW32,readW32,floorW32,ceilW32,Word32)
C(toW64,readW64,floorW64,ceilW64,Word64)
D(toDouble,readDouble,Double)
D(toFloat,readFloat,Float)
#undef D
#undef C

#define N(f,g,a,m)\
  f :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> e -> m (a i e); f=A.newArray;\
  g :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> m (a i e); g=A.newArray_
#define C(a,m)
N(newIOA,newIOA_,IOArray,IO)
N(newSTA,newSTA_,STArray s,ST s)
#undef C
#define C(a,m) MArray (a) e (m),
N(newIOUA,newIOUA_,IOUArray,IO)
N(newSTUA,newSTUA_,STUArray s,ST s)
#undef C
#undef N


module Main where

main = do
    (xa, ya) <- getVectorLine
    (xb, yb) <- getVectorLine
    (xc, yc) <- getVectorLine

    let try :: Integer -> Integer -> Bool
        try x y = let
            det = xc*xc + yc*yc
            det_a = x*xc + y*yc
            det_b = xc*y -yc*x
            in if det == 0
                then x == 0 && y == 0
                else ( det_a `mod` det ) == 0 && ( det_b `mod` det ) == 0

    putStrLn $ if try (xb-xa) (yb-ya) || try (xb-ya) (yb+xa) || try (xb+xa) (yb+ya) || try (xb+ya) (yb-xa)
        then "YES"
        else "NO"

getVectorLine :: IO (Integer, Integer)
getVectorLine = do
    input <- getLine
    let [x, y] = map read $ words input
    return (x, y)


f :: (Int,Int,Int) -> Maybe Int
f (1, 2,x) = if x ==3 then Just 2 else Just 3
f (1, 3,_) = Just 4
f (2, 1,_) = Just 3
f (2 ,3,_) = Nothing
f (3, 1,_) = Just 4
f (3, 2,_) = Nothing

solve ::[Int] -> Maybe Int
solve l = foldl sum' (Just 0) (map f ((head l,l!!1,12):pairs l))
    where sum' x y = (+) <$> x <*> y

pairs :: [Int] -> [(Int,Int,Int)]
pairs (x:y:z:ys) = (y,z,x):(pairs (y:z:ys))
pairs (x:y:ys) = []
pairs _ = []

c :: Maybe Int -> String
c (Just x) ="Finite\n"++ (show x)
c _ = "Infinite"

main = do
    n <- getLine
    inp <- getLine
    let l =map read $ words inp
    putStrLn $ c $ solve l


import Control.Monad
import Control.Applicative

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

solve :: Integer -> Integer -> Integer
solve a b =
  if b - a > 5 then 0 else solve' b (b - a) 1
  where
    solve' n 0 acc = acc
    solve' n c acc = solve' (n - 1) (c - 1) (acc `seq` ((acc * n) `mod` 10))