AcWing 875.快速幂 (快速幂模板)

快速幂模板
模板题
AcWing 875.快速幂 (快速幂模板)_第1张图片
AcWing 875.快速幂 (快速幂模板)_第2张图片

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter pw = new PrintWriter(System.out);

    public static void pow(int a, int k, int q) {
        int res = 1;
        while (k != 0) {
            if ((k & 1) == 1) res = (int) ((long) res * a % q);
            k >>= 1;
            a = (int) ((long) a * a % q);
        }
        pw.println(res);
    }

    public static void main(String[] args) throws IOException {
        String[] s = br.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        while (n-- > 0) {
            s = br.readLine().split(" ");
            int a = Integer.parseInt(s[0]);
            int b = Integer.parseInt(s[1]);
            int c = Integer.parseInt(s[2]);
            pow(a, b, c);
        }
        pw.println();
        pw.flush();
        br.close();
    }
}

你可能感兴趣的:(数学算法)