# Carmichael Numbers //快速幂 筛选素数

## Carmichael Numbers

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 17  Solved: 8
[Submit][Status][Discuss]

## Description

Certain cryptographic algorithms make use of big prime numbers. However, checking whether a big number is prime is not so easy. Randomized primality tests exist that offer a high degree of confidence of accurate determination at low cost, such as the Fermat test. Let a be a random number between 2 and n - 1, where n is the number whose primality we are testing. Then, n is probably prime if the following equation holds: an mod n = a If a number passes the Fermat test several times, then it is prime with a high probability. Unfortunately, there is bad news. Certain composite numbers (non-primes) still pass the Fermat test with every number smaller than themselves. These numbers are called Carmichael numbers. Write a program to test whether a given integer is a Carmichael number.

## Input

The input will consist of a series of lines, each containing a small positive number n ( 2 < n < 65, 000). A number n = 0 will mark the end of the input, and must not be processed.

## Output

For each number in the input, print whether it is a Carmichael number or not as shown in the sample output

## Sample Input

1729
17
561
1109
431
0



## Sample Output

The number 1729 is a Carmichael number.
17 is normal.
The number 561 is a Carmichael number.
1109 is normal.
431 is normal.



## Source

UVA toolkit

[Submit][Status][Discuss]

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 65000+10
typedef long long ll;
ll prime[maxn+1];
ll isprime[maxn+1];
ll primelen=0;
void sushu(int n)
{
for(ll i=1;i<=maxn;i++) //筛选素数
isprime[i]=1;
isprime[0]=isprime[1]=0;
for(ll i=2;i<=maxn;i++)
{
if(isprime[i])
{
prime[primelen++]=i;
for(ll j=2*i;j<=maxn;j=j+i)
isprime[j]=0;
}
}
/*memset(prime,0,sizeof(prime)); //筛选素数
for(ll i=2;i*i<maxn;i++)
{
if(!prime[i])
{
for(ll j=i;j*j<maxn;j++)
{
prime[i*j]=1;
}
}
}*/
}
ll pow(ll x,ll n,ll mod)//快速幂模板
{
ll res=1;
while(n>0)
{
if(n%2==1)
{
res=res*x;
res=res%mod;
}
x=x*x;
x=x%mod;
n>>=1;
}
return res;
}
int main()
{
int n,i,j;
sushu(maxn);
/*for(ll i=1;i<20;i++)
{
printf("%d ",prime[i]);
}
cout<<endl;
for(ll i=0;i<20;i++)
{
printf("%d ",isprime[i]);
}
cout<<endl;
cout<<primelen<<endl;
*/
while(scanf("%d",&n),n)
{
bool ok=true;
if(isprime[n])
{
ok=false;
}
if(ok)
{
for(ll i=2;i<n;i++)
{
ll temp=pow(i,n,n);  //验证i的n次方取余n是否等于i
if(temp!=i)//如果不等于i，就改变ok
{
ok=false;
break;
}
}
}
if(ok)
{
printf("The number %d is a Carmichael number.\n",n);
}
else
{
printf("%d is normal.\n",n);
}
}
return 0;
}