-->Carmichael Numbers

 Descriptions:

题目很长,基本没用,大致题意如下

给定一个数n,n是合数且对于任意的1 < a < n都有a的n次方模n等于a,这个数就是Carmichael Number.

输出The number n is a Carmichael number.

n是素数

输出

n is normal.

Input

多组输入,第一行给一个n (2 < n < 65000) 。n = 0 表示输入结束并不需要处理

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.
题目链接
 
可以先判定n是否为合数,是就接着判断。
由于i的n次方的值可能很大,因此次题可以采用快速幂取余,由于数值范围可能很大,因此可采用long long 类型。
 
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 65000+10
using namespace std;
ll n;
ll mod;
int isprime[Maxn];//素数表
void eratos(int x)//求素数表,true为素数
{
for(int i=; i<=x; ++i)
isprime[i]=true;
isprime[]=isprime[]=false;
for(int i=; i<=x; ++i)
{
if(isprime[i])
{
int j=i+i;
while(j<=x)
{
isprime[j]=false;
j+=i;
}
}
}
}
ll qpow(ll a, ll n)//计算a^n % mod 快速幂
{
ll re = ;
while(n)
{
if(n & )//判断n的最后一位是否为1
re = (re * a) % mod;
n >>= ;//舍去n的最后一位
a = (a * a) % mod;//将a平方
}
return re % mod;
}
int main()
{
eratos(Maxn-);
while(cin>>n,n)
{
if(isprime[n])//是素数
cout << n << " is normal." << endl;
else//不是素数
{
int f=;
for(int i=; i<n; i++)//判断
{
mod=n;
ll t=qpow(i,n);
if(t!=i)
{
f=;
cout << n << " is normal." << endl;
break;
}
}
if(f)
cout << "The number " << n <<
" is a Carmichael number." << endl;
}
}
}

【UVA - 10006 】Carmichael Numbers (快速幂+素数筛法)的更多相关文章

  1. Uva 10006 Carmichael Numbers (快速幂)

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

  2. UVa 10006 - Carmichael Numbers

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

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

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

  4. POJ3641 Pseudoprime numbers(快速幂+素数判断)

    POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...

  5. poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题

    Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...

  6. pojPseudoprime numbers (快速幂)

    Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...

  7. POJ1995 Raising Modulo Numbers(快速幂)

    POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时0 ...

  8. POJ 1995:Raising Modulo Numbers 快速幂

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5532   Accepted: ...

  9. UVA 11609 Teams 组合数学+快速幂

    In a galaxy far far away there is an ancient game played among the planets. The specialty of the gam ...

随机推荐

  1. iBatisNet 中 iterate标签的使用

    <iterate>标签,顾名思义是用来遍历标签用的. 支持的属性如下: 属性 说明 是否必选 open 遍历后生成的这些sql,开始的第一个符号 可选 close 遍历后生成的这些sql, ...

  2. enum 枚举一般用法 dotnet

    public enum Demo { [Description("Moning描述")] Moning = , [Description("Afternoon描述&quo ...

  3. Windows Python虚拟环境配置(Distribute + pip + virtualenv + virtualenvwrapper-powershell)

    对于Python开发新手,很多人会迷茫那些各种名目的工具和概念,如Python2.7, Python3.3, Distribute, pip, virtualenv,Setuptools, easy_ ...

  4. C++的类型转换:static_cast、dynamic_cast、reinterpret_cast和const_cast(dynamic_cast还支持交叉转换,const_cast将一个类的const、volatile以及__unaligned属性去掉)

    在C++中,存在类型转换,通常意味着存在缺陷(并非绝对).所以,对于类型转换,有如下几个原则:(1)尽量避免类型转换,包括隐式的类型转换(2)如果需要类型转换,尽量使用显式的类型转换,在编译期间转换( ...

  5. Windows 7 频繁提示:计算机的内存不足

    最近由于同时打开的程序比较多,Windows 7 频繁提示:计算机的内存不足,如下图: 问题原因: 经过一番尝试,得出一个大概的结论:当虚拟内存空间的大小小于物理内存空间的大小时,一旦程序开的太多,物 ...

  6. 开源代码分析工具 good

    checkstyle - static code analysis tool for JavaPMD - A source code analyzer

  7. Java集合 LinkedList的原理及使用

    Java集合 LinkedList的原理及使用 LinkedList和ArrayList一样是集合List的实现类,虽然较之ArrayList,其使用场景并不多,但同样有用到的时候,那么接下来,我们来 ...

  8. Spark学习之路(二)—— Spark开发环境搭建

    一.安装Spark 1.1 下载并解压 官方下载地址:http://spark.apache.org/downloads.html ,选择Spark版本和对应的Hadoop版本后再下载: 解压安装包: ...

  9. Hive 学习之路(七)—— Hive 常用DML操作

    一.加载文件数据到表 1.1 语法 LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (p ...

  10. 【转+存】JVM指令集

    jvm指令集: 转载地址:https://www.cnblogs.com/yaoyinglong/p/4300447.html 一.未归类系列A 此系列暂未归类. 指令码    助记符         ...