Uva 10006 - Carmichael Numbers


 // Accepted---Time::0.139
 
// Carmichael number হচ্ছে সেইসব নাম্বার যাদের ক্ষেত্রে a^n mod n = a ;(Fermat's Little Theorem) সত্য হবে 
// কেবল 2 থেকে n-1 পর্যন্ত । 
// একবার যদি a^n mod n = a ;(Fermat's Little Theorem) মিথ্যা হয় তাহলে 
// সেটা Carmichael number নাম্বার নয় । 
 
// Header file begin
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#include<vector>
#include<cmath>
#include<cctype>
#include<sstream>
#include<set>
#include<list>
#include<stack>
#include<utility>
#include<queue>
#include<algorithm>
#include<cstdlib>
#include<bitset>
#include<iomanip>
#include<ctime>
 #include <time.h>
// End
//..........
// Macro
#define sf scanf
#define pf printf
#define lp1(i,n) for(i=0;i<n;i++)
#define lp2(i,n) for(i=1;i<=n;i++)
#define mset(c,v) memset(c,v,sizeof(c))
#define cp(a) cout<<" "<<a<<" "
#define nl puts("")
#define sq(x) ((x)*(x))
#define all(x) x.begin(),x.end()
#define reall(x) x.rbegin(),x.rend()
#define s_wap(x,y) x^=y;y^=x;x^=y;
#define sz size()
#define gc getchar()
#define pb push_back
#define freader freopen("input.txt","r",stdin)
// End.........

// Size
#define mx7 20000100
#define mx6 1500000
#define mx5 100005
#define mx4 1000100
#define inf 1<<30                                           //infinity value
#define eps 1e-9
#define mx (65540)
#define mod 1000000007
#define pi acos(-1.0)

// Macros for Graph

#define white 1
#define gray 2
#define black 3
#define nil 0

using namespace std;
/***************/

// typedef

typedef long long LL;
typedef long L;
typedef unsigned long long ull;
typedef unsigned long ul;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef vector<int>vi;
typedef vector<long long> vll;
typedef vector<long>vl;
typedef vector<char>vch;
typedef vector<string>vs;
typedef map<int,int>mpii;
typedef map<int,bool>mpbi;
typedef map<long,bool>mpbl;
typedef map<long long,bool>mpbll;
typedef map<char,int>mpci;
typedef map<char,bool>mpbc;
typedef map<string,int>mpsi;
typedef map<long long,long long>mpll;

// template

template<class T> T gcd(T a, T b ) {return b!=0?gcd<T>(b,a%b):a;}
template<class T> T large(T a, T b ) {return a>b?a:b;}
template<class T> T small(T a, T b ) {return a<b?a:b;}
template<class T> T diffrnce(T a, T b) {return a-b<0?b-a:a-b;}

bitset<mx4+9>bs;
int prm[(mx4>>1)+9],plen=0;
LL psz=mx4+1;

void sieve()
{
    bs.set();
    bs[0]=bs[1]=0;
    LL i,j;

    for(i=2;i<=psz;i++)
    {
        if(bs[i])
        {
            for(j=i*i;j<=psz;j+=i)
            {
                bs[j] = 0;
            }

            prm[plen++]=i;
        }
    }

    //lp1(i,100)cp(prm[i]);
}

bool isPrime(LL n)
{
    if(n <= psz)
    {
        return bs[n];
    }

    for(int i=0;i<plen and prm[i]*prm[i]<=n; i++)
    {
        if(!(n%prm[i]))
        {
            return false;
        }
    }

    return true;
}

LL big_mod(LL x, LL y, LL m)
{
    if(!y)
    {
        return 1;
    }

    if(y&1)
    {
        return (x * big_mod(x,y-1,m))%m;
    }
    else
    {
        LL val = big_mod(x,y/2,m);
        return ((val*val)%m);
    }
}

//int crmicl[mx4+9];
//
//void isCarmicael(LL n)
//{
//    for(LL i=2;i<=65010;i++)
//    {
//        if(isPrime(i))
//        {
//            crmicl[i]=0;
//            continue;
//        }
//
//        LL res = big_mod(i,n,n);
//
//        if(res != i)
//        {
//            crmicl[i] = 1;
//        }
//    }
//}

int main()
{
    sieve();

    LL num;

    while(~sf("%lld",&num) and num)
    {
        //isCarmicael(num);

        if(isPrime(num))
        {
            pf("%lld is normal.\n",num);
            continue;
        }

        bool f=false;

        for(LL i=2;i<num;i++)
        {
            LL res = big_mod(i,num,num);

            if(res != i)
            {
                //pf("The number %lld is a Carmichael number.\n",num);
                f=true;
                break;
            }
        }

        if(f)
        {
            pf("%lld is normal.\n",num);
        }
        else
        {
            pf("The number %lld is a Carmichael number.\n",num);
        }
    }

    return 0;
}
 

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number