返回题解分享
讨论 / 题解分享/ 帖子详情

快速幂 - 题解

def quick_pow_mod_iterative(a, b, p):
    result = 1
    base = a % p
    while b > 0:
        if b % 2 == 1:  # b 是奇数
            result = (result * base) % p
        base = (base * base) % p
        b //= 2
    return result

n = int(input())
for _ in range(n):
    a, k, p = map(int, input().split())
    result = quick_pow_mod_iterative(a, k, p)
    print(result)
0 回复 0 转发 0 喜欢 4 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!