Description

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-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不是素数且pow(a,p)%p==a时输出yes,否则输出no
思路:只要会最基本的快速幂就可以做出这道题来。
快速幂:思路就是将pow(a,p)中的p换成多个2的倍数相加的形式,那么pow(a,p)=pow(a,c1)*pow(a,c2)*....*pow(a,cn),其中p=c1+c2+..+cn;利用位运算符
可以很容易实现这种操作。

int Fastpow(int a,int p){

int Fastpow(int a,int p){
long long int base=a;
long long int res=1;
while(p){
if(p&1) //若p的二进制表示形式最后一位数是1,则为真,否则为假
res*=base/*,res=res%mod*/;
base*=base;
/*base=base%mod;*/
p>>1; //将p的二进制表示形式后移一位,把刚处理过的p的最后一位去掉
}
return res;
}

快速幂的第二种表达方式就是利用递归

int Fastpow(int a,int p){
if(p==1) return a;
long long int temp=Fastpow(a,p/2)%p;
if(p%2==1) return temp*temp%p*a%p; //这里的第一个p若是省略,则会wrong answer
else return temp*temp%p;
}

最后附上AC代码:

#include<iostream>
#include<cstdio>
using namespace std;
long long int p;
int Fastpow(int a,int n){
long long int m=n,base=a;
long long int res=1;
while(m){
if(m&1){
res=res*base%p;
}
base=(base*base)%p;
m>>=1;
}
return res;
}
/*int Fastpow(int a,int n){
if(n==1) return a;
long long int temp=Fastpow(a,n/2)%p;
if(n%2==1) return temp*temp%p*a%p;
else return temp*temp%p;
}*/
int main(){
long long int a,c[100000];
while(~scanf("%lld%lld",&p,&a)){
if(p==0&&a==0) return 0;
int plug=0;
for(int i=2;i*i<=p;i++)
if(p%i==0) plug=1;
if(plug==0) {
printf("no\n");
continue;
}
//cout<<Fastpow(a,p)<<endl;
if(Fastpow(a,p)==a) printf("yes\n");
else printf("no\n");
}
}

这个是最基本的判断是否是素数,利用的是试除法,下面给出一个利用素数筛的快捷算法,比上面这一个耗时少不少:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=31700;
const long long int maxn1=1e9+10;
int Fastpow(int a,int p){
long long int m=p,base=a;
long long int res=1;
while(m){
if(m&1){
res=res*base%p;
}
base=(base*base)%p;
m>>=1;
}
return res;
}
int main(){
long long int p,a;
int c[maxn],prim[maxn],j=0;
memset(c,0,sizeof(c));
for(int i=2;i*i<maxn1;i++){
if(c[i]==0){
prim[j]=i,j++;
for(int k=i*i;k<maxn;k+=i)
c[k]=-1;
}
}
while(~scanf("%lld%lld",&p,&a)){
if(p==0&&a==0) return 0;
int plug=1;
for(int i=0;i<j&&prim[i]<p;i++)
if(p%prim[i]==0) plug=0;
if(plug==1) {
printf("no\n");
continue;
}
if(Fastpow(a,p)==a) printf("yes\n");
else printf("no\n");
}
}

pojPseudoprime numbers (快速幂)的更多相关文章

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

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

  2. POJ1995 Raising Modulo Numbers(快速幂)

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

  3. POJ 1995:Raising Modulo Numbers 快速幂

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

  4. Uva 10006 Carmichael Numbers (快速幂)

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

  5. ZOJ2150 Raising Modulo Numbers 快速幂

    ZOJ2150 快速幂,但是用递归式的好像会栈溢出. #include<cstdio> #include<cstdlib> #include<iostream> # ...

  6. hdu 2817 A sequence of numbers(快速幂)

    Problem Description Xinlv wrote some sequences on the paper a long time ago, they might be arithmeti ...

  7. POJ 1995 Raising Modulo Numbers (快速幂)

    题意: 思路: 对于每个幂次方,将幂指数的二进制形式表示,从右到左移位,每次底数自乘,循环内每步取模. #include <cstdio> typedef long long LL; LL ...

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

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

  9. POJ1995:Raising Modulo Numbers(快速幂取余)

    题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> ...

随机推荐

  1. 洛谷P2460 [SDOI2007]科比的比赛(题解)(贪心+搜索)

    科比的比赛(题解)(贪心+搜索) 标签:算法--贪心 阅读体验:https://zybuluo.com/Junlier/note/1301158 贪心+搜索 洛谷题目:P2460 [SDOI2007] ...

  2. python进阶之类的反射

    有应用场景的技术才是有灵魂的技术------>最近同时问我,在python中,给你一个函数或者类的字符串名称,你怎么得到该函数和类,以下结合源码记录我得到的方式: 1.给一个函数的字符串&quo ...

  3. HTML水平居中和垂直居中的实现方式

    父元素是块元素,根据子元素不同分为以下几种: 1.子元素是行内元素: a.水平居中:在父元素上设置text-align:center; b.垂直居中:在行内子元素上设置行高与父元素相同line-hei ...

  4. TensorFlow 安装及使用

    安装 (1)安装包安装:pip install tensorflow==1.14 -i https://pypi.douban.com/simple virtualenv -p /usr/bin/py ...

  5. CentOS7 systemctl 命令

    *启动.重启.停止.重载服务 # systemctl start httpd.service # systemctl restart httpd.service # systemctl stop ht ...

  6. ssh免口令密码登录及兼容性处理

    1). client ---> server 客户端发起对服务器的连接,登录服务器. 2). 须在客户端生成密钥对 注意: 公钥加密私钥解:私钥加密公钥解. 可以发布公钥,但私钥是不能出本机的. ...

  7. hadoop HA架构

    什么是Hadoop? http://hadoop.apache.org/ 解决问题:·海量数据的存储 (HDFS)·海量数据的分析 (MapReduce)·资源管理调度 (YARN) 集群规划:(这里 ...

  8. [python 学习] requests 库的使用

    1.get请求 # -*- coding: utf-8 -*- import requests URL_IP = "http://b.com/index.php" pyload = ...

  9. sqlmap 基本使用步骤(一)

    列出数据据信息:python sqlmap.py -u "http://ctf5.shiyanbar.com/web/index_3.php?id=1" --dbs 列出当前数据库 ...

  10. flask之视图函数从前端接收数据的方法

    一:从前端接收查询字符串  query-string 注意:get和post都可以在url后面添加查询字符串?a=1&b=2 测试工具:postman 1:get方式接收 视图函数 from ...