UVa 10006 - Carmichael Numbers

连接网址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=947


这一题的用到快速幂的求法:

 代码如下


AC代码:

# include <cstdio>
# include <cmath>
using namespace std;
typedef long long int LL;
int mod_pow(int x, int n, int mod){
	if(n==0){
		return 1;
	}
	LL ans;
	ans=mod_pow(x, n/2, mod);
	ans=ans*ans%mod;
	if(n%2==1){
		ans=ans*x%mod;
	}
	return (int)ans;
}
int is_prime(int n){
	if(n<=1){
		return 0;
	}
	for(int i=2; i<=sqrt(n); i++){
		if(n%i==0){
			return 0;
		}
	}
	return 1;
}
int main(){
	int n, i, j, k, flage;
	while(scanf("%d", &n)){
		if(n==0){
			break;
		}
		if(is_prime(n)){
			printf("%d is normal.\n", n);
			continue;
		}
		flage=0;
		for(i=2; i<n; i++){
			if(mod_pow(i, n, n)!=i){
				flage=1;
				break;
			}
		}
		if(flage){
			printf("%d is normal.\n", n);
		}
		else{
			printf("The number %d is a Carmichael number.\n", n);
		}
	}
	return 0;
}


你可能感兴趣的:(ACM,uva,快速幂,高效算法,判断素数)