-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrac.hs
32 lines (24 loc) · 897 Bytes
/
frac.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import Data.List (group, intersperse)
import Data.List.Split (chunksOf)
import Data.Digits(unDigits)
doIt :: (Integer, [Char]) -> [Char]
doIt (i, s) = "Case #" ++ (show i) ++ ": " ++ (frac1 . map read . words $ s)
frac1 :: [Int] -> String
frac1 [k,c,s]
| possible k c s = concat . intersperse " " . map (show . succ) $ (frac k c s)
| otherwise = "IMPOSSIBLE"
-- frac :: Int -> Int -> Int -> [Int]
-- frac k c s = map (unDigits k) (chunksOf c [0..k-1])
frac :: Int -> Int -> Int -> [Int]
frac k c s = map (index k) (chunksOf c [0..k-1])
index :: Int -> [Int] -> Int
index k ns = foldl (\acc x -> acc * k + x) 0 ns
possible :: Int -> Int -> Int -> Bool
possible k c s = needed k c <= s
needed :: Int -> Int -> Int
needed k c = (k+c-1) `div` c
main :: IO ()
main = do
tests <- getLine
contents <- getContents
mapM_ (putStrLn . doIt) $ zip [1..] (take (read tests) (lines contents))