Binary Exponentiation - to find a b → converted b into binary Benefit - instead of multiplying b times => multiply lo g 2 ( b ) times If a is large → take (a % M) If M is itself very large (like 1e18) => instead of a*a → DO a+a+a....(a times) even if a*a = 1e18 → will not get reduced log²(n) bcz log(n) for binary_Multiply & log(n) f or expo Same code as expo => Only change * to + everywhere If b is large in a b → a b c ≠ a ( b c % M ) ( mod M ) → cannot take mod in power Use Euler Theorem → a b c ≡ a ( b c % ɸ(m) ) (mod M ) → ɸ is euler totient function For safety, add extra ɸ in power bcz above fails if gcd(a, m) ≠ 1 a b c ≡ a (ɸ(m) + ( b c % ɸ(m) )) ( mod M ) → updated GCD + prime properties a divides b + b divides a ---> a = ± b gcd is alwa...
Comments
Post a Comment