結果

問題 No.118 門松列(2)
ユーザー ducktail
提出日時 2018-06-18 21:08:00
言語 Haskell
(9.10.1)
結果
AC  
実行時間 145 ms / 5,000 ms
コード長 836 bytes
コンパイル時間 4,883 ms
コンパイル使用メモリ 180,832 KB
実行使用メモリ 17,152 KB
最終ジャッジ日時 2024-06-30 17:04:32
合計ジャッジ時間 6,161 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

import Control.Applicative ((<$>), (<*>))
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as B
import Data.List (unfoldr)
import Data.List (sort, group)
import Data.Char (isSpace)

main :: IO ()
main = solve <$> readLn <*> (readil B.readInt <$> B.getLine) >>= print

solve :: Int -> [Int] -> Int
solve n xs = flip mod (10^9+7) $ comb n 3 - (sum . map (flip comb 3) $ l3) - (sum . map (\m -> (n-m) * comb m 2) $ l2)
  where l2 = filter (>=2) . map length . group . sort $ xs
        l3 = filter (>=3) l2

comb n m | m == 0 = 1
         | n < 2 * m = comb n (n-m)
         | otherwise = comb (n-1) (m-1) * n `div` m

readil :: Integral a =>  (ByteString -> Maybe (a, ByteString)) -> ByteString -> [a]
readil f = unfoldr g
  where
    g s = do
      (n, s') <- f s
      return (n, B.dropWhile isSpace s')
0