Isa@Diary

ソフトウェア開発やってます。プログラミングとか、US生活とかについて書きます。

CF 215A

すごいH本一応読み終わったけど終盤は結構理解が適当なので
2周目を読み始めている。

A. Bicycle Chain

http://www.codeforces.com/problemset/problem/215/A

組み合わせ全列挙して、比が整数になる最大の値を求めて、そうなる組み合わせをカウントする。
fold使って書き直したい。

コード

combination::[Int]->[Int]->[(Int,Int)]
combination [] _ = []
combination (x:xs) ys = [(x,yy) | yy <- ys] ++ combination xs ys

getMax::[(Int,Int)] -> Int
getMax [] = 0
getMax (x:xs)
       | (((snd x) `mod` (fst x)) == 0) = max ((snd x) `div` (fst x)) (getMax xs)
       | otherwise = max 0 (getMax xs)

count::[(Int,Int)] -> Int -> Int
count xs maxi = sum [1|x <- xs, ((snd x) `mod` (fst x)) == 0, ((snd x) `div` (fst x)) == maxi]

main = do
     _ <- getLine
     arg1 <- getLine
     let xs = map (read::String->Int) $ words arg1
     _ <- getLine
     arg2 <- getLine
     let ys = map (read::String->Int) $ words arg2
     let comb = combination xs ys
     let maxi = getMax comb
     putStrLn(show $ count comb maxi)