UVA 10006 Carmichael Numbers

 

大意:判断是否是一个特定的数字。

思路:按照题目的要求写就行,枚举+判断是否是素数。

CODE:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using  namespace std;

#define MAXN 65100
int vis[MAXN] = { 0};
int prime[MAXN] = { 0};
int n, tot;

void init()
{
    tot =  0;
     int SIZE = ( int)sqrt(MAXN+ 0.5);
     for( int i =  2; i <= SIZE; i++)  if(!vis[i])
    {
        prime[tot++] = i;
         for( int j = i*i; j < MAXN; j += i) vis[j] =  1;
    }
     return ;
}

int mod1( int a,  int b,  int m)
{
     if(!b)  return  1;
     if(b ==  1return a%m;
     long ans = mod1(a, b/ 2, m);
    ans = ans*ans%m;
     if(b& 1) ans = ans*a%m;
     return ans;
}

int check( int n)
{
     if(!vis[n])  return  0;
     for( int i =  2; i < n; i++)
    {
         if(mod1(i, n, n) != i)
             return  0;
    }
     return  1;
}

int main()
{
    init();
     while(scanf( " %d ", &n) && n)
    {
         if(check(n))
        {
            printf( " The number %d is a Carmichael number.\n ", n);
        }
         else printf( " %d is normal.\n ", n);
    }
     return  0;
}

 

你可能感兴趣的:(number)