[UVA - 10006] Carmichael Numbers (power + fast prime number sieve method)

-->Carmichael Numbers

 Descriptions:

The title is very long, basically useless, roughly meaning of the questions below

Given a number n, n is a composite number and for any 1 <a <n has a modulus n is equal to n-th power of a, this number is Carmichael Number.

输出The number n is a Carmichael number.

n is a prime number

Export

n is normal.

Input

Plural sets of inputs, a first row to n (2 <n <65000). n = 0 represents the input end of the processing is not required

Output

For each input, it is not output Carmichael number, reference sample.

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.
Topic Link
 
It may first determine whether the number n together, it is then determined.
Since the value of the power of n i may be large, and therefore can be used quickly views title modulo exponentiation, since the value range may be large, so long long type may be employed.
 
AC Code
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 65000+10
using namespace std;
ll n;
ll mod;
int isprime[Maxn];//素数表
void eratos(int x)//求素数表,true为素数 
{
    for(int i=0; i<=x; ++i)
        isprime[i]=true;
    isprime[0]=isprime[1]=false;
    for(intI = 2 ; I <= X; ++ I) 
    { 
        IF (isPrime [I]) 
        { 
            int J = I + I;
             the while (J <= X) 
            { 
                isPrime [J] = to false ; 
                J + = I; 
            } 
        } 
    } 
} 
LL qpow (a LL, LL n) // calculate a ^ n% mod fast power 
{ 
    LL Re = 1 ;
     the while (n) 
    { 
        iF (n & 1 ) // determines whether the last bit of n is 1
            = Re (Re * a)% MOD; 
        n >> = . 1 ; // discarded last a n 
        a = (a * a)% MOD; // to a squared 
    }
     return Re% MOD; 
} 
int main ( ) 
{ 
    eratos (MAXN - . 5 );
     the while (CIN >> n-, n-) 
    { 
        IF (isPrime [n-]) // primes 
            COUT n-<< << " . iS Normal " << endl;
         the else // not a prime number 
        {
             int F =1;
            for(int i=2; i<n; i++)//判断
            {
                mod=n;
                ll t=qpow(i,n);
                if(t!=i)
                {
                    f=0;
                    cout << n << " is normal." << endl;
                    break;
                }
            }
            if(f)
                cout << "The number " << n <<
                     " is a Carmichael number." << endl;
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/sky-stars/p/11210546.html