Sherlock and The Beast

  • + 0 comments

    Haskell

    module Main where
    
    import Control.Monad (replicateM_)
    
    solve :: String -> String
    solve nstr = do
        let n = read nstr :: Int
        let res = n `divMod` 3
        case go res of
            Nothing -> "-1"
            Just (q, r) -> replicate (q * 3) '5' ++ replicate r '3'
      where
        go :: (Int, Int) -> Maybe (Int, Int)
        go (q, r)
            | q < 0 = Nothing
            | r `mod` 5 == 0 = Just (q, r)
            | otherwise = go (q - 1, r + 3)
    
    main :: IO ()
    main = do
        cases <- readLn :: IO Int
        replicateM_ cases $ do
            n <- getLine
            putStrLn $ solve n