uva 10006 - Carmichael Numbers

考同余跟快速幂取模

#include <stdio.h> #include <string.h> #define MAX 65001 bool isPrimer[MAX]; //筛选法求素数 void init() { memset(isPrimer, true, sizeof(isPrimer)); isPrimer[0] = isPrimer[1] = false; for(int i = 2; i * i <= MAX; i++) { if(isPrimer[i]) for(int j = i * i; j <= MAX; j += i) isPrimer[j] = false; } } //快速幂取模 long mod(long a, long n, long m) { if(n == 1) return a % m; long ans = mod(a, n >> 1, m); ans = ans * ans % m; if(n % 2 == 1) return ans * a % m; return ans; } bool check(long n) { for(long a = 2; a < n; a++) if(mod(a, n, n) != a) return false; return true; } int main() { init(); long n; while(~scanf("%ld", &n) && n != 0) { if(!isPrimer[n] && check(n)) printf("The number %ld is a Carmichael number./n",n); else printf("%ld is normal./n",n); } return 0; } 

你可能感兴趣的:(Numbers)