## SRM 385 – Division II, Level Three

Gettc is excellent software for practicing topcoder problems offline and specially in Haskell. I just wrote solution for SRM 385 Problem [ requires registration but its worth and excellent place for programmers ]. You can see the editorial for SRM 385. Haskell source code.

module SummingArithmeticProgressions where canRep :: Int -> Int -> Int -> Bool canRep a b n = canRephelp 1 a b n where canRephelp x a b n | x > b = False | n <= a * x = False | mod ( n - a * x ) b == 0 = True | otherwise = canRephelp ( succ x ) a b n howMany :: Int -> Int -> Int -> Int howMany left right k = ans where a = k b = div ( k * ( k - 1 ) ) 2 d = gcd a b a' = div a d b' = div b d left' = div ( left + d - 1 ) d right' = div right d ans = helphowMany a' b' left' right' 0 helphowMany :: Int -> Int -> Int -> Int -> Int -> Int helphowMany a b left right cnt | and [ left <= right , left < 2 * a * b ] = if canRep a b left then helphowMany a b ( succ left ) right ( succ cnt ) else helphowMany a b ( succ left ) right cnt | otherwise = cnt + right - left + 1

Advertisements

I have participated in my first topcoder contest yesterday, and I aldready like it. Thank’s for the head’s up. gettc does sound very useful.

Comment by BSRK Aditya (@bsrkaditya) | September 11, 2011 |