Carmichael numbers

An important topic nowadays in computer science is cryptography. Some people even think that cryptography is the only important field in computer science, and that life would not matter at all without cryptography. ´ Alvaro is one of such persons, and is designing a set of cryptographic procedures for cooking paella. Some of the cryptographic algorithms he is implementing make use of big prime numbers. However, checking if a big number is prime is not so easy. An exhaustive approach can require the division of the number by all the prime numbers smaller or equal than its square root. For big numbers, the amount of time and storage needed for such operations would certainly ruin the paella. However, some probabilistic tests exist that offer high confidence at low cost. One of them is the Fermat test. Let a be a random number between 2 and n−1 (being n 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 are bad news. Some numbers that are not prime still pass the Fermat test with every number smaller than themselves. These numbers are called Carmichael numbers. In this problem you are asked to write a program to test if a given number is a Carmichael number. Hopefully, the teams that fulfill the task will one day be able to taste a delicious portion of encrypted paella. As a side note, we need to mention that, according to ´Alvaro, the main advantage of encrypted paella over conventional paella is that nobody but you knows what you are eating.
Input
The input will consist of a series of lines, each containing a small positive number n (2 < n < 65000). A number n = 0 will mark the end of the input, and must not be processed.
Output
For each number in the input, you have to print if 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.
题意 在2和n-1之间随机选取一个数,如果a^nmodn=a成立,n就可能是一个素数。如果一个数通过费马测试很多次那么它就很可能是一个素数。
不幸的是,一些数不是素数但是它们依然能通过每一个比它小的数的费马测试。这些数被称作卡迈克尔数
给你一个数,判断它是不是迈克尔数
这道题涉及2个知识点 素数筛和快速幂
素数筛 这又涉及算数基本定理:任何大于1的正整数都能唯一分解为有限个质数的乘积
因为一个数肯定是由合数和质数构成
合数又可以分解成质数和合数,最后递归下去就变成质数的乘积

在这里插入代码片void so()
{
	prim[0]=prim[1]=1;
	for(int i=2;i<=maxn;i++)
	{
		if(!prim[i])
		for(int j=i*2;j<=maxn;j+=i)
		prim[j]=1;  //如果不是素数标记为1
	}
}

快速幂

int power(int a,int b)
{
    int ans=1,base=a;
    while(b!=0)
    {
        if(b&1)    //奇数
            ans*=base;
        //x&1==0为偶   x&1==1为奇
        base*=base;
        b>>=1;
    }
}

这是用递归写的快速幂 看不懂的我来举例说明
11(10)–>1011(2) //11转化为二进制//
1101&1==1 ans=a^1; 这时 a = a^2; b右移一位变成101
还是为奇 ans=a^2; 这是a=a^4; b右移一位变成10
a=a^4 以此类推
下面就直接写就行了

在这里插入代码片#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int prim[maxn];
void so()
{
	prim[0]=prim[1]=1;
	for(int i=2;i<=maxn;i++)
	{
		if(!prim[i])
		for(int j=i*2;j<=maxn;j+=i)
		prim[j]=1;
	}
}
ll pi(ll a,ll b,int mod)
{
	ll res=1;
	while(b)
	{
		if(b&1)
		res=res*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return res%mod;
}
int main()
{
	so();
	int n;
	while(~scanf("%d",&n)&&n)
	{
		if(prim[n])
		{
			int flag=0;
			for(int i=2;i<n;i++)
			{
				if(pi(i,n,n)!=i)
				{
					flag=1;
					break;
				}
			 } 
			if(!flag)
			printf("The number %d is a Carmichael number.\n",n);
			else
			printf("%d is normal.\n",n);
		}
		else
		printf("%d is normal.\n",n);
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值