這是uva上的一道題,用快速冪解,很裸的模板題。
Carmichael Numbers
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:
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.
Miguel Revilla
2000-08-21
/*
13889176 10006 Carmichael Numbers Accepted C++ 0.376
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long
bool prime(int x)
{
for(int i=2;i*i<=x;i++)
if(x%i==0)
return false;
return true;
}
ll mod_pow(ll x,ll n,ll mod)
{
ll res=1;
while(n>0)
{
if(n&1)
res=res*x%mod;
x=x*x%mod;
n>>=1;
}
//cout<<"res "<<res<<endl;
return res;
}
int main()
{
ll n;
A:while(scanf("%lld",&n)&&n)
{
if(prime(n))
{
cout<<n<<" is normal."<<endl;
continue;
}
for(int i=2;i<n;i++)
if(mod_pow(i,n,n)!=i%n)
{
cout<<n<<" is normal."<<endl;
goto A;
}
cout<<"The number "<<n<<" is a Carmichael number."<<endl;
}
return 0;
}