题解 | #快速乘#

快速乘

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

import java.io.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        //和快速幂一样的思路
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = Integer.parseInt(in.readLine());

        for (int i = 0; i < n; i++) {
            String[] lines = in.readLine().split(" ");
            int a = Integer.parseInt(lines[0]);
            int b = Integer.parseInt(lines[1]);
            int p = Integer.parseInt(lines[2]);

            int res = 0;
            while (b > 0) {
                if (b % 2 == 1) {
                    res += a;
                    res %= p;
                    b--;
                }
                b /= 2;
                a += a;
                a %= p;
            }
            out.write(res + "\n");
        }
        in.close();
        out.close();
    }
}

全部评论

相关推荐

白火同学:大二有这水平很牛了,可以适当对关键信息加粗一点,比如关键技术、性能指标之类的。
点赞 评论 收藏
分享
Rena1ssanc...:对的,要是面评没太烂,勤更新简历等捞就行了,腾讯可以无限复活
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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