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. TCP 粘包/拆包问题

    简介    TCP 是一个’流’协议,所谓流,就是没有界限的一串数据. 大家可以想想河里的流水,是连成一片的.期间并没有分界线, TCP 底层并不了解上层业务数据的具体含义 ,它会根据 TCP 缓冲区 ...

  2. 读书笔记:<世界是数字的>

    世界是数字的?第一次听到这个名词不由的感到惊讶,我有听过地球是海洋的,那世界不应该是人类共同拥有的吗或者也可以说世界是大自然的?为什么世界偏偏是数字的呢?我带着满心的疑问去看了那本<世界是数字的 ...

  3. 版本控制 - SVN/TortoiseSVN

    研读了blog: 1. http://www.open-open.com/lib/view/open1346982569725.html  2. http://www.360doc.com/conte ...

  4. ToolStripButton样式

    public static class Extensions { public static void SetMouseDownStyle(this ToolStripButton button) { ...

  5. Leetcode#166 Fraction to Recurring Decimal

    原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...

  6. 编写一函数用来实现左右循环移位。函数原型为move(value,n);n>0时右移n位,n<0时左移|n|位。

    #include<stdio.h> #include<stdlib.h> int main(){ setbuf(stdout,NULL); int move(int,int); ...

  7. zero to one:创业秘籍并不存在,因为任何创新都是新颖独特的,任何权威都不可能具体规定如何创新

    彼得·蒂尔(Peter Thiel)的新作<从0到1>从预售开始就占据美国亚马逊排行榜第一名的位置,被一批创业家和企业家评为“迄今为止最好的商业书”.这是一本关于如何创建创新公司的书,主要 ...

  8. maven 构建spring ssh mybatis 配置

    详情参与 http://blog.csdn.net/yuguiyang1990/article/details/8811817 前面我们使用Maven构建了Struts2项目,这里我们来试一下Hibe ...

  9. javascript设计模式--中介者模式(Mediator)

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Unity Texture 2D Compress

    测试了一下 unity 图片 对 apk 的影响. 上两种测试环境    1024 * 1024     带 alpha的话 默认压缩就是RBA 16bit就是2M     不带的话就是 etc 的话 ...