题解 | #快速幂#

快速幂

https://www.nowcoder.com/practice/defdedf4fe984c6c91eefa6b00d5f4f0

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        long a;
        long b;
        long p;
        for(int i = 0;i<n;i++){
            a = in.nextInt();
            b = in.nextInt();
            p = in.nextInt();
            System.out.println(quickn(a,b,p));
        }
    }
    public static long quickn(long a,long b,long p){
        long res = 1;
        while(b > 0){
            if(b%2==1){
                res = res * a % p;
            }
            b /= 2;
            a = a * a % p;
        }
        return res;
    }
}

全部评论

相关推荐

2025-12-23 11:11
快手_MLOps(实习员工)
jay118_:实在担心的话,就把你账号里面的认证人脸改成你室友的脸,让他每天帮你刷。要是人脸写死了就每天打视频电话刷脸。办法总比困难多。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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