素数表+快速幂

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
#define N 65000+10
#define INF 0x3f3f3f3f
#define LL long long

using namespace std;

int n;
bool prime[N];
void is_prime()
{
    prime[0]=prime[1]=false;
    for(int i=2; i<N; ++i)
    {
        if(prime[i])
        {
            for(int j=i+i; j<N; j+=i)
                prime[j]=false;
        }
    }
}
int mod(int a, int n, int m)
{
    if(!n) return 1;
    int x=mod(a, n/2, m);
    LL ans=(LL)x*x%m;
    if(n%2==1) ans=ans*a%m;
    return (int)ans;
}
bool solve(int a)
{
    for(int i=2; i<a; ++i)
    {
        if(mod(i,a,a)!=i)
            return false;
    }
    return true;
}
int main()
{
    memset(prime, true, sizeof(prime));
    is_prime();
    while(scanf("%d",&n)==1 && n)
    {
        if(!prime[n] && solve(n))
            printf("The number %d is a Carmichael number.\n",n);
        else printf("%d is normal.\n",n);
    }
    return 0;
}
View Code

 

posted on 2013-10-27 16:48  风流monkey  阅读(156)  评论(0编辑  收藏  举报