UVa 10006 Carmichael Numbers(数论&卡米歇尔数&考塞特判别法)

Posted synapse7

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVa 10006 Carmichael Numbers(数论&卡米歇尔数&考塞特判别法)相关的知识,希望对你有一定的参考价值。

10006 - Carmichael Numbers

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=947

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: 


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.


题意:

判断一个数是否为卡米歇尔数,可用考塞特判别法,参考《数论概论》P80:


思路:

根据考塞特判别法,我们只需要求≤maxn/3的素数,

但由于要越界多判定一个素数的缘故,要保证多求一个素数(在循环中加限制反而会变慢),有两种方法:

方法1. 把N的上限扩大1%,可以保证多求一个素数。

方法2. 通过事先计算求,这里Pn表示第n个素数,

a)把maxn设为65000打印出c值(见代码),此题为2432;

b)扩大1%倍maxn,比如取maxn=66000;

c)打印出prime[2432]*3,得到65019,即设maxn为65019即可。

PS:上述方法可扩展到求≤f(maxn)的素数的其他题目,比如求≤√maxn的素数等。

筛法复杂度:O(Nlog log N)

考塞特判别法复杂度:


先说结果:小于65000的卡米歇尔数为:

const int CarmichaelNumbers[15] = 561,1105,1729,2465,2821,6601,8911,10585,15841,29341,41041,46657,52633,62745,63973;


完整代码:

/*0.056s*/

#include<cstdio>
const int maxm = 2433;
const int maxn = 65019;

int prime[maxm], c = 0;
bool vis[maxn];

inline void create_prime()

	int m = maxn / 3;
	for (int i = 2; i <= m; ++i)
		if (!vis[i])
		
			prime[c++] = i;
			for (int j = i * i; j < maxn; j += i)
				vis[j] = true;
		


int main()

	create_prime();
	//printf("##%d\\n",c);
    //printf("##%d\\n",prime[2432] * 3);
	int n, maxi;
	while (scanf("%d", &n), n)
	
		if (vis[n] && n & 1)
		
			maxi = n / 3;//因为n是奇数
			for (int i = 0; prime[i] <= maxi; ++i)
				if (n % prime[i] == 0)
					if (n % (prime[i] * prime[i]) == 0 || (n - 1) % (prime[i] - 1))
						goto flag;
			printf("The number %d is a Carmichael number.\\n", n);
			continue;
		
flag:
		printf("%d is normal.\\n", n);
	
	return 0;




以上是关于UVa 10006 Carmichael Numbers(数论&卡米歇尔数&考塞特判别法)的主要内容,如果未能解决你的问题,请参考以下文章

[UVA11100]The Trip

Carmichael Numbers (快速幂)

W: http://archive.ubuntukylin.com:10006/ubuntukylin/dists/xenial/InRelease: Signature by key 6CE35A4

numb2string

python切片

二位平面坐标的离散化