Thursday, 30 April 2015

UVA 10006 Carmichael Numbers

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

bool status[65010];
void primeGenerator(int n)
{
    int sq=sqrt(n);
    status[0]=status[1]=true;
    for(int i=3;i<=sq;i+=2)
        if(!status[i])
            for(int j=i*i;j<=n;j+=i)
                status[j]=true;
return;
}

long long bigMod(long long base, long long power, long long mod)
{
    long long ret=1;
    while(power)
    {
        if(power & 1)
            ret=(ret*base)%mod;
        base = (base*base)%mod;
        power=power>>1;
    }
    return ret;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    long long n, tc, kk=1;
    string s;
    primeGenerator(65005);
    while(cin>>n && n)
    {
        bool chk=true;
        if(!status[n])
        {
            cout<<n<<" is normal.\n";
            continue;
        }
        for(long long i=2;i<n;i++)
                if(bigMod(i, n, n)!=i)
                {
                    chk=false;
                    break;
                }
        if(chk) cout<<"The number "<<n<<" is a Carmichael number.\n";
        else cout<<n<<" is normal.\n";
    }
return 0;
}

No comments:

Post a Comment