Pseudoprime numbers
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8529   Accepted: 3577

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是不是伪素数。伪素数条件:①p不是素数。② ap = a (mod p)。
#include <iostream>
using namespace std;
typedef unsigned long long ull;
ull a,p;
bool testPrime(ull x)
{
for(ull i=;i*i<=x;i++)
{
if(x%i==)
{
return false;
}
}
return true;
}
ull mpow(ull x,ull n,ull mod)
{
ull res=;
while(n>)
{
if(n&)
{
res=(res*x)%mod;
}
x=(x*x)%mod;
n>>=;
}
return res;
}
int main()
{
while(cin>>p>>a)
{
if(a==&&p==) break;
if(testPrime(p))
{
cout<<"no"<<endl;
}
else
{
if(mpow(a,p,p)==a%p)
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
}
}
return ;
}
 

最新文章

  1. 一次sql注入中转
  2. Ubuntu14.10安装Eclipse
  3. Git 常用命令行
  4. SQL Server Management Studio 已停止工作 异常错误
  5. 安装R语言扩展包vegan
  6. Java实验一
  7. android——创建camera应用(译)
  8. OO ALV 学习参考
  9. Office2007图标变成白框,但是还能使用问题解决办法
  10. 一个经典的js中关于块级作用域和声明提升的问题
  11. java类的初始化
  12. 学习笔记——抽象工厂模式Abstract Factory
  13. Android组件生命周期(一)
  14. Windows 10下Markdown不能显示预览
  15. android离线缓存技术
  16. js-ES6学习笔记-Class
  17. 深入理解Java虚拟机 #01# 自己编译JDK
  18. CSS 0.5px 细线边框的原理和实现方式
  19. 【刷题】BZOJ 1061 [Noi2008]志愿者招募
  20. Hash开散列 拉链法

热门文章

  1. MySQL-LRU_List Free_List Flush_List
  2. IntelliJ Idea 快捷键精选
  3. java深入探究12-框架整合
  4. Effective java第一章引言
  5. node操作mongdb的常用函数示例
  6. 智课雅思词汇---十五、前缀co-com-con-col-cor-是什么意思
  7. spring: 使用嵌入式数据源 EmbeddedDatabaseBuilder
  8. 免费获得 NTFS for Mac 12. Special Edition 激活码活动
  9. JProfiler远程监控 -转
  10. Struts2(2)