博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Poj 3641 Pseudoprime numbers 快速幂取模
阅读量:3904 次
发布时间:2019-05-23

本文共 1603 字,大约阅读时间需要 5 分钟。

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 210 3341 2341 31105 21105 30 0

Sample Output

nonoyesnoyesyes

很基础的一道快速幂取模的题目....

只需要判断a^p%p与a%p两个结果是否相等和判断p是否为素数即可...

代码如下:

#include 
#include
#include
#include
#include
using namespace std;typedef long long ll;ll p,a;ll Fast (ll a,ll b){ ll sum=1,mod=b; while (b>0) { if(b&1) { sum=sum*a%mod; } b>>=1; a=a*a%mod; } return sum;}bool Is_pri (ll x){ if(x==1) return false; for (int i=2;i<=sqrt(x);i++) if(x%i==0) return false; return true;}int main(){ while (scanf("%lld%lld",&p,&a)!=EOF&&(p||a)) { ll mod1=a%p; ll mod2=Fast(a,p); if(mod2==mod1) { if(Is_pri(p)) printf("no\n"); else printf("yes\n"); } else printf("no\n"); } return 0;}

 

转载地址:http://paaen.baihongyu.com/

你可能感兴趣的文章
graph slam tutorial :从推导到应用2
查看>>
graph slam tutorial :从推导到应用3
查看>>
texstudio语法检查
查看>>
ComboBox用AddString添加字符显示乱码
查看>>
基于CSerialPort修改类的串口调试助手源代码(支持中文、自动保存等)
查看>>
MFC下边自动寻找串口
查看>>
PID调节经验
查看>>
机器学习经典书籍
查看>>
Latex排版全解
查看>>
2D-slam 激光slam: 开源代码的比较HectorSLAM Gmapping KartoSLAM CoreSLAM LagoSLAM
查看>>
北航王田苗教授:国内外机器人发展热点与趋势(精华版)
查看>>
windows常用软件收集
查看>>
Markdown语法注意借鉴
查看>>
Java 容器Collection(5)
查看>>
Java IO操作(6)
查看>>
Java数组(3)
查看>>
Java线程(7)
查看>>
Java GUI基础(9)
查看>>
Java网络基础(8)
查看>>
Java_正则表达式
查看>>