4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

mod7占い (paizaランク S 相当)

問題はここから

mod 7 の足し算

整数$a$を$7$で割ったあまりを$\bar{a}$と書くことにすると,

\bar{a} + \bar{b} = \overline{a+b}

であるので,与えられた数字$a_1,...,a_N$を一度全て$7$で割ったあまりに変換する.

modList :: [Int] -> [Int]
modList = map (`mod` 7)

組み合わせ

あまりのリストの中から,$3$つ選ぶ関数を定義する.

combinations :: Int -> [a] -> [[a]]
combinations 0 _  = [[]]
combinations _ [] = []
combinations n (x:xs) = [x:ys | ys <- combinations (n-1) xs] ++ combinations n xs

choose3 :: [a] -> [[a]]
choose3 = combinations 3

まとめ

完成した$3$つ組を足して,また$7$で割ったあまりを求め,それが$0$である組み合わせを数える,

main :: IO ()
main = do
  _ <- getLine
  a_n <- fmap (map read . lines) getContents
  let 
    ans = length $ filter (\x -> 0 == mod (sum x) 7) $ choose3 $ modList a_n
  print ans
  pure ()
4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?