题解 | #快速乘#

快速乘

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

全部评论

相关推荐

12-13 12:11
复旦大学 Java
点赞 评论 收藏
分享
10-28 10:48
已编辑
门头沟学院 Java
孩子我想要offer:发笔试后还没笔试把我挂了,然后邮箱一直让我测评没测,后面不知道干嘛又给我捞起来下轮笔试,做完测评笔试又挂了😅
点赞 评论 收藏
分享
12-15 12:50
河北工程大学
sta666:我也是这个国际商业化的,三天,一天一面,就通过了,不过我是后端实习生,好好面感觉能过。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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