Skip to content

Instantly share code, notes, and snippets.

@SPY
Created March 9, 2015 10:04
Show Gist options
  • Save SPY/d1a851803a6226766573 to your computer and use it in GitHub Desktop.
Save SPY/d1a851803a6226766573 to your computer and use it in GitHub Desktop.
import Control.Applicative ((<$>))
import Text.Printf (printf)
type Solution = Maybe [Double]
solve :: [[Double]] -> Solution
solve [[a, b]] = return [b / a]
solve (h@(x : xs) : t) = do
let minor = transform h t
if any isNull minor
then Nothing
else do
solutions <- solve minor
return $ (sum $ zipWith (*) xs (solutions ++ [-1.0])) / (negate x) : solutions
isNull :: [Double] -> Bool
isNull [x] = True
isNull (x:xs) = if x == 0 then isNull xs else False
transform :: [Double] -> [[Double]] -> [[Double]]
transform row = map $ elliminate row
where
elliminate :: [Double] -> [Double] -> [Double]
elliminate (x : xs) (y : ys) =
let factor = negate (y / x) in
zipWith (+) (map (* factor) xs) ys
readNumList :: String -> [Double]
readNumList = map read . words
main :: IO ()
main = do
[n, m] <- readNumList <$> getLine
inp <- map readNumList <$> (sequence $ replicate (floor n) getLine)
if n < m
then putStrLn "INF"
else case solve inp of
Just solutions -> putStrLn "YES" >> (putStrLn . unwords . map (printf "%.8f") $ solutions)
Nothing -> putStrLn "NO"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment