题解 | #快速乘#

快速乘

https://www.nowcoder.com/practice/043c66e95fe548d0b8e56c1830330f93

import sys

q = int(input())
# 1.(a+b)%p=(a%p+b%p)%p 
# 2. (a-b)%p=(a%p-b%p)%p  
# 3. (a*b)%p=(a%p*b%p)%p
# 一般 (a*b)%p = ((a+a)%p + b//2 %p)%p, 注意b是否能整除
for i in range(q):
    a,b,p = list(map(int,input().split(' ')))
    res = 0
    base = 1
    while b:
        if b%(base+base)!=0:
            res = (res+a) % p
            b -= base
        a = (a+a)%p
        base = base + base
    print(res)
        
    

全部评论

相关推荐

头像
05-16 11:16
已编辑
东华理工大学 Java
牛客737698141号:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务