We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
def mod_pow(base, exponent, modulus): result = 1 # 将指数展开为二进制形式,从高位到低位逐位处理 while exponent > 0: # 如果当前位是1,则乘上当前的 base,然后取模 if exponent % 2 == 1: result = (result * base) % modulus # 将 base 平方,然后取模 base = (base * base) % modulus # 右移一位,相当于除以2 exponent //= 2 return result # 示例:计算 (7^5) % 13 a = 7 c = 5 n = 13 result = mod_pow(a, c, n) print(f"{a}^{c} mod {n} = {result}") # 7^5 mod 13 = 11
上面的算法是从低位到高位吧?
The text was updated successfully, but these errors were encountered:
No branches or pull requests
上面的算法是从低位到高位吧?
The text was updated successfully, but these errors were encountered: