Carmichael Numbers(快速幂和素数筛选)
2014-08-06 22:19
246 查看
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 designinga 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 numberby 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 numbern ( 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.
题意大致是:如果一个数不是素数,且对于任意的2< a <n满足方程,则称n是Carmichael数;否则n就不是Carmichael数
#include<stdio.h>#include<iostream>#include<memory.h>#include<algorithm>#include<math.h>using namespace std;int prime[65010];typedef long long ll;ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1) res=res*x%mod;x=x*x%mod;n>>=1;}return res;}void pri(){int i,j;memset(prime,0,sizeof(prime));int m=(int)sqrt(65010+0.5);for(i=2;i<=m;i++){if(prime[i]==0){for(j=i*i;j<=65010;j+=i)prime[j]=1;}}}int main(){pri();ll n,ans;while(scanf("%lld",&n),n){if(!prime){printf("%lld is normal.\n",n);continue;}int tag=1;for(ll i=2;i<=n-1;i++){if(pow_mod(i,n,n)!=i){tag=0;break;}}if(tag) printf("The number %lld is a Carmichael number.\n",n);//这里在输出时少了a,结果wa了4次else printf("%lld is normal.\n",n);}return 0;}[/code]
相关文章推荐
- HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)
- 快速筛选法求素数表
- 素数--筛选法打表,快速幂,欧拉函数,gcd
- 素数筛选与快速幂取模
- 快速筛选法求1到100的素数
- 筛选法--快速求素数
- 素数表的快速建立,合数分解,1-2^31内某个长度小于10w的区间素数筛选的三个模板及解析
- 筛选法快速查找素数
- 埃式筛法——快速筛选n以内的素数
- 埃氏筛法(快速筛选n以内素数的个数)
- 快速查找素数 187 (筛选找素数 模板)
- 素数快速筛选法
- JavaScript使用filter方法实现100以内素数的快速筛选
- 快速线性素数筛选[模板]
- 素数筛选法,快速获取素数序列
- 快速幂。素数筛选。辗转相除。
- HDOJ/HDU 2710 Max Factor(素数快速筛选~)
- 快速幂取模&&素数筛选&&最大公约数
- HDOJ/HDU 2710 Max Factor(素数快速筛选~)
- 素数筛实现素数的快速筛选