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
 #include <iostream>
#include <cstdio>
#include <algorithm> using namespace std;
typedef long long ll;
ll quick_pow(ll a,ll b,ll mod) {
ll ans=;
while(b) {
if(b&)
ans=(ans*a)%mod;
a=(a*a)%mod;
b>>=;
}
return ans;
}
bool isprime(int x) {
for(int i=;i*i<=x;i++) {
if(x%i==) return false;
}
return true;
}
int main() {
// freopen("input.txt","r",stdin);
int a,p;
while(scanf("%d%d",&p,&a)!=EOF) {
if(a==&&p==) break;
if(isprime(p)) {
printf("no\n");
continue; }
ll z=quick_pow(a,p,p);
if(z==a) printf("yes\n");
else printf("no\n");
}
return ;
}

poj 3641 快速幂的更多相关文章

  1. POJ 3641 快速幂+素数

    http://poj.org/problem?id=3641 练手用,结果念题不清,以为是奇偶数WA了一发 #include<iostream> #include<cstdio> ...

  2. Pseudoprime numbers(POJ 3641 快速幂)

    #include <cstring> #include <cstdio> #include <iostream> #include <cmath> #i ...

  3. POJ 1995 快速幂模板

    http://poj.org/problem?id=1995 简单的快速幂问题 要注意num每次加过以后也要取余,否则会出问题 #include<iostream> #include< ...

  4. POJ 1845-Sumdiv(快速幂取模+整数唯一分解定理+约数和公式+同余模公式)

    Sumdiv Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Statu ...

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

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

  6. poj 1995 快速幂

    题意:给出A1,…,AH,B1,…,BH以及M,求(A1^B1+A2^B2+ … +AH^BH)mod M. 思路:快速幂 实例 3^11  11=2^0+2^1+2^3    => 3^1*3 ...

  7. POJ 3613 快速幂+Floyd变形(求限制k条路径的最短路)

    题意:       给你一个无向图,然后给了一个起点s和终点e,然后问从s到e的最短路是多少,中途有一个限制,那就是必须走k条边,路径可以反复走. 思路:       感觉很赞的一个题目,据说证明是什 ...

  8. POJ 1995 (快速幂) 求(A1B1+A2B2+ ... +AHBH)mod M

    Description People are different. Some secretly read magazines full of interesting girls' pictures, ...

  9. POJ 3641 Pseudoprime numbers (数论+快速幂)

    题目链接:POJ 3641 Description Fermat's theorem states that for any prime number p and for any integer a ...

随机推荐

  1. C++各种优化

    目录 1.快速结束程序 2.register 3.inline 4.位运算 5.少用或不用STL 6.快读快写 准备工作 超慢cin cout 输出 输入 神一般的快读快写 对比 7.小技巧 8.co ...

  2. maven项目的配置

    软件151  王帅 1.增加web.xml Maven项目最重要的配置文件是pom.xml,pom是“项目对象模型”的意思.现在pom.xml中有一个错误,提示缺少web.xml: 展开目录src—m ...

  3. Saku实力挖坑记!!(十八)

    Saiku实力挖坑记!!!!!!! 我可真真真的是个挖坑小能手呀!不知道你们有没有遇到过这个异常: Enclosure class mondrian.olap.MondrianDef not foun ...

  4. python简单爬虫 使用pandas解析表格,不规则表格

    url = http://www.hnu.edu.cn/xyxk/xkzy/zylb.htm 部分表格如图: 部分html代码: <table class="MsoNormalTabl ...

  5. python ---split()函数讲解

    python ---split()函数讲解 split中文翻译为分裂. 在python用于分割字符串使用. split()就是将一个字符串分裂成多个字符串组成的列表. split()可以传入参数,也可 ...

  6. HTML5 classList使用

       add:给元素添加一个指定的class var test = document.getElementById('test'); test.classList.add('yellow');//添加 ...

  7. 移动端设备中1px适配

    方式1:伪类+transform实现,主要用transform中的scale缩放,缩放默认中心点是以x,y轴的50%处,因此需要用transform-origin调整中心点 html代码: <d ...

  8. bottle模板中的替换

    line是模板中一行的内容,类似: {{x}}testinfo{{x+10}} x=10时,模板输出: 10testinfo20 x = 10 splits = re.split(r'\{\{(.*? ...

  9. JavaScript几种常见的继承方法

    1.call() 方法 call() 方法是与经典的对象冒充方法最相似的方法.它的第一个参数用作 this 的对象.其他参数都直接传递给函数自身 function Huster(name,idNum, ...

  10. re正则表达式匹配字符串中的数字

    re.match(r'.*-(\d*).html',url_1).group(1) \d+匹配1次或者多次数字,注意这里不要写成*,因为即便是小数,小数点之前也得有一个数字:\.?这个是匹配小数点的, ...