UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)

网友投稿 867 2022-11-13

UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)

UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)

M - Carmichael Numbers

Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu

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

172917 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.

题意:判断一个数是不是Carmichael数。

如果一个数不是素数,且对于任意的2< a

,则称n是Carmichael数;否则n就不是Carmichael数。

这个题的关键是求快速幂。

#include#include#include#define aa long longint a[66000];void get_prime() //筛法求素数{ int i,j,m=sqrt(65001+0.5); memset(a,0,sizeof(a)); for(i=2; i<=m; i++) { if(!a[i]) //素数为0 { for(j=i*i; j<65001; j+=i) a[j]=1; //非素数为1 } }}aa runFermatPow(aa a, aa n, aa m){ int result = 1; while (n > 0) { if ((n & 1) == 1) result = (result * a) % m; a = (a * a) % m; n >>= 1; } return result; /* if(n==0) return 1; aa x=runFermatPow(a,n/2,m); aa ans=x*x%m; if(n%2==1) ans=ans*a%m; return ans;*/}int main(){ get_prime(); aa i,n; bool flag; while(~scanf("%lld",&n)&&n) { if(!a[n]) { printf("%lld is normal.\n",n); continue; } flag=true; for(i=2; i

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringCloud学习笔记之Feign远程调用
下一篇:HDU--2674 N!Again
相关文章

 发表评论

暂时没有评论,来抢沙发吧~