GCD&&素筛&&快速幂 --A - Pseudoprime numbers
Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). That is, if we raise a to the pth power and divide by p, the remainder is a. Some (but not very many) non-prime values of p, known as base-a pseudoprimes, have this property for some a. (And some, known as Carmichael Numbers, are base-a pseudoprimes for all a.)
Given 2 < p ≤ 1000000000 and 1 < a < p, determine whether or not p is a base-a pseudoprime.
Input
Input contains several test cases followed by a line containing "0 0". Each test case consists of a line containing p and a.
Output
For each test case, output "yes" if p is a base-a pseudoprime; otherwise output "no".
Sample Input
3 2
10 3
341 2
341 3
1105 2
1105 3
0 0
Sample Output
no
no
yes
no
yes
yes
本题用到快速幂,素数判定、二者结合;
题意:输入两个数p,a.先判断p是否为素数,如果是,输出no。否则,再判断a的p次方取余p是否为a,是则yes,反之
则no。
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
typedef long long ll;
int isprime(ll n)
{
if(n<=3) return n>1;
int k;
k=sqrt(n);
if(n%6!= 1 && n%6!=5)
return 0;
for(int i=5;i<=k;i+=6)
{
if(n%i==0 || n%(i+2)==0)
return 0;
}
return 1;
}
ll qpow(ll a, ll n,ll mod)//计算a^n % mod
{
ll re = 1;
while(n)
{
if(n & 1)//判断n的最后一位是否为1
re = (re * a) % mod;
n >>= 1;//舍去n的最后一位
a = (a * a) % mod;//将a平方
}
return re;
}
int main()
{
ll p,a;
while(cin>>p>>a&&a&&p)
{
if(isprime(p))
cout<<"no"<<endl;
else
{
if(a==qpow(a,p,p))
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
} }
return 0;
}
typedef
typedef long long ll;
快速幂模板
ll qpow(ll a, ll n,ll mod)//计算a^n % mod
{
ll re = 1;
while(n)
{
if(n & 1)//判断n的最后一位是否为1
re = (re * a) % mod;
n >>= 1;//舍去n的最后一位
a = (a * a) % mod;//将a平方
}
return re;
}
质数判定模板
int isprime(ll n)
{
if(n<=3) return n>1;
int k;
k=sqrt(n);
if(n%6!= 1 && n%6!=5)
return 0;
for(int i=5;i<=k;i+=6)
{
if(n%i==0 || n%(i+2)==0)
return 0;
}
return 1;
}
注意输入用cin,用scanf会wa
GCD&&素筛&&快速幂 --A - Pseudoprime numbers的更多相关文章
- 读入 并查集 gcd/exgcd 高精度 快速幂
ios_base::sync_with_stdio(); cin.tie(); ], nxt[MAXM << ], Head[MAXN], ed = ; inline void added ...
- Qbxt 模拟赛 Day4 T2 gcd(矩阵乘法快速幂)
/* 矩阵乘法+快速幂. 一开始迷之题意.. 这个gcd有个规律. a b b c=a*x+b(x为常数). 然后要使b+c最小的话. 那x就等于1咯. 那么问题转化为求 a b b a+b 就是斐波 ...
- hzau 1202 GCD(矩阵快速幂)
1202: GCD Time Limit: 1 Sec Memory Limit: 1280 MBSubmit: 201 Solved: 31[Submit][Status][Web Board] ...
- 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数
1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(l ...
- POJ3641 Pseudoprime numbers(快速幂+素数判断)
POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...
- POJ 3641 Pseudoprime numbers (数论+快速幂)
题目链接:POJ 3641 Description Fermat's theorem states that for any prime number p and for any integer a ...
- 【POJ - 3641】Pseudoprime numbers (快速幂)
Pseudoprime numbers Descriptions 费马定理指出,对于任意的素数 p 和任意的整数 a > 1,满足 ap = a (mod p) .也就是说,a的 p 次幂除以 ...
- HDU 3641 Pseudoprime numbers(快速幂)
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11336 Accepted: 4 ...
- 【快速幂】POJ3641 - Pseudoprime numbers
输入a和p.如果p不是素数,则若满足ap = a (mod p)输出yes,不满足或者p为素数输出no.最简单的快速幂,啥也不说了. #include<iostream> #include ...
随机推荐
- 测试的Python、 Java语言之争
现在测试行业如果不会开发语言的话是很难找到工作的,即使是一些功能测试的岗位也会要求代码语言作为技术储备,因为如果做自动化测试或者测试工具脚本开发或者接口测试等都离不开开发语言,那作为测试如果没有代码经 ...
- java设计模式简述
1.代理模式:有一个接口或者顶层类(可以是抽象的)A,一个实现类B,一个代理类C,代理类C之所以能够是代理类,是因为1.C也实现了A.2.C持有A的依赖,用来注入真实的实现B.3.C的实现方法中实际调 ...
- zookeeper源码 — 三、集群启动—leader、follower同步
zookeeper集群启动的时候,首先读取配置,接着开始选举,选举完成以后,每个server根据选举的结果设置自己的角色,角色设置完成后leader需要和所有的follower同步.上面一篇介绍了le ...
- 设顺序表中的数据元素递增有序,试着写一算法,将x插入到顺序表上的适当位置上,以保持该表的有序性。
原创,转载请注明出处.https://www.cnblogs.com/yangf428/p/11254370.html 天勤例题[2-1]: 设顺序表va中的数据元素递增有序.试写一算法,将x插入到顺 ...
- Embedded based learning
简单整理了一些嵌入式底层需要接触的相关概念. # CPU CU. Control Unit. send need-clac-data -> ALU clac -> get resul ...
- 【Linux】ssh命令行下多任务前后台切换
原文:https://my.oschina.net/huxuanhui/blog/13844 我们都知道,在 Windows 上面,我们要么让一个程序作为服务在后台一直运行,要么停止这个服务.而不能让 ...
- linux下添加动态链接库路径、动态库加载等方法
linux下添加动态链接库路径的方法 2017年01月20日 10:08:17 阅读数:5596 Linux共享库路径配置 Linux下找不到共享库文件的典型现象为明明已经安装某个软包(如libn ...
- ExtractIcon function Retrieves a handle to an icon from the specified executable file, DLL, or icon file.
https://msdn.microsoft.com/zh-cn/vstudio/ms648068(v=vs.90)
- TransitionDrawable
ayerDrawable的一个子类,TransitionDrawable只管理两层的Drawable!两层!两层! 并且提供了透明度变化的动画,可以控制一层Drawable过度到另一层Drawable ...
- python flask url参数
python flask url参数 常见 url 传参中都是 xxx?xxx=xxx 问题来了 flask中我没有找到 关于xx? 问号的使用方式 是不是flask就不支持这种方式 如果有 rout ...