結果

問題 No.117 組み合わせの数
ユーザー HaarHaar
提出日時 2018-09-27 00:04:56
言語 Haskell
(9.8.2)
結果
MLE  
実行時間 -
コード長 1,371 bytes
コンパイル時間 10,648 ms
コンパイル使用メモリ 188,928 KB
実行使用メモリ 622,720 KB
最終ジャッジ日時 2024-10-12 04:23:25
合計ジャッジ時間 12,271 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )

Main.hs:41:10: warning: [GHC-63394] [-Wx-partial]
    In the use of ‘tail’
    (imported from Data.List, but defined in GHC.List):
    "This is a partial function, it throws an error on empty lists. Replace it with drop 1, or use pattern matching or Data.List.uncons instead. Consider refactoring to use Data.List.NonEmpty."
   |
41 |     s' = tail . init $ s
   |          ^^^^

Main.hs:43:40: warning: [GHC-63394] [-Wx-partial]
    In the use of ‘tail’
    (imported from Data.List, but defined in GHC.List):
    "This is a partial function, it throws an error on empty lists. Replace it with drop 1, or use pattern matching or Data.List.uncons instead. Consider refactoring to use Data.List.NonEmpty."
   |
43 |     (n,k) = (\(a,b) -> (read a, read $ tail b)) $ splitAt i s'
   |                                        ^^^^
[2 of 2] Linking a.out

ソースコード

diff #

import Control.Monad
import Data.Array
import Data.List
import Data.Int

{-- n^p mod m --}
powerMod :: Integral a => a -> a -> a -> a
powerMod n 0 m = 1
powerMod n 1 m = n `mod` m
powerMod n p m = (k * k * (if mod p 2 == 0 then 1 else n)) `mod` m
  where
    k = powerMod n (div p 2) m

modinv n p = powerMod n (p-2) p

factorials n m = scanl (\a b -> (a*b) `mod` m) 1 [1..n]

divisor = 10^9+7
numElem = 10^6*2

factorials_ = listArray (0,numElem) $ factorials numElem divisor
invfactorials_ = listArray (0,numElem) $
  reverse $ scanl (\a b -> (a*(b+1))`mod`divisor) (modinv (factorials_!numElem) divisor) (reverse [0..numElem-1])

combinationModP_2 n k
  | n < k || n < 0 || k < 0 = 0
  | otherwise = (factorials_!n * invfactorials_!k * invfactorials_!(n-k)) `mod` divisor
permutationMod_2 n k
  | n < k || n < 0 || k < 0 = 0
  | otherwise = (factorials_!n * invfactorials_!(n-k)) `mod` divisor
hcombinationModP_2 n k
  | n < 0 || k < 0 = 0
  | otherwise = combinationModP_2 (n+k-1) k


parse (c:s) = case c of
                 'C' -> combinationModP_2 n k
                 'P' -> permutationMod_2 n k
                 'H' -> hcombinationModP_2 n k
  where
    s' = tail . init $ s
    Just i = elemIndex ',' s'
    (n,k) = (\(a,b) -> (read a, read $ tail b)) $ splitAt i s'

main = do
  n <- readLn :: IO Int

  replicateM_ n $ do
    s <- getLine
    print $ parse s
0