10006

这道题蛋疼了一下午。。。竟然最后是快速幂里面要用long long,真蛋疼,

思路还是没错的,就是nlogn的算法

//============================================================================
// Name        : main.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int MAXV = 65000;
bool flag[MAXV+5]; //标志一个数是否为素数
int n;

long long quickpow(long long m,long long n,long long k)
{
    long long b = 1;
    while (n > 0)
    {
          if (n & 1)
             b = (b*m)%k;
          n = n >> 1 ;
          m = (m*m)%k;
    }
    return b;
}

int Is(int m)
{
	for(int i = 2;i < m;i++)
	{
		if(quickpow(i, m, m) != i)
		{
			return 0;
		}
	}
	return 1;
}

int isprime(int x)
{
	for(int i = 2;i <= sqrt(x);i++)
	{
		if(x%i == 0) return 0;
	}
	return 1;
}

int main() {
	//freopen("a.txt", "r", stdin);
	while(scanf("%d", &n)&&n)
	{
		if(isprime(n) == 0&&Is(n) == 1) printf("The number %d is a Carmichael number.\n", n);
		else printf("%d is normal.\n", n);
	}
	return 0;
}

posted @ 2011-05-14 01:58  KOKO's  阅读(316)  评论(0编辑  收藏  举报