UVA 10006 Carmichael Numbers


/*
*   [  ]
*     n,         , n Carmichael number
*       1、n    
*       2、    a(2<=a<n), (a^n)%n = a
*
*   [    ]
*        ,            int
*/

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define LL long long
#define M 65000
#define inf 0x3fffffff

int vis[M];

int qmod (int a, int b, int c)    //        (a^b)%c
{
    int res = 1;
    for ( ; b; b >>= 1)
    {
        //    LL,          
        if (b & 1) res = (LL)res*a % c;
        a = (LL)a*a % c;
    }
    return res;
}

int main()
{
    int n, a, i, j;
    for (i = 2; i < M; i++) //    
        if (!vis[i])
            for (j = i+i; j < M; j+=i)
                vis[j] = 1;
    while (scanf("%d", &n), n)
    {
        if (!vis[n])        //   
        {
            printf ("%d is normal.
", n); continue; } for (a = 2; a < n; a++) if (qmod(a, n, n) != a) break; if (a < n) printf ("%d is normal.
", n); else printf ("The number %d is a Carmichael number.
", n); } return 0; }