Codeforces

Codeforces 37A - A. Towers

37A - A. Towers import Data.List calc :: [Int] -> (Int, Int) calc xs = (maximum [count x xs | x <- xs], length $ nub xs) where count a xs = sum [1 | x <- xs, x == a] main = do s <- getLine t <- getLine let (x, y) = calc $ map read $ words …

Codeforces 122B - B. Lucky Substring

122B - B. Lucky Substring calc :: String -> Int calc s | n4+n7 == 0 = -1 | otherwise = if n4 >= n7 then 4 else 7 where n4 = sum [1 | c <- s, c == '4'] n7 = sum [1 | c <- s, c == '7'] main = do s <- getLine print $ calc s

Codeforces 122A - A. Lucky Division

122A - A. Lucky Division isLucky :: Int -> Bool isLucky n = show n == filter (\c -> c == '4' || c == '7') (show n) calc :: Int -> String calc n = if null [i | i <- [1..n], isLucky i, n `mod` i == 0] then "NO" else "YES" main = do s <- getL…

Codeforces 146B - B. Lucky Mask

146B - B. Lucky Mask整数 a, b が与えられたとき、c のマスクが b と等しく、かつ c > a であるような c を求めよという問題。「c のマスクが b と等しい」とは、整数 c から 4 と 7 以外の数字を取り除いた数が b と等しいという意味。制約: 1 全探索。 ma…

Codeforces 221A - A. Little Elephant and Function

221A - A. Little Elephant and Function calc :: Int -> [Int] calc 1 = [1] calc 2 = [2, 1] calc n = [n, 1] ++ [2..n-1] main = do s <- getLine putStrLn $ unwords $ map show $ calc $ read s

Codeforces 26A - A. Almost Prime

26A - A. Almost Prime素因数分解したときに、素因数の種類が 2 つかどうか求める問題。 import Data.List factorize :: Int -> [Int] factorize 1 = [1] factorize n = fac 2 n where fac p n | n == 1 = [] | n `mod` p == 0 = p : fac p (n `div` p) | ot…

Codeforces 148A - A. Insomnia cure

148A - A. Insomnia cure calc :: Int -> Int -> Int -> Int -> Int -> Int calc k l m n d = d - length [i | i <- [1..d], i `mod` k /= 0, i `mod` l /= 0, i `mod` m /= 0, i `mod` n /= 0] main = do s <- getContents let [k, l, m, n, d] = map read …

Codeforces 3A - A. Shortest path of the king

3A - A. Shortest path of the king import Data.Char parse :: String -> (Int, Int) parse s = (digitToInt (s!!1), ord (s!!0) - ord 'a' + 1) calc :: (Int, Int) -> (Int, Int) -> [String] calc (sr, sc) (dr, dc) | sr == dr && sc == dc = [] | othe…

Codeforces 139A - A. Petr and Book

139A - A. Petr and Book calc :: Int -> [Int] -> Int calc n pages = calc' n (cycle pages) (cycle [1..7]) where calc' n (p:pages) (d:dayWeek) | n <= p = d | otherwise = calc' (n-p) pages dayWeek main = do s <- getLine t <- getLine print $ ca…

Codeforces 146A - A. Lucky Ticket

146A - A. Lucky Ticket import Data.Char isLucky :: String -> Bool isLucky s = all (\c -> c == '4' || c == '7') s calc :: String -> String calc s = if isLucky s && sum hd == sum tl then "YES" else "NO" where xs = map digitToInt s n = length…

Codeforces 219A - A. k-String

219A - A. k-String 文字列をソートする。 同じ文字でグルーピング。 グルーピングした文字列の長さが k の倍数なら、k-string は作れる。そうでなければ k-string は作れない import Data.List calc :: Int -> String -> Maybe String calc k s = if all (\…

Codeforces 50A - A. Domino piling

50A - A. Domino pilingM x N サイズのボードに 2 x 1 サイズのドミノを出来るだけ沢山敷き詰める問題。ボードに配置可能なドミノの個数を出力する。制約: 1 ボードのサイズが偶数であれば、隙間なくドミノを敷き詰められます。なので、まず偶数サイズのボー…

Codeforces 189A - A. Cut Ribbon

189A - A. Cut Ribbon長さ n のリボンある。そのリボンを出来るだけたくさんカットしたい。カットされたリボンの長さは、a, b, cのいずれかでなければならない。最大でいくつのリボンにカットできるか、という問題。制約: 1 DPの問題なのでC++で解きました。…

Codeforces 4A - A. Watermelon

4A - A. Watermelon calc :: Int -> String calc n | n > 2 && even n = "YES" | otherwise = "NO" main = do s <- getLine putStrLn $ calc $ read s

Codeforces 208A - A. Dubstep

208A - A. Dubstep WUBを空白に置き換える 連続する空白をひとつの空白にまとめる 両端の空白を取り除く -- "WUB"を取り除く(代わりに空白をおく) removeWUB :: String -> String removeWUB "" = "" removeWUB ('W':'U':'B':xs) = ' ' : removeWUB xs remove…

Codeforces 1A - A. Theatre Square

1A - A. Theatre Square calc n m a = x * y where x = (n `div` a) + (signum $ n `mod` a) y = (m `div` a) + (signum $ m `mod` a) main = do s <- getLine let [n, m, a] = map read $ words s :: [Integer] print $ calc n m a signumを括弧で囲まない…

Codeforces 216A - A. Tiling with Hexagons

216A - Tiling with Hexagonsb*cを一つの固まりとみなす。aの値が増減するとき、b*cの固まりを取り除いた図形のパターンがどのように変換するかを考えた。b*cの固まりを取り除くと、以下の「へ」の字のラインが見えてくる。 calc a b c = (a-1) * (b + c - 1…

Codeforces 194B - B. Square

194B - B. Square 左下隅からスタートして、はじめて4隅に止まるのは何ステップ目だろうか。 小さいサイズで試してみる。 正方形のサイズを n とすると、 n = 1 のときは、1 ステップ目で四隅にはじめて到達する n = 2 のときは、2 ステップ目 n = 3 のとき…

Codeforces 205A - Little Elephant and Rozdil

205A - Little Elephant and Rozdil以下のように書いたら、calcに渡すリストのサイズが 1 のときにRUNTIME_ERRORとなりました。 import Data.List calc :: [Int] -> String calc xs | length xs > 1 && a == b = "Still Rozdil" | otherwise = case findInde…

Codeforces 214A - A. System of Equations

214A - A. System of Equations全探索。 calc n m = sum [1 | a<-[0..1000], b<-[0..1000], a^2+b==n, a+b^2==m] main = do s <- getLine let xs = map (\x -> read x :: Int) $ words s let (n, m) = (xs!!0, xs!!1) putStrLn $ show $ calc n m

Codeforces 200B - Drinks

200B - Drinks calc xs = (sum xs / fromIntegral (100 * length xs)) * 100 main = do s <- getLine t <- getLine putStrLn $ show $ calc $ map (\x -> read x :: Double) $ words t 型に関する理解があいまいなので、型エラーがなかなか取れませんでした…

Codeforces 199A - A. Hexadecimal's theorem

199A - A. Hexadecimal's theoremフィボナッチ数列を求める関数を作成しましたが、必要ありませんでした。 main = do s <- getLine putStrLn ("0 0 " ++ s)

Codeforcesの問題をHaskellで解いてみる

Haskellで問題を解いてみます。CodeforcesのRatingは1300くらいなので、div2の易しい問題を中心に解いていきたいと思います。