import List import Ratio inverse :: [[Rational]] -> [[Rational]] inverse mat = sweep ([], zipWith (++) mat unit) where unit = map (take (length mat)) $ iterate (0:) (1:[0,0..]) sweep (xss,[]) = xss sweep (xss,yss) = sweep (xss' ++ [ws], filter (any (/= 0)) yss') where Just (x:xs) = find ((/= 0).head) yss ws = map (/ x) xs [xss',yss'] = map (map f) [xss,yss] f (y:ys) = zipWith (\d e -> e - d*y) ws ys main = interact $ unlines.map(unwords.map(show.round)).inverse.map(map(fromIntegral.read).words).lines