UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)
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 数论(快速幂取模 + 筛法求素数)的更多相关文章
- POJ3641-Pseudoprime numbers(快速幂取模)
题目大意 判断一个数是否是伪素数 题解 赤果果的快速幂取模.... 代码: #include<iostream> #include<cmath> using namespace ...
- 杭电 2817 A sequence of numbers【快速幂取模】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817 解题思路:arithmetic or geometric sequences 是等差数列和等比数 ...
- UVA 11609 - Teams 组合、快速幂取模
看题传送门 题目大意: 有n个人,选一个或者多个人参加比赛,其中一名当队长,如果参赛者相同,队长不同,也算一种方案.求一共有多少种方案. 思路: 排列组合问题. 先选队长有C(n , 1)种 然后从n ...
- The 2018 ACM-ICPC China JiangSu Provincial Programming Contest快速幂取模及求逆元
题目来源 The 2018 ACM-ICPC China JiangSu Provincial Programming Contest 35.4% 1000ms 65536K Persona5 Per ...
- POJ 1995 Raising Modulo Numbers 【快速幂取模】
题目链接:http://poj.org/problem?id=1995 解题思路:用整数快速幂算法算出每一个 Ai^Bi,然后依次相加取模即可. #include<stdio.h> lon ...
- 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) ...
- UVa 10006 - Carmichael Numbers
UVa 10006 - Carmichael Numbers An important topic nowadays in computer science is cryptography. Some ...
- 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...
- HDU1013,1163 ,2035九余数定理 快速幂取模
1.HDU1013求一个positive integer的digital root,即不停的求数位和,直到数位和为一位数即为数根. 一开始,以为integer嘛,指整型就行吧= =(too young ...
随机推荐
- SQL遍历字符串的方法
字符串穿越: 1.创建一个只存递增序列(1…n)的表——Temp,并将它与目标字符串所在的表Src进行笛卡尔运算.(Temp表的记录数要不小于遍历的目标字符串的长度) 2.过滤掉序列值大于串长的行. ...
- Integer做WeakHashMap的Key应注意的问题
WeakHashMap使用弱引用来作为Map的Key,利用虚拟机的垃圾回收机制能自动释放Map中没有被使用的条目.但是WeakHashMap释放条目是有条件的:首先条目的Key在系统中没有强引用指向: ...
- 如何将自定义RPM包加入YUM
1 前言 在很多时候进行编译了自己的RPM包,在搭建YUM的时候,希望将自定义的RPM加入到YUM源中,从而出现了下列方法. 2. 将RPM包加入YUM源 2.1 查看目前repodata位置 YUM ...
- Asp.net MVC 处理文件的上传下载
如果你仅仅只有Asp.net Web Forms背景转而学习Asp.net MVC的,我想你的第一个经历或许是那些曾经让你的编程变得愉悦无比的服务端控件都驾鹤西去了.FileUpload就是其中一个, ...
- 机器学习中的数学(3)-模型组合(Model Combining)之Boosting与Gradient Boosting
版权声明: 本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gm ...
- 《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇01:播放序列动画》
1.播放序列动画 系列动画播放概述 2D游戏中的动画系统,不同于3D游戏.3D游戏中,角色美术资源不仅包含角色模型的,还包括角色的贴图和动作等,模型本身自带角色的动作动画效果.2D游戏中,角色美术资源 ...
- 自定义实现InputFormat、OutputFormat、输出到多个文件目录中去、hadoop1.x api写单词计数的例子、运行时接收命令行参数,代码例子
一:自定义实现InputFormat *数据源来自于内存 *1.InputFormat是用于处理各种数据源的,下面是实现InputFormat,数据源是来自于内存. *1.1 在程序的job.setI ...
- java 拷贝功能
java 中的 拷贝分为浅拷贝 和 深拷贝 浅拷贝需要实现Cloneable接口,深拷贝需要实现Serializable接口. public class Square implements Clone ...
- F5 刷新功能
//1.引入单元 uses ShlObj; //2.执行命令 procedure TForm1.Button1Click(Sender: TObject); begin SHChangeNotify( ...
- C#正则表达式判断字符串是否是金钱
public static bool IsMoney(string input) { string pattern = @"^\-{0,1}[0-9]{0,}\.{0,1}[0-9]{1,} ...