Carmichael Numbers //快速幂 筛选素数

Carmichael Numbers

Time Limit: 1 Sec   Memory Limit: 64 MB
Submit: 17   Solved: 8
[ Submit][ Status][ Discuss]

Description

Certain cryptographic algorithms make use of big prime numbers. However, checking whether a big number is prime is not so easy. Randomized primality tests exist that offer a high degree of confidence of accurate determination at low cost, such as the Fermat test. Let a be a random number between 2 and n - 1, where n is the number whose primality we are testing. Then, n is probably prime if the following equation holds: an mod n = a If a number passes the Fermat test several times, then it is prime with a high probability. Unfortunately, there is bad news. Certain composite numbers (non-primes) still pass the Fermat test with every number smaller than themselves. These numbers are called Carmichael numbers. Write a program to test whether a given integer is a Carmichael number.

Input

The input will consist of a series of lines, each containing a small positive number n ( 2 < n < 65, 000). A number n = 0 will mark the end of the input, and must not be processed.

Output

For each number in the input, print whether it is a Carmichael number or not as shown in the sample output

Sample Input

1729
17
561
1109
431
0

Sample Output

The number 1729 is a Carmichael number.
17 is normal.
The number 561 is a Carmichael number.
1109 is normal.
431 is normal.

HINT

Source

UVA toolkit

[ Submit][ Status][ Discuss]


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 65000+10
typedef long long ll;
ll prime[maxn+1];
ll isprime[maxn+1];
ll primelen=0;
void sushu(int n)
{
	for(ll i=1;i<=maxn;i++) //筛选素数
	 isprime[i]=1;
	 isprime[0]=isprime[1]=0;
	 for(ll i=2;i<=maxn;i++)
	 {
	 	if(isprime[i])
	 	{
	 	   prime[primelen++]=i;
			for(ll j=2*i;j<=maxn;j=j+i)
			 isprime[j]=0;	
		}
	 }
	 /*memset(prime,0,sizeof(prime)); //筛选素数
	 for(ll i=2;i*i<maxn;i++)
	 {
	 	if(!prime[i])
	 	{
	 	   for(ll j=i;j*j<maxn;j++)
			{
				prime[i*j]=1;
			}	
		}
	 }*/
}
ll pow(ll x,ll n,ll mod)//快速幂模板 
{
    ll res=1;
	while(n>0)
	{
	   if(n%2==1)	
	   {
	   	 res=res*x;
	   	 res=res%mod;
	   }
	   x=x*x;
	   x=x%mod;
	   n>>=1;
	}
	return res;	
}
int main()
{
	int n,i,j;
	sushu(maxn);
	/*for(ll i=1;i<20;i++) 
	{
		printf("%d ",prime[i]);
	}
	cout<<endl;
	for(ll i=0;i<20;i++)
	{
		printf("%d ",isprime[i]);
	}
	cout<<endl;
    cout<<primelen<<endl;
    */
	while(scanf("%d",&n),n)
	{
		bool ok=true;
		if(isprime[n])
		{
			ok=false;
		}
		if(ok)
		{
			for(ll i=2;i<n;i++)
		  {
		     ll temp=pow(i,n,n);  //验证i的n次方取余n是否等于i
			 if(temp!=i)//如果不等于i,就改变ok
			 {
			    ok=false;
				break;   	
			 } 	
		  }	
		}
		if(ok)
		{
			printf("The number %d is a Carmichael number.\n",n);
		}
		else
		{
			printf("%d is normal.\n",n);
		}
	}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值