Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Extra math module.
Since: 1.0.0.0
Re-exports from the internal math module
invGcd :: Int -> Int -> (Int, Int) Source #
Returns \((g, x)\) such that \(g = \gcd(a, b), \mathrm{xa} \equiv g \pmod b, 0 \le x \le b/g\).
Constraints
- \(1 \le b\) (not asserted)
Since: 1.0.0.0
Binary exponentiation
Examples
>>>
import AtCoder.Extra.Math qualified as M
>>>
import Data.Semigroup (Product(..), Sum(..))
>>>
getProduct $ M.power (<>) 32 (Product 2)
4294967296
>>>
getProduct $ M.stimes' 32 (Product 2)
4294967296
>>>
getProduct $ M.mtimes' 32 (Product 2)
4294967296
power :: (a -> a -> a) -> Int -> a -> a Source #
Calculates \(x^n\) with custom multiplication operator using the binary exponentiation technique.
The internal implementation is taken from Data.Semigroup.stimes
, but power
uses strict
evaluation and is often much faster.
Complexity
- \(O(\log n)\)
Constraints
- \(n \gt 0\)
Since: 1.0.0.0