Created
May 31, 2021 04:56
-
-
Save jinwoo1225/09fe2cff6db9e2a6af9fa2e5a6c30b05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fast_pow(n: int, k: int, mod: int) -> int: | |
""" | |
faster power that uses Divide & Conquer | |
:param n: base | |
:param k: count | |
:param mod: modular | |
:return: powered n to k | |
""" | |
if n == 0: | |
raise ArithmeticError("n can't be 0") | |
if k == 0: | |
return 1 | |
temp: int = fast_pow(n, k // 2, mod) | |
ret: int = (temp * temp) % mod | |
if k % 2: | |
ret = (ret * n) % mod | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment