uva 10006 Carmichael Numbers
来源:互联网 发布:android系统源码是什么 编辑:程序博客网 时间:2024/05/22 12:17
这是uva上的一道题,用快速幂解,很裸的模板题。
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.这是uva上的一道题,用快速幂解,很裸的模板题。
Carmichael Numbers
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
17291756111094310
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.
Miguel Revilla
2000-08-21
/*1388917610006Carmichael NumbersAcceptedC++0.376*/#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define ll long longbool prime(int x){ for(int i=2;i*i<=x;i++) if(x%i==0) return false; return true;}ll mod_pow(ll x,ll n,ll mod){ ll res=1; while(n>0) { if(n&1) res=res*x%mod; x=x*x%mod; n>>=1; } //cout<<"res "<<res<<endl; return res;}int main(){ ll n; A:while(scanf("%lld",&n)&&n) { if(prime(n)) { cout<<n<<" is normal."<<endl; continue; } for(int i=2;i<n;i++) if(mod_pow(i,n,n)!=i%n) { cout<<n<<" is normal."<<endl; goto A; } cout<<"The number "<<n<<" is a Carmichael number."<<endl; } return 0;}
- UVa 10006 Carmichael Numbers
- uva 10006 - Carmichael Numbers
- UVA 10006 Carmichael Numbers
- UVa 10006 - Carmichael Numbers
- Uva - 10006 - Carmichael Numbers
- UVa 10006 - Carmichael Numbers
- UVA - 10006 Carmichael Numbers
- UVa 10006 - Carmichael Numbers
- UVa 10006 Carmichael Numbers
- uva 10006 Carmichael Numbers
- uva 10006 Carmichael Numbers
- UVA 10006 Carmichael Numbers
- UVA 10006 Carmichael Numbers
- UVA - 10006 Carmichael Numbers
- UVA 10006 Carmichael Numbers
- UVa-10006 Carmichael Numbers
- UVa 10006 - Carmichael Numbers
- uva 10006 Carmichael Numbers
- UIButton解析(iOS7)
- 偶的Android 视频学习之路
- sql oracle如何删除某一用户下所有表数据
- Linux进程间通信-基于内存(共享内存)
- Spring源码学习MVC:HandlerMapping的注册与查找
- uva 10006 Carmichael Numbers
- 查询多个表放在一个集合中用union做连接
- Android与服务器端数据交互(基于SOAP协议整合android+webservice)
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- 机房收费系统5之视图
- WA 了的uva 10806 - Dijkstra, Dijkstra.
- UVA590- Always on the run
- Ehcache页面缓存的使用
- 深入浅出观察者模式