UVa 10006 Carmichael Numbers(快速幂)


1.先判断是不是素数;
2.再按题意遍历2~n-1即可,用快速幂加速就可以了;(注意用long long

代码:

#include<iostream>
using namespace std;
typedef long long LL;
bool isPrime(int n){
	for(int i=2;i*i<=n;i++) if(n%i==0) return false;
	return n!=1;
}
LL N;
LL fastPow(LL x,LL n){
	if(n==0) return 1ll;
	LL res=fastPow(x*x%N,n>>1);
	if(n&1) res=res*x%N;
	return res;
}
void solve(){
	bool flag=true;
	for(LL i=2;i<N&&flag;i++) if(fastPow(i,N)!=i) flag=false;
	if(flag) cout<<"The number "<<N<<" is a Carmichael number.
";
	else cout<<N<<" is normal.
";
}
int main(){
//	freopen("Arutoria.txt","r",stdin);
	while(cin>>N,N){
		if(isPrime(N)) cout<<N<<" is normal.
";
		else solve();
	}
	return 0;
}