My Weblog

Blog about programming and math

SPOJ 6731. Coeficientes

SPOJ 6731. Coeficientes‘s only catch is empty lines in the input file otherwise problem is quite easy. The limits on n and k is less than 13 so you just calculate the factorial of every number. You can have look at wikipedia or mathworlds. Haskell source code .

import Data.List

factorial :: Integer -> Integer
factorial 0 = 1
factorial n = product [1..n]

solve :: [[Integer]] ->  String 
solve [] = []  
solve (x:y:xs) = a ++  "\n" ++  solve xs where 
     (n:k:_) = x 
     c = product [1..n]
     b = foldl (\acc p -> div acc ( factorial p ) ) c y
     a = show b

format::[[Integer]] -> [[Integer]]
format [] = [] 
format (x:xs) = if null x then format xs else x : format xs 



main = interact $    solve . format .  map (map read . words )  . lines 
Advertisements

June 4, 2011 - Posted by | Programming | ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: