UVa10006 - Carmichael Numbers

#include <stdio.h>

#define MAX 65010

int prime[MAX];

int is_prime(int  a);
long long big_mod(int a, int n, int m);
int is_ok(int n);

int main()
{
    int i, n;

    #ifndef ONLINE_JUDGE
        freopen("d:\\UVa\\uva_in.txt", "r", stdin);
    #endif

    for (i = 2; i < MAX; i++)
    {
        prime[i] = is_prime(i);
    }



    while (scanf("%d", &n) && (n != 0))
    {
        if (!prime[n] && is_ok(n))
            printf("The number %d is a Carmichael number.\n", n);
        else
            printf("%d is normal.\n", n);
    }
    return 0;
}

int is_prime(int a)
{
    int i;

    for (i = 2; i * i <= a; i++)
    {
        if (a % i == 0)
            return 0;
    }

    return 1;
}

long long  big_mod(int a, int n, int m)
{
    long long x;

    if (n == 0)
        return 1;
    else if (n % 2)
    {
        return (a * big_mod(a, n - 1, m) % m);
    }
    else
    {
        x = big_mod(a, n / 2, m);
        return (x * x % m);
    }
}

int is_ok(int n)
{
    int i;

    for (i = 2; i < n; i++)
    {
        if (big_mod(i, n, n) != i)
            return 0;
    }

    return 1;
}

你可能感兴趣的:(UVa10006 - Carmichael Numbers)