这道题的巧妙方法没有想出来,但是算是优化的暴力破解吧。Accepted
import java.io.BufferedInputStream;
import java.util.Scanner; public class Main { public static void main(String[] args) {
Scanner scan = new Scanner(new BufferedInputStream(System.in));
while(scan.hasNextInt()){
int n = scan.nextInt();
int m = scan.nextInt();
if(n==0&&m==0){
break;
}
int right3OfN = n%1000;
int result = right3OfN;
while(--m!=0){
result = result*right3OfN%1000;
}
System.out.println(result);
}
scan.close();
}
}
但是,发现了个问题
如果我把1-999的m次方可能的结果用静态容器保存起来时,结果就 Wrong Answer
没想通,有谁知道么?
以下是JAVA实现:
import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Scanner; public class Main { public static void main(String[] args) {
Scanner scan = new Scanner(new BufferedInputStream(System.in));
while(scan.hasNextInt()){
int n = scan.nextInt();
int m = scan.nextInt();
if(n==0&&m==0){
break;
}
int right3OfN = n%1000;
ArrayList<Integer> right3s = arrays.get(right3OfN);
System.out.println(right3s.get((m-1)%right3s.size()));
}
scan.close();
} static ArrayList<ArrayList<Integer>> arrays = getRightMost3DigitArray(); static ArrayList<ArrayList<Integer>> getRightMost3DigitArray(){
ArrayList<ArrayList<Integer>> arrays = new ArrayList<>();
for(int i = 0 ; i != 1000 ; i ++){
ArrayList<Integer> integers = new ArrayList<>();
int right3OfI = i;
integers.add(right3OfI);
int right3 = right3OfI;
while(true){
right3 = right3 * right3OfI%1000;
if(integers.contains(right3)){
break;
}else{
integers.add(right3);
}
}
arrays.add(integers);
}
return arrays;
}
}