結果

問題 No.117 組み合わせの数
ユーザー HaarHaar
提出日時 2018-09-27 00:22:21
言語 Haskell
(9.6.2)
結果
MLE  
実行時間 -
コード長 1,404 bytes
コンパイル時間 8,617 ms
コンパイル使用メモリ 184,736 KB
実行使用メモリ 636,172 KB
最終ジャッジ日時 2023-08-02 09:33:05
合計ジャッジ時間 15,597 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

import Control.Monad
import qualified Data.Vector as V
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_ = V.fromList $ factorials numElem divisor
invfactorials_ = V.fromList $
  reverse $ scanl (\a b -> (a*(b+1))`mod`divisor) (modinv (factorials_ V.! (fromIntegral numElem)) divisor) (reverse [0..numElem-1])

combinationModP_2 n k
  | n < k || n < 0 || k < 0 = 0
  | otherwise = (factorials_ V.! n * invfactorials_ V.! k * invfactorials_ V.! (n-k)) `mod` divisor
permutationMod_2 n k
  | n < k || n < 0 || k < 0 = 0
  | otherwise = (factorials_ V.! n * invfactorials_ V.! (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