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

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.

题意:判断一个数是不是Carmichael数。

如果一个数不是素数,且对于任意的2< a <n满足方程 ,则称n是Carmichael数;否则n就不是Carmichael数。

这个题的关键是求快速幂。

#include<stdio.h>
#include<string.h>
#include<math.h>
#define LL long long
int a[66000];
void judge_prime() /*筛法求素数*/
{
int i,j,m=sqrt(65010+0.5);
memset(a,0,sizeof(a));
for(i=2;i<=m;i++)
{
if(!a[i]) /*素数为0*/
{
for(j=i*i;j<65010;j+=i)
a[j]=1; /*非素数为1*/
}
}
}
LL pow_mod(LL a,LL n,LL m) /*递归求快速幂*/
{
if(n==0) return 1;
LL x=pow_mod(a,n/2,m);
LL ans=x*x%m;
if(n%2==1) ans=ans*a%m;
return ans;
}
int main()
{
judge_prime();
LL i,n;
bool flag;
while(~scanf("%lld",&n)&&n)
{
if(!a[n])
{
printf("%lld is normal.\n",n);
continue;
}
flag=true;
for(i=2;i<n;i++)
{
if(pow_mod(i,n,n)!=i)
{
flag=false;
break;
}
}
if(flag)
printf("The number %lld is a Carmichael number.\n",n);
else
printf("%lld is normal.\n",n);
}
return 0;
}

UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)的更多相关文章

  1. POJ3641-Pseudoprime numbers(快速幂取模)

    题目大意 判断一个数是否是伪素数 题解 赤果果的快速幂取模.... 代码: #include<iostream> #include<cmath> using namespace ...

  2. 杭电 2817 A sequence of numbers【快速幂取模】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817 解题思路:arithmetic or geometric sequences 是等差数列和等比数 ...

  3. UVA 11609 - Teams 组合、快速幂取模

    看题传送门 题目大意: 有n个人,选一个或者多个人参加比赛,其中一名当队长,如果参赛者相同,队长不同,也算一种方案.求一共有多少种方案. 思路: 排列组合问题. 先选队长有C(n , 1)种 然后从n ...

  4. The 2018 ACM-ICPC China JiangSu Provincial Programming Contest快速幂取模及求逆元

    题目来源 The 2018 ACM-ICPC China JiangSu Provincial Programming Contest 35.4% 1000ms 65536K Persona5 Per ...

  5. POJ 1995 Raising Modulo Numbers 【快速幂取模】

    题目链接:http://poj.org/problem?id=1995 解题思路:用整数快速幂算法算出每一个 Ai^Bi,然后依次相加取模即可. #include<stdio.h> lon ...

  6. UVa 11582 (快速幂取模) Colossal Fibonacci Numbers!

    题意: 斐波那契数列f(0) = 0, f(1) = 1, f(n+2) = f(n+1) + f(n) (n ≥ 0) 输入a.b.n,求f(ab)%n 分析: 构造一个新数列F(i) = f(i) ...

  7. UVa 10006 - Carmichael Numbers

    UVa 10006 - Carmichael Numbers An important topic nowadays in computer science is cryptography. Some ...

  8. 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)

    先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...

  9. HDU1013,1163 ,2035九余数定理 快速幂取模

    1.HDU1013求一个positive integer的digital root,即不停的求数位和,直到数位和为一位数即为数根. 一开始,以为integer嘛,指整型就行吧= =(too young ...

随机推荐

  1. 【九度OJ】题目1078-二叉树遍历

    题目 这道题和后面的两道题,题目1201和题目1009,主要内容是对递归的实现,在逻辑上,递归是容易理解的,这种实现方式和我们思考的方式是相吻合的.但是在转换为计算机语言时,要明确告知计算机应该从哪里 ...

  2. 基于easyui的验证扩展

    基于easyui的验证扩展 ##前言 自己做项目也有好几年的时间了,一直没有时间整理自己的代码,趁春节比较闲,把自己以前的代码整理了一篇.这是基于easyui1.2.6的一些验证扩展,2012年就开始 ...

  3. APACHE如何里一个站点绑定多个域名?用ServerAlias

    APACHE2如何里一个站点绑定多个域名?用ServerAlias以前很笨,要使多个域名指向同一站点总是这样写: <VirtualHost *:80>ServerAdmin i@kuigg ...

  4. ffmpeg内置aac编码器正式发布

    https://www.ffmpeg.org/#aac_encoder_stable February 15th, 2016, FFmpeg 3.0 "Einstein" FFmp ...

  5. homework_01

    一. 程序的架构和思路: 这段求解最大子数组之和的程序使用的主要思想是贪心算法,即每一步求出的都是当前的最优解. 首先这道题要分两种情况来讨论: 1)如果当前的输入中所有的数均为负数时,那么最后的解就 ...

  6. 问题-Delphi2007跟踪变量时提示“E2171 Variable 'APolygon' inaccessible here due to optimization”

    问题现象:Delphi2007跟踪变量时提示“E2171 Variable 'APolygon' inaccessible here due to optimization” . 问题原因:可能是因为 ...

  7. ActiveX控件是什么?

    一.ActiveX的由来 ActiveX最初只不过是一个商标名称而已,它所涵盖的技术并不是各自孤立的,其中多数都与Internet和Web有一定的关联.更重要的是,ActiveX的整体技术是由Micr ...

  8. [iOS基础控件 - 6.12.1] QQ菜单管理 UITabBarController 控制器管理

    A.需求 1.类似QQ.微信顶部或者底部的窗口转换导航条 2.给每个页面添加相应内容   B.UITabBarController 1.基本概念: (1)内容高度 iOS7之前内容高度为:屏幕高度 - ...

  9. ural 1303 Minimal Coverage(贪心)

    链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 按照贪心的思想,每次找到覆盖要求区间左端点时,右端点最大的线段,然后把要求覆盖的区间 ...

  10. HDU1856More is better(并查集)

    最近发现以前的东西都忘得差不多了,在这里刷刷水题 并查集: int find_parent(int x) { return x = p[x] ? x : p[x] = find_parent(p[x] ...