pojPseudoprime numbers (快速幂)
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-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不是素数且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 (快速幂)的更多相关文章
- POJ3641 Pseudoprime numbers(快速幂+素数判断)
POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...
- POJ1995 Raising Modulo Numbers(快速幂)
POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时0 ...
- POJ 1995:Raising Modulo Numbers 快速幂
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5532 Accepted: ...
- Uva 10006 Carmichael Numbers (快速幂)
题意:给你一个数,让你判断是否是非素数,同时a^n%n==a (其中 a 的范围为 2~n-1) 思路:先判断是不是非素数,然后利用快速幂对每个a进行判断 代码: #include <iostr ...
- ZOJ2150 Raising Modulo Numbers 快速幂
ZOJ2150 快速幂,但是用递归式的好像会栈溢出. #include<cstdio> #include<cstdlib> #include<iostream> # ...
- hdu 2817 A sequence of numbers(快速幂)
Problem Description Xinlv wrote some sequences on the paper a long time ago, they might be arithmeti ...
- POJ 1995 Raising Modulo Numbers (快速幂)
题意: 思路: 对于每个幂次方,将幂指数的二进制形式表示,从右到左移位,每次底数自乘,循环内每步取模. #include <cstdio> typedef long long LL; LL ...
- poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...
- POJ1995:Raising Modulo Numbers(快速幂取余)
题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> ...
随机推荐
- H-Updating a Dictionary (模拟)
In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, a ...
- Vert.x学习第一天
昨天看了下异步,然后就开始了Vert.x相关知识的学习. Vert.x是当下非常流行的一套全异步框架,其优势在于轻量级.高效.非常适合作为移动端后台或是企业应用. 当然对于第一天接触这个框架的人(没错 ...
- 【译】Redux 还是 Mobx,让我来解决你的困惑!
原文地址:Redux or MobX: An attempt to dissolve the Confusion 原文作者:rwieruch 我在去年大量的使用了 Redux,但我最近都在使用 Mob ...
- MIT 6.824学习笔记3 Go语言并发解析
之前看过一个go语言并发的介绍:https://www.cnblogs.com/pdev/p/10936485.html 但这个太简略啦.下面看点深入的 还记得https://www.cnblog ...
- Latex--入门系列三
Latex 专业的参考 tex对于论文写作或者其他的一些需要排版的写作来说,还是非常有意义的.我在网上看到这个对于Latex的入门介绍还是比较全面的,Arbitrary reference .所以将会 ...
- vue路由守卫配合权限,白名单
router.beforeEach(async(to, from, next) => { // 进度条开始 NProgress.start() // 确认用户是否已登录(获取它的token值,这 ...
- manjaro软件源报错 不停看到错误 "PackageName: signature from "User <email@archlinux.org>" is invalid" 的几种解决方法
对于报错情况, 格式大致如下: error: PackageName: signature from "User <email@archlinux.org>" is i ...
- vue 组件间传值方式
/* 父组件给子组件传值 1.父组件调用子组件的时候 绑定动态属性 <v-header :title="title"></v-header> 2.在子组件里 ...
- 一、ffmpeg理论
一.ffmpeg三件套 ffprobe看文件属性 ffmpeg 编转码 ffplay 播放器 C:\Users\Bruce\Desktop\Temp\ffmpeg\ffmpeg\ToolPack\ff ...
- MacOS系統Flutter打包apk
一.打包前需要做一些基本设置的确认 1.应用名 2.权限设置 3.applicationId:应用唯一标识符 4.versionCode:版本号 5.versionName:版本名称 6.APP应用图 ...