UVA 10006 - Carmichael Numbers(快速幂取模)
来源:互联网 发布:mac 文件打包命令 编辑:程序博客网 时间:2024/04/30 15:55
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:
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.
============================
快速幂+素数判断
#include <iostream>#include <cstdio>using namespace std;int prime(int x){ for(int i=2;i*i<=x;i++) { if(x%i==0) return 0; } return 1;}long long quick_mod(long long a,long long b,long long m){ long long ans=1; a%=m; while(b) { if(b%2) { ans=ans*a%m; b--; } b/=2; a=a*a%m; } return ans;}int solve(int x){ for(int i=2;i<x;i++) { if(quick_mod(i,x,x)!=i) return 0; } return 1;}int main(){ int n; while(~scanf("%d",&n)) { if(n==0) break; //cout<<prime(n)<<" "<<solve(n)<<endl; if(!prime(n)&&solve(n)) printf("The number %d is a Carmichael number.\n",n); else printf("%d is normal.\n",n); } 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 (暴力+快速幂取模)
- 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(数论+快速幂)
- 配置OpenCV产生flann\logger.h(66): error C4996: 'fopen': This function or variable may be unsafe问题
- 数据库设计中的14个技巧解决方法
- 进制转换
- 从简单的三角形开始
- 正面有Android手机常用的三枚虚拟按键
- UVA 10006 - Carmichael Numbers(快速幂取模)
- HQL in参数问题
- 提示用户分别输入10个学生的成绩(百分制), 最后输出各个学员序号对应的成绩。
- 千兆网接口的学习之路之------Avalon-MM Pipeline Bridge
- Linuxc 下用穷举算法求解爱因斯坦的一个数学问题
- 字符串拼接
- 黑马程序员 ---XML编程
- 三维数组的应用
- 二维数组的最大值和最小值