题解 | #快速幂#

快速幂

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;
    }
}

全部评论

相关推荐

想找运维方向的工作,好难
TestUserpl...:把中山学院去掉,反正师资也是共享的,外面也承认
投了多少份简历才上岸
点赞 评论 收藏
分享
DBsan:我也遇到过好的HR,全程友好交流。这年头基本的礼貌和尊重为什么好多HR都做不到
找工作时遇到的神仙HR
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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