/*The analysis of this problem
What is a Carmichael number?
First it is not a prime;
Second it must meet the contition that a^p mod p=a.
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>//get a random number
#include<cstring>
using namespace std;
#define MAXN 70000
int visit[MAXN];
void get_prime()
{
memset(visit,0,sizeof(visit));
for(int i=2;i<=MAXN;i++)
{
if(!visit[i])
for(int j=i*2;j<=MAXN;j+=i)
visit[j]=1;
}
}
int pow_mod(int a,int n,int m)
{
if(n==0) return 1;
if(n==1) return a%m;
long long x=pow_mod(a,n/2,m);
long long ans=x*x%m;
if((n&1)) ans=ans*a%m;
return (int)ans;
}
bool solve(int n)
{
for(int i=2;i<n;i++)
{
if(pow_mod(i,n,n)!=i)
return false;
}
return true;
}
int main()
{
int n;
get_prime();
while(scanf("%d",&n)&&n)
{
if(visit[n]==1&&solve(n))
printf("The number %d is a Carmichael number.\n",n);
else printf("%d is normal.\n",n);
}
return 0;
}
10006 - Carmichael Numbers//快速幂取模
最新推荐文章于 2019-03-18 12:40:55 发布