求A^B mod C
第一次用python写
def fastExpMod(b, e, m):
result = 1
while e != 0:
if (e&1) == 1:
# ei = 1, then mul
result = (result * b) % m
e >>= 1
# b, b^2, b^4, b^8, ... , b^(2^n)
b = (b*b) % m
return result
t=int(input())
while t:
t-=1
a, b, c = map(int, input().split())
print(fastExpMod(a,b,c))
本文介绍了一种使用Python实现的快速幂取模算法,该算法通过位运算提高大数指数次幂模运算的效率,适用于求解A^BmodC类型的问题。文章提供了完整的代码示例,并展示了如何输入参数和输出结果。
732

被折叠的 条评论
为什么被折叠?



