import Control.Applicative main = (enumFromTo 1 <$> readLn) >>= mapM_ do1 where do1 caseno = do [n, m, a] <- map read . words <$> getLine putStr $ "Case #" ++ show caseno ++ ": " {- This is a beautiful yet inefficient application of list comprehension done by Reid (ranked 117). I suppose he didn't have time so he used this quick and dirty way to grab some points. case [ [0, 0, x1, y1, x2, y2] | x1 <- [0..n], y1 <- [0..m], x2 <- [0..n], y2 <- [0..m], x1 * y2 - x2 * y1 == a ] of v:_ -> putStrLn $ unwords . map show $ v _ -> putStrLn "IMPOSSIBLE" The area formula: NO. (18) at http://mathworld.wolfram.com/TriangleArea.html The key insight is we can always pick (0,0) as one vertex, and (x1,y1) and (x2,y2) in counter-clockwise order, so that the area becomes 1/2 * (x1*y2 - x2*y1), which needs to be A/2. Idea inspired by stoitchkov1981 (ranked 280). -} case compare a (n*m) of LT -> putStrLn $ unwords . map show $ [0, 0, x1, y1, x2, y2] where x1 = a `div` m + 1 y1 = x1*y2 - a y2 = m x2 = 1 EQ -> putStrLn $ unwords . map show $ [0, 0, n, 0, 0, m] GT -> putStrLn "IMPOSSIBLE"