uva 10006 10006 - Carmichael Numbers
2015-07-28 17:23
309 查看
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:
\begin{displaymath}a^n \bmod n = a\end{displaymath}
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,都满足a^n mod n = a,那么n就是一个米切尔数
刚开始的时候看错了,以为只要在2~~n-1中有一个满足上述关系式的就是米切尔数,提交多次都超时了,改过之后AC。
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:
\begin{displaymath}a^n \bmod n = a\end{displaymath}
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,都满足a^n mod n = a,那么n就是一个米切尔数
刚开始的时候看错了,以为只要在2~~n-1中有一个满足上述关系式的就是米切尔数,提交多次都超时了,改过之后AC。
#include <iostream> #include <cstdio> using namespace std; bool is_prime(int n) { for(int i=2;i*i<=n;i++) if(n%i==0) return false; return true; } int quickpow(int a, int b, int c) {//计算,对大数很管用计算pow(a,b)%c long long int ans = 1; a=a%c; while (b > 0) { if (b & 1) ans = (ans*a)%c; b = b >> 1 ; a = ((long long int )a*a)%c; } return ans; } int main(void) { int n; while(scanf("%d",&n)!=EOF&&n) { if(is_prime(n)) printf("%d is normal.\n",n); else { int ok=0; for(int i=2;i<=n-1;i++) { if(i!=quickpow(i,n,n)) { ok=1; printf("%d is normal.\n",n); break; } } if(ok==0) printf("The number %d is a Carmichael number.\n",n); } } return 0; }
相关文章推荐
- 树莓派 raspberry安全关机命令重启命令
- 转如何在oracle中导入dmp数据库文件
- BlockingQueue
- IOS-写文件到本地
- Activity的启动模式与flag详解
- 【原】如何阅读学术论文
- 【MySQL笔记】mysql来源安装/配置步骤和支持中国gbk/gb2312编码配置
- 总线接口与计算机通信(三)UART起止式异步通用串行数据总线
- Android中按钮的水波纹点击效果的实现
- 回顾:中网饱经沧桑劫后余生 万平国回首艰辛历程
- GIT使用指南
- Ubuntu 下 su:authentication failure的解决办法
- C#虚方法和抽象方法 .
- JAVA动态加载类和静态加载类
- 使用JavaScript修改浏览器URL地址栏的实现代码
- hdoj,愚人节的礼物
- toast 因 activity 的样式设置而变化
- 微信开发(八)自动回复和多客服开发
- NoSQL数据库:Redis适用场景及产品定位
- netty处理粘包问题——1