您的位置:首页 > 其它

UVA 10006 Carimichael Numbers(快速幂)

2017-01-28 16:30 429 查看
大年初一无聊刷道题吧…..

【中文题意】给你一个整数,问你它是Carimichael Numbers还是正常数。关于那个Carimichael Numbers定义:我们把对任意的1 < x < n都有x^n恒等x(mod n)成立的合数(不是素数)n称为Carimichael Numbers。

【思路分析】先用筛法预处理一遍所有数,然后再用快速幂判断就好了。

【AC代码】

#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long

bool is_prime[70000];

void init()
{
memset(is_prime,1,sizeof(is_prime));
is_prime[2]=0;
for(int i=2;i<70000;i++)
{
if(is_prime[i])
{
for(int j=i*2;j<70000;j+=i)
{
is_prime[j]=false;
}
}
}
}

LL mod_pow(LL x,LL n,LL mod)
{
LL res=1;
while(n>0)
{
if(n&1)
res=res*x%mod;
x=x*x%mod;
n>>=1;
}
return res;
}

int main()
{
LL n;
init();
while(~scanf("%lld",&n))
{
if(n==0)break;
if(is_prime
)
{
printf("%lld is normal.\n",n);
continue;
}
int flag=0;
for(LL i=2;i<n;i++)
{
if(mod_pow(i,n,n)!=i)
{
flag=1;break;
}
}
if(flag==0)
{
printf("The number %lld is a Carmichael number.\n",n);
}
else
{
printf("%lld is normal.\n",n);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva