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. 对羊车门的思考/python/

    作业完成人: 学号:20181603048,温晨阳 学号:20181603024,刘鑫垚 题目描述:有3扇关闭的门,一扇门后面停着汽车,其余门后是山羊,只有主持人知道每扇门后面是什么.参赛者可以选择一 ...

  2. Java之冒泡排序(升序)

    Java之冒泡排序 * 编辑者:鸿灬嗳 * 实现功能: 使用冒泡排序对数组:{25,24,12,76,101,96,28} 排序. */ package test05; public class Bu ...

  3. VM虚拟机拍摄快照时出错或者克隆失败解决办法

    在换了固态硬盘后,下载好VM,装虚拟机,结果克隆虚拟机和拍摄快照时出问题了. 拍摄快照时出错或者隆失败出现参数错误如图: 所提供的参数中有一个无效参数 解决办法 出现这个问题一般是在机械硬盘的电脑上面 ...

  4. Google 最新推出数据集搜索

    继Google在2004年11月推出Google的重量产品Google scholar后,Google在最近推出的另一个重量级产品 Google Dataset Search.众所周知,互联网上的信息 ...

  5. 配置jQuery环境

    获取最新版jQuery 一.jq库类型说明 jQuery.js(开发版):完整无压缩,主要用于学习.开发和测试 jQuery.min.js(生产版):主要用于产品开发和项目 二.在页面引入 <s ...

  6. font-family 中文字体列表

    华文细黑:STHeiti Light [STXihei] 华文黑体:STHeiti 华文楷体:STKaiti 华文宋体:STSong 华文仿宋:STFangsong 儷黑 Pro:LiHei Pro ...

  7. 获取百度地图POI数据二(准备搜索关键词)

    上篇讲到  想要获取尽可能多的POI数据 需要准备尽可能多的搜索关键字   那么这些关键字如何得来呢?   本人使用的方法是通过一些网站来获取这些关键词   http://poi.mapbar.com ...

  8. Windows上SQLPLUS的设置

    sqlplus启动的时候会调用login.sql,首先在当前路径下查找login.sql,如果没有找到,则在SQLPATH中查找该文件 另外sqlplus执行命令的时候也会首先在当前目录查找脚本,如果 ...

  9. LINQ Expresstion Tree 表达式树

    Expression trees represent code in a tree-like data structure, where each node is an expression, for ...

  10. ConcurrentHashMap源码

    ---------------------------------------------------------------------------------------------------- ...