UVA 1006 水题

挑战P122的水题


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cmath>
#include <vector>
using namespace std;

#define LL long long
LL n;

LL er(LL t)
{
	return (t*t) % n;
}


LL get(LL a, LL b, LL mod)//a^b
{
	if (b==0)	return 1;
	if (b % 2 == 0)	return er(get(a,b/2,mod)) % mod;
	else return ((er(get(a,b/2,mod))%mod)*a)%mod;
}

bool check(LL k)
{
	
	int flag = 0;
	for (int i = 2; i * i <= k; ++ i)
	{
		if (k % i == 0)	
		{
			flag = 1;
			break;
		}
	}
	if (flag==0)	return false;
	


	for (int i = 2; i != k; ++ i)
	{
		LL tmp = get(i, n, n);
		//cout << tmp << endl;
		if (tmp != i)	return false;
	}
	return true;
}

int main()
{
	while (1)
	{
		scanf("%lld", &n);
		if (n==0)	break;
		if (check(n))	printf("The number %lld is a Carmichael number.\n", n);
		else printf("%lld is normal.\n", n);
	}
	return 0;
}


你可能感兴趣的:(UVA 1006 水题)