Carmichael Numbers(快速幂和素数筛选)
来源:互联网 发布:穿越火线为什么优化差 编辑:程序博客网 时间:2024/06/07 23:56
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 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[n]) { 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;}
- Carmichael Numbers(快速幂和素数筛选)
- UVA10006 - Carmichael Numbers(筛选构造素数表+快速幂)
- UVA 10006 - Carmichael Numbers (快速幂+筛素数)
- uva 10006 Carmichael Numbers(快速幂+素数)
- UVA - 10006 - Carmichael Numbers (快速幂+素数判断)
- UVA - 10006 Carmichael Numbers(素数筛+快速幂)
- Uva 10006 Carmichael Numbers(数论、快速幂、素数筛法)
- UVa10006 Carmichael Numbers【素数判定+快速模幂】
- Uva 10006 - Carmichael Numbers【素数+快速幂取模】
- UVA10006-快速幂Carmichael Numbers
- UVa 10006 Carmichael Numbers 【快速幂+素数打表(埃氏筛法)】
- UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)
- UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)
- UVa10006-Carmichael Numbers 快速幂运算
- Uva 10006 Carmichael Numbers 快速幂
- uva 10006 Carmichael Numbers(快速幂)
- UVA 10006 - Carmichael Numbers 快速幂
- Uva 10006-Carmichael Numbers(快速幂)
- poj 2635
- qt 静态库 动态库 elf 生成和使用
- android为每个屏幕尺寸和分辨率做设计(一)
- 永久性解决ORA-27125: unable to create shared memory segment
- Hive中ObjectInspector的作用
- Carmichael Numbers(快速幂和素数筛选)
- 作用域
- android:如何用一天时间,写出“飞机大战”这样的游戏!(无框架-SurfaceView绘制)
- Spring Integration --- Gateway
- 概述:软件开发工具
- NSString类和NSMutableString类常用方法
- poj 3252 Round Numbers 组合数学
- LeetCode-Restore IP Addresses
- maven与nexus的相关问题