An important topic nowadays in computer science is cryptography. Some people even think that cryptography is the only important field in computer science, and that life would not matter at all without cryptography.
Alvaro is one of such persons, and is designing a set of cryptographic procedures for cooking paella. ´ Some of the cryptographic algorithms he is implementing make use of big prime numbers. However, checking if a big number is prime is not so easy. An exhaustive approach can require the division of the number by all the prime numbers smaller or equal than its square root. For big numbers, the amount of time and storage needed for such operations would certainly ruin the paella.
However, some probabilistic tests exist that offer high confidence at low cost. One of them is the Fermat test.
Let a be a random number between 2 and n−1 (being n the number whose primality we are testing). Then, n is probably prime if the following equation holds:
a n mod n = a
If a number passes the Fermat test several times then it is prime with a high probability.
Unfortunately, there are bad news. Some numbers that are not prime still pass the Fermat test with every number smaller than themselves. These numbers are called Carmichael numbers.
In this problem you are asked to write a program to test if a given number is a Carmichael number. Hopefully, the teams that fulfill the task will one day be able to taste a delicious portion of encrypted paella. As a side note, we need to mention that, according to Alvaro, the main advantage of encrypted ´ paella over conventional paella is that nobody but you knows what you are eating.
Input
The input will consist of a series of lines, each containing a small positive number n (2 < n < 65000). A number n = 0 will mark the end of the input, and must not be processed.
Output
For each number in the input, you have to print if 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.
思路
这道题需要用到素数表这个东西。这道题求的是可以满足这个公式的非素数,所以,我们要先把素数表求出来作为一个标准,如果输入的数是素数的话,就输出"xxx is normal",如果不是再判断输入的数是否符合那条公式,如果符合就输出"The number xxx is a Carmichael number. "这里要注意的一点是,如果需要判断多组数据是否符合条件的时候,素数表只需要在程序开始的时候计算一次就行了,后面就不需要重复计算了,这样可以节省程序的运行时间。
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <cstring>
using namespace std;
int n;
int f[65100];
typedef long long ll;
void prise()//素数打表,质数都为1,素数都为0
{
memset(f,0,sizeof(f));
for(int i=2;i<=65100;i++)
{
if(f[i]==0)
{
for(int j=2*i;j<=65100;j=i+j)
{
f[j]=1;
}
}
}
return ;
}
ll qq(ll x,ll n,ll mod)//快速幂算法
{
ll ret=1;
while(n>0)
{
if(n%2==1)
{
ret=ret*x%mod;
}
x=x*x%mod;
//n>>=1;
n=n/2;
}
return ret;
}
int panduan()//判断n是否满足a^n%n==a
{
for(int i=2;i<n;i++)
{
if(qq(i,n,n)!=i)
{
return 0;
}
}
return 1;
}
int main()
{
prise();
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
if(f[n]==1&&panduan()==1)
{
printf("The number %d is a Carmichael number.\n",n);
}
else
{
printf("%d is normal.\n",n);
}
}
return 0;
}