UVA-10006 Carmichael Numbers

5 篇文章 0 订阅
4 篇文章 0 订阅

题目传送门

题目的意思就是给出n ,看它是否满足下面两个条件:

  1. n是合数 。
  2. 对于区间 [ 2 , n ) 的任意数a ,都使得 pow (a , n) % n == a成立。

首先判断n是不是合数,我使用素数筛进行一次性记录判断。
然后用快速幂遍历所有小于n的a(2<=a< n),判断它是不是满足第二个条件.
注意使用long long 。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=65000+10;
bool isprime[maxn];
int prime[maxn];
int tot,mod;
ll mypow(ll a,ll n)
{
	ll res=(ll)1;
	while(n)
	{
		if(n&1)
		res=res*a%mod;
		a=a*a%mod;
		n>>=1;
	}
	return res;
}
void prime_match()
{
	tot=0;
	fill(isprime,isprime+maxn,true);
	isprime[0]=isprime[1]=false;
	for(int i=2;i<maxn;i++)
	{
		if(isprime[i])
		{
			prime[tot++]=i;
		}
		for(int j=0;j<tot&&i*prime[j]<maxn;j++)
		{
			isprime[i*prime[j]]=false;
			if(!(i%prime[j]))
			break;
		}
	}
}
int main()
{
	int n,i;
	prime_match();
	while(scanf("%d",&n)==1&&n)
	{
		if(isprime[n])
		{
			printf("%d is normal.\n",n);
			continue;
		}
		else
		{
			mod=n;
			for(i=2;i<n;i++)
			{
				int cnt=mypow(i,n);
				if(cnt!=i)
				break;
			}
			if(i>=n)
			{
				printf("The number %d is a Carmichael number.\n",n);
			}
			else
			{
				printf("%d is normal.\n",n);
			}
		}
	}
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ITKaven

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值