如果一个数n不是素数,却满足对于所有a(a>=2 && a<=n-1),a^n mod n == a,就说他是Carmichael number,否则是normal的。

快速幂计算,范围会超int。

代码:

#include <stdio.h>
#include <math.h>
int prime(int a) {
    if(a <= 1)return 0;
    if(a == 2 || a == 3)return 1;
    if(a % 6 != 1 && a % 6 != 5)return 0;
    for(int i = 5;i * i <= a;i += 6) {
        if(a % i == 0 || a % (i + 2) == 0)return 0;
    }
    return 1;
}
long long pow_n(long long a,long long b) {
    long long d = 1,c = b;
    while(b) {
        if(b % 2)d = (d * a) % c;
        a = (a * a) % c;
        b /= 2;
    }
    return d;
}
int check(int a) {
    for(int i = 2;i < a;i ++) {
        if(pow_n(i,a) != i)return 0;
    }
    return 1;
}
int main() {
    int a;
    while(~scanf("%d",&a) && a) {
        if(!prime(a) && check(a))printf("The number %d is a Carmichael number.\n",a);
        else printf("%d is normal.\n",a);
    }
}