/*
* [题意]
* 输入n,若满足如下两个条件,则n是Carmichael number
* 1、n不是素数
* 2、对于所有a(2<=a<n),有(a^n)%n = a
*
* [解题方法]
* 快速幂取模,注意运算过程中的乘法溢出int
*/
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define LL long long
#define M 65000
#define inf 0x3fffffff
int vis[M];
int qmod (int a, int b, int c) //二进制思想快速求(a^b)%c
{
int res = 1;
for ( ; b; b >>= 1)
{
//强制转换LL,是因为乘法有可能溢出
if (b & 1) res = (LL)res*a % c;
a = (LL)a*a % c;
}
return res;
}
int main()
{
int n, a, i, j;
for (i = 2; i < M; i++) //素数筛法
if (!vis[i])
for (j = i+i; j < M; j+=i)
vis[j] = 1;
while (scanf("%d", &n), n)
{
if (!vis[n]) //是素数
{
printf ("%d is normal.\n", n);
continue;
}
for (a = 2; a < n; a++)
if (qmod(a, n, n) != a)
break;
if (a < n) printf ("%d is normal.\n", n);
else printf ("The number %d is a Carmichael number.\n", n);
}
return 0;
}
UVA 10006 Carmichael Numbers
最新推荐文章于 2024-10-31 16:16:13 发布