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. 《剑指offer》面试题18 树的子结构 Java版

    (输入两棵二叉树A和B,判断B是不是A的子结构.补充下,根据书中的代码来看,子结构的定义并不包括叶子节点下的null,也就是说只要B的存在数字的结构存在于A中就行,那么如果B是null树,那么就不属于 ...

  2. Dijkstra经典算法注意点

    Dijkstra经典算法注意点 前言 迪杰斯特拉算法,经典模板如下: void dij(int s) { for(int i=1; i<=n; i++) dis[i]=road[s][i]; v ...

  3. 树莓派Pi账户密码简单重置

    由于经常忘记树莓派Pi账户的密码而导致无法正常的玩树莓派,本篇文章综合网上的教程,总结了两种快速重置树莓派Pi账户密码的方法,以下一切操作都需在树莓派本机上进行操作. 方法一: 打开终端,执行 sud ...

  4. c++知识点总结3

    http://akaedu.github.io/book/ week1 引用:相当于变量的别名.下面r和n就相当于同一回事 ; int &r=n; 引用做函数参数: void swap(int ...

  5. apache2.4 只允许合法域名访问网站 禁止使用ip、非法域名访问

    1.ip访问禁用ip访问 只能对应端口有效<VirtualHost *:80> ServerName xx.xx.xx.xx ServerAlias * <Location /> ...

  6. 移动端300ms延迟原理,穿透、遮罩层滑动导致下面滑动总结

    遮罩层滑动导致下面滑动 1,阻止弹层滑动,使用默认事件,使用这种方式弹层不能滑动 document.getElementById("model").addEventListener ...

  7. mybatis对java自定义注解的使用

    转自:https://www.cnblogs.com/sonofelice/p/4980161.html 最近在学习spring和ibatis框架. 以前在天猫实习时做过的一个小项目用到的mybati ...

  8. Anaconda 安装及Python 多版本间切换

    安装 Anaconda 安装anaconda 安装较为简单,这里参考官方文档:https://docs.continuum.io/anaconda/install/linux.html 在文件目录下执 ...

  9. lua视频教程三套高清新

    目录 1. 下载地址 2. 某网校 Lua 经典教程 3. lua脚本语言零基础开发教程19课全 4. LUA完整视频+Cocos2d-x项目实战 1. 下载地址 https://www.cnblog ...

  10. ArrayUtils工具类更加方便的操作数据

    不废话,上代码: package com.jxd; import org.apache.commons.lang3.ArrayUtils; public class TestArr { /** * A ...