UVa 10006 - 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: a^n mod n = a

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.

迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……

#include <iostream>
using namespace std;
int prime[65000];
long long powmod(int a,int n,int m)
{
if (n==1)return a%m;
long long x=powmod(a,n/2,m);
x=(x*x)%m;
if (n%2)x=(x*a)%m;
return x;
}
int tests(int n)
{
for (int i=2; i<n; ++i)
if (powmod(i,n,n)!=i)
return 0;
return 1;
}
int main()
{
for (int i =0; i<65000; ++i)
prime[i]=1;
for (int i=2; i<65000; ++i)
if (prime[i])
for (int j=2*i; j<65000; j+=i)
prime[j]=0;
int n;
while(cin>>n&&n)
if(!prime[n]&&tests(n))
cout<< "The number "<<n<<" is a Carmichael number."<<endl;
else cout<<n<<" is normal."<<endl;
return 0;
}

UVa 10006 - Carmichael Numbers的更多相关文章

  1. UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)

      Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some people e ...

  2. Uva 10006 Carmichael Numbers (快速幂)

    题意:给你一个数,让你判断是否是非素数,同时a^n%n==a (其中 a 的范围为 2~n-1) 思路:先判断是不是非素数,然后利用快速幂对每个a进行判断 代码: #include <iostr ...

  3. 【UVA - 10006 】Carmichael Numbers (快速幂+素数筛法)

    -->Carmichael Numbers  Descriptions: 题目很长,基本没用,大致题意如下 给定一个数n,n是合数且对于任意的1 < a < n都有a的n次方模n等于 ...

  4. UVA10006 - Carmichael Numbers

    题目链接:UVA10006 本来想直接打素数表,然后根据素数表来判断,结果一直超时,后来把素数表去掉,再在for循环中加判断才勉强过了. Some numbers that are not prime ...

  5. Carmichael Numbers - PC110702

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10006.html 原创:Carm ...

  6. Uva - 12050 Palindrome Numbers【数论】

    题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然 ...

  7. UVA.136 Ugly Numbers (优先队列)

    UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...

  8. UVA - 13022 Sheldon Numbers(位运算)

    UVA - 13022 Sheldon Numbers 二进制形式满足ABA,ABAB数的个数(A为一定长度的1,B为一定长度的0). 其实就是寻找在二进制中满足所有的1串具有相同的长度,所有的0串也 ...

  9. UVA10006 - Carmichael Numbers(筛选构造素数表+高速幂)

    UVA10006 - Carmichael Numbers(筛选构造素数表+高速幂) 题目链接 题目大意:假设有一个合数.然后它满足随意大于1小于n的整数a, 满足a^n%n = a;这种合数叫做Ca ...

随机推荐

  1. 【转载】MyBatis之传入参数

    原文地址:http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 在MyBatis的select.insert.update.dele ...

  2. Hibernate exercise 54

    针对马士兵的Hibernate讲解第54讲的练习: 1) 学生.课程.分数的设计,并用Hibernate操作 在实际中,一般是先手动写SQL(可以优化)去创建表和关系,再设置Hibernate配置为u ...

  3. 升级到win8.1后除IE11外,其它浏览器无法打开网页解决办法

    原文 : http://productforums.google.com/forum/#!topic/chrome/TUDjVQzf4Os 用管理员方式打开cmd 输入 netsh winsock r ...

  4. Leetcode#115 Distinct Subsequences

    原题地址 转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的 举个例子,S="aabb",T="ab".构造如下地图("."表示空位 ...

  5. Connection reset by peer: socket write error 连数据库出现改错

    1.网络原因 2.从池中获取连接后没有释放到池中导致的

  6. java web项目,java类中获得WEB-INF路径

    private static String getWebInfPath() { URL url = 当前类.class.getProtectionDomain().getCodeSource().ge ...

  7. safeseh+dep保护绕过

    [文章作者]       :h_one [漏洞程序名称]:mplayer.exe [漏洞类型]       :缓冲区溢出 [保护方式]       :safeseh+dep [操作平台]       ...

  8. Tomcat漏洞说明与安全加固

    Tomcat是Apache软件基金会的一个免费的.开放源码的WEB应用服务器,可以运行在Linux和Windows等多个平台上,由于其性能稳定.扩展性好.免费等特点深受广大用户的喜爱.目前,互联网上绝 ...

  9. Codeforces Round #216 (Div. 2)解题报告

    又范低级错误! 只做了两题!一道还被HACK了,囧! A:看了很久!应该是到语文题: 代码:#include<iostream> #include<];    ,m2=;    ;i ...

  10. JS范围

    JS API-->DOM/PoneGap/Cordova/Android/NodeJS JS OOP