UVa 10006

题目:判断一个数是不是Carmichael number。

分析:数论。利用素数的随进判定算法,可以通过判定并且不是素数的数称为Carmichael number。

            首先,利用筛法对素数进行打表。

            然后,利用费马小定理枚举所有的a进行判断。

#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

typedef long long LL;

int prime[65000];

LL powmod( int a, int n, int m )
{
	if ( n == 1 ) return a%m;
	LL x = powmod( a, n/2, m );
	x = (x*x)%m;
	if ( n%2 ) x = (x*a)%m;
	return x;
}

int tests( int n )
{
	for ( int i = 2 ; i < n ; ++ i )
		if ( powmod( i, n, n ) != i )
			return 0;
	return 1;
}

int main()
{
	//打表计算素数
	for ( int i = 0 ; i < 65000 ; ++ i )
		prime[i] = 1;
	for ( int i = 2 ; i < 65000 ; ++ i )
		if ( prime[i] )
			for ( int j = 2*i ; j < 65000 ; j += i )
				prime[j] = 0;
	
	int n;
	while ( cin >> n && n )
		if ( !prime[n] && tests( n ) )
			cout << "The number " << n << " is a Carmichael number.
";
		else cout << n << " is normal.
";
	
	return 0;
}
原文地址:https://www.cnblogs.com/riskyer/p/3400368.html