CarMichael 数 UVA 10006

大意:

给定一个数,让你判断是否这个数既不是素数,又能满足

公式的数字 n,其中 a 从 2到 n-1

思路:

素数表打表,如果不是素数,再用快速幂判断一下,注意中间变量会超int 所以要中间加longlong 防止溢出

代码:

#include<iostream>
#include<cstring>
#include<math.h> 
#include<cstdio>
using namespace std;

const int MAXN = 65005;
int N;

bool prime_list[MAXN];
void init(){
	memset(prime_list,false,sizeof(prime_list));
	int m  = sqrt(MAXN+0.5);
	for(int i=2;i<=m;i++)
		if(!prime_list[i])
		for(int j=i*2;j<=MAXN;j+=i){
			prime_list[j] = true;
		}
}
int pow_num(int a,int n,int mod){
	
	int res =1;
	int m = a%mod;
	while(n>0){
		if(n&1) res= (long long)res *m%mod;
		m = (long long)m*m %mod;
		n = n>>1;
	}
	return res;
}

int main(){
	init();
	while(~scanf("%d",&N)){
		if(N<=0) break;
		if(!prime_list[N]) {
			printf("%d is normal.\n",N);  continue;
		}
		int count =2;
		for(int i=2;i<N;i++){
			int res = pow_num(i,N,N);
			if( res != i ) break;
			count ++;
		}
		if(count!=N) printf("%d is normal.\n",N);
		else printf("The number %d is a Carmichael number.\n",N);
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/Willen_/article/details/88100734