水题,快速幂的方法很重要,附上AC代码。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Main {
private static long mod_pow(long x, long n, long mod){
//快速幂
//求x的n次方模mod
if(n == 0) return 1;
long res = mod_pow(x*x%mod, n/2, mod);
if((n&1) > 0) res = res*x%mod;
return res;
}
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while(in.nextToken() != StreamTokenizer.TT_EOF){
int n = (int)in.nval;
if(n == 0) break;
boolean flag = false;
for(int i = 2; i<=Math.sqrt(n); i++){
if(n % i == 0){
flag=true;
break;
}
}
if(n == 2) flag=false;
if(flag){
for(int i = 2; i<n; i++){
if(mod_pow(i, n, n)%n != i){
flag = false;
break;
}
}
}
if(flag){
out.println("The number "+n+" is a Carmichael number.");
}
else out.println(n+" is normal.");
out.flush();
}
}
}