[算法题]Python 快速幂

# 如无特殊要求,可以直接使用pow(x, y, mod) 函数
def fast_power(x, y, z):
    res = 1
    while y:
        if y&1 == 1:  # y&1 是取y的二进制最后一位, 用来判断是否为奇数
            res = res*x%z
        y = y>> 1 # 位运算,也就是b//2
        x = x * x % z
    return res
ans = fast_power(2, 6, 10**9+7)
print(ans)
posted @ 2021-09-06 22:39  pas_a_pas  阅读(158)  评论(0)    收藏  举报