题意:判断是否对于所有情况都成立,用到了打素数表和幂取模,LRJ上都有详细的代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 65005;
bool vis[MAXN];
void isPrime(int n){
memset(vis,false,sizeof(vis));
int m = sqrt(n+0.5);
for (int i = 2; i <= m; i++)
if (!vis[i]){
for (int j = i * i; j <= n; j += i)
vis[j] = true;
}
}
int pow_mod(int a,int n,int num){
if (n == 1)
return a;
int x = pow_mod(a,n/2,num);
long long ans = (long long) x * x % num;
if (n % 2 == 1)
ans = (ans * a) % num;
return (int)ans;
}
bool judge(int num){
int ans;
for (int i = 2; i < num; i++){
ans = pow_mod(i,num,num);
if (ans != i)
return false;
}
return true;
}
int main(){
int num;
isPrime(MAXN);
while (scanf("%d",&num) != EOF && num){
if (vis[num] && judge(num))
printf("The number %d is a Carmichael number.\n", num);
else
printf("%d is normal.\n", num);
}
return 0;
}