一题如此基础的题目,搞了那么久,晕。。。不过有很多细节错误自己改过来了,给自己鼓鼓掌^_^
求幂的时候要用longlong型的,可是为什么啊?x的值根本就不可能比65000大啊,每次都取模啊,求反例。。。。。。
还有判断素数要在前,刘汝佳先生说过&&是短路来着。。。。。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 5 int prime[65002]; 6 int n; 7 8 void is_prime() 9 { 10 int m = sqrt(65001 + 0.5); 11 memset(prime,0,sizeof(prime)); 12 prime[0] = prime[1] = -1; 13 for(int i = 2;i <= m;i ++)//这里判断素数也挺快滴,也是刘汝佳先生的白皮书上滴。 14 { 15 if(!prime[i]) 16 { 17 prime[i] = 1; 18 for(int j = i*i;j <= 65001;j += i) 19 { 20 prime[j] = -1; 21 } 22 } 23 } 24 } 25 26 long long pow_mod(int a,int k,int m)//这个求幂挺快的logk,刘汝佳先生的那本白皮书上滴 27 { 28 if(k == 1) 29 return (long long)a; 30 31 long long x = pow_mod(a,k/2,m); 32 long long ans = x * x % m; 33 if(k % 2 == 1) 34 { 35 ans = ans * a % m; 36 } 37 38 return ans; 39 } 40 41 bool fermat(int k) 42 { 43 for(int i = 2;i < k;i ++) 44 { 45 if(pow_mod(i,k,k) != i) 46 return false; 47 } 48 49 return true; 50 } 51 int main() 52 { 53 is_prime(); 54 while(scanf("%d",&n) == 1) 55 { 56 if(!n) 57 break; 58 if((prime[n] == -1)&&fermat(n)) 59 { 60 printf("The number %d is a Carmichael number.\n",n); 61 } 62 else 63 { 64 printf("%d is normal.\n",n); 65 } 66 } 67 68 return 0; 69 }