Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. import qualified Data.Vector.Unboxed as U
  2. import Data.Vector.Unboxed ( (!) )
  3. import qualified Data.Vector.Unboxed.Mutable as UM
  4. import Control.Monad.ST
  5. import Control.Monad
  6.  
  7. base = 10 ^ 9 + 7 :: Int
  8. maxL = 3 * 10 ^ 5 :: Int
  9.  
  10. n *% m = (n * m) `mod` base
  11. facs = U.scanl' (*%) 1 $ U.generate maxL (+ 1)
  12.  
  13. invs = U.create f
  14. where
  15. f :: ST s (UM.MVector s Int)
  16. f = do
  17. v <- UM.new maxL
  18. UM.write v 0 1
  19. UM.write v 1 1
  20. forM_ [2 .. UM.length v - 1] $ \i -> do
  21. let (q, r) = divMod base i
  22. vr <- UM.read v r
  23. UM.write v i (base - vr * q `mod` base)
  24. return v
  25.  
  26. facinvs = U.tail $ U.scanl' (*%) 1 invs
  27.  
  28. combmod n k
  29. | n < k
  30. = 0
  31. | n < 0 || k < 0
  32. = 0
  33. | otherwise
  34. = facs ! n * (facinvs ! k * facinvs ! (n - k) `mod` base) `mod` base
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement