目录
题目描述
求A^B的最后三位数表示的整数(1<=A,B<=1000)
输入
第一行输入一个整数n,表示有n个测试实例,接着有n行,每行一个实例,给出两个正整数A,B
输出
针对每个测试实例,输出A^B的最后三位(没有前导0) ,单独占一行。
样例输入 Copy
2 2 3 12 6
样例输出 Copy
8 984
code
import java.util.*;
public class Main {
public static int q(int a, int b, int mod) {
int s = 1;
while (b > 0) {
if ((b&1) == 1) s = (int) ((long)s * a % mod);
b >>= 1;
a = (int) ((long)a * a % mod);
}
return s;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n -- > 0) {
int a = sc.nextInt(), b = sc.nextInt();
System.out.printf("%d\n", q(a, b, 1000));
}
}
}