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. Selenium自动化获取Http报文信息并判断当前API状态

    public int loadingFinishedCount(WebDriver driver){ LogEntries logs = driver.manage().logs().get(&quo ...

  2. 一般处理程序、Ajax多图片上传带进度条

    <!DOCTYPE html><html><head>    <meta charset="utf-8" />    <tit ...

  3. 关于python中的GIL

    什么是GIL锁? GIL是Global Interpreter Lock的缩写,GIL中文可以称为全局解释器锁.提及到GIL,我们要知道它是在实现Python解析器(CPython)时所引入的一个概念 ...

  4. ubuntu14/16 安装python3-opencv3_百度经验

    http://jingyan.baidu.com/article/e4511cf348dac52b845eafc8.html

  5. SecureCRT标签显示标题

  6. OO第一单元作业总结之初识面向对象

    第一个单元的三次作业均为求导,循序渐进的让我们掌握如何构造类和方法,让整个代码是面向对象的设计而不是面向过程的设计.如果第一次作业和第二次作业你只是简单的对过程着手架构类,到了第三次作业就会变得格外麻 ...

  7. 最近在学习Flask框架,那么就说下jinja2吧~~~

    jinja是组成Flask的模板引擎,先写一个demo吧 {% block body %} <ul> {% for user in users %} <li><a hre ...

  8. 动态导入Js文件

    var ScriptLoader = { worker: , isWait: false, readyQueue: [], callback: [], timer: null, wait: funct ...

  9. 微信小程序:首页设置方法(开发模式,使用模式)与其他相关设置

    小程序开发并不愉快,许多必建的文件不会自动生成,页面之间的跳转没有快捷键,开发者工具显示区域受限……如果谁有对应的解决办法求告知…… 开始的时候每次保存代码,页面都会刷洗重新渲染一次,而且自动跳回首页 ...

  10. QT自定义控件系列(二) --- Loading加载动画控件

    本系列主要使用Qt painter来实现一些基础控件.主要是对平时自行编写的一些自定义控件的总结. 为了简洁.低耦合,我们尽量不使用图片,qrc,ui等文件,而只使用c++的.h和.cpp文件. 由于 ...