首页 > 代码库 > uva10006-Carmichael数

uva10006-Carmichael数

题目链接 http://acm.hust.edu.cn/vjudge/problem/19411

 

解题思路

快速幂, 筛法求素数。

 

代码

#include<iostream>#include<cstdio>#include<string.h>#include<cmath>typedef long long ll; using namespace std;const int maxS = 65005;bool isPrime[maxS];ll PowMod(int a, int b, int c){    ll result = 1;    ll base = a % c;    while(b) {        if(b & 1) result = (result * base) % c;        base = (base * base) % c;        b = b >> 1;    }    return result;}int main(){    int flag, n, m;    memset(isPrime, 1, sizeof(isPrime));    isPrime[0] = isPrime[1] = false;    m = sqrt(maxS) + 0.5;    for(int i=2; i<m; i++) {        if(isPrime[i]) {            for(int j=i*i; j<maxS; j+=i)                isPrime[j] = false;        }    }    cin >> n;    while(n) {        flag = false;        if(isPrime[n]) { cout <<  n << " is normal." << endl; cin >> n; continue; }        for(int i=2; i<n; i++) if(PowMod(i, n, n) != i) {            flag = true;            break;        }        if(!flag) cout << "The number " << n << " is a Carmichael number." << endl;         else cout <<  n << " is normal." << endl;        cin >> n;    }    return 0;}

 

uva10006-Carmichael数