Skip to content

Instantly share code, notes, and snippets.

@neetsdkasu
Created July 1, 2021 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neetsdkasu/db510701af3b24cc3afa554522efba55 to your computer and use it in GitHub Desktop.
Save neetsdkasu/db510701af3b24cc3afa554522efba55 to your computer and use it in GitHub Desktop.
ModValue (雑)
-- author: Leonardone @ NEETSDKASU
-- modvalue MUST BE PRIME!
modvalue :: Integral a => a
modvalue = 10^9 + 7
(+%),(-%),(*%),(/%) :: Integral a => a -> a -> a
a +% b = (a+b) `mod` modvalue
a -% b = a +% (modvalue - b)
a *% b = (a*b) `mod` modvalue
a /% b = a *% modinv b
modinv v = modpow v (modvalue-2)
modsum xs = foldl1 (+%) xs
modpow _ 0 = 1
modpow v 1 = v
modpow v p | odd p = v *% modpow v (p-1)
| otherwise = modpow (v*%v) (p`div`2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment