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 ;
}
 

POJ3641(快速幂)的更多相关文章

  1. POJ3641 (快速幂) 判断a^p = a (mod p)是否成立

    Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...

  2. POJ3641 Pseudoprime numbers(快速幂+素数判断)

    POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...

  3. 【快速幂】POJ3641 - Pseudoprime numbers

    输入a和p.如果p不是素数,则若满足ap = a (mod p)输出yes,不满足或者p为素数输出no.最简单的快速幂,啥也不说了. #include<iostream> #include ...

  4. 《挑战程序设计竞赛》2.6 数学问题-快速幂运算 POJ1995

    POJ3641 此题应归类为素数. POJ1995 http://poj.org/problem?id=1995 题意 求(A1^B1+A2^B2+ - +AH^BH)mod M. 思路 标准快速幂运 ...

  5. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  6. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  7. hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)

    题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7).其中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3.                  ...

  8. Codeforces632E Thief in a Shop(NTT + 快速幂)

    题目 Source http://codeforces.com/contest/632/problem/E Description A thief made his way to a shop. As ...

  9. GDUFE-OJ 1203x的y次方的最后三位数 快速幂

    嘿嘿今天学了快速幂也~~ Problem Description: 求x的y次方的最后三位数 . Input: 一个两位数x和一个两位数y. Output: 输出x的y次方的后三位数. Sample ...

随机推荐

  1. iOS_数据存取(二)

    本节内容目录: 一.SQLite3 二.Core Data 一.SQlite3 SQLite3是⼀款开源的嵌入式关系型数据库,可移植性好.易使用.内存开销小SQLite3是⽆类型的,意味着你可以保存任 ...

  2. Qt编译器

    有两种,MSVC和MINGW Qt 中有两种方式编译,一种是MinGW ,另一种MSVC. MSVC是指微软的VC编译器: MingGW是指是Minimalist GNU on Windows的缩写. ...

  3. linux 虚拟机在线添加新磁盘

    在线添加磁盘,扩展LVM卷案例   一.添加硬盘,在线扫描出来 首先到虚拟机那里添加一块硬盘,注意必须是SCSI类型的硬盘. 扫描硬盘,不用重启操作系统的. echo "- - -" ...

  4. TypeScript手册1 - 基本类型和接口

    基本类型 像基本的JavaScript一样,TypeScript支持numbers, strings, structures, boolean等基本类型,而扩展的enum等类型,为编程提供了更多帮助. ...

  5. winform获取文件路径

    1.取得控制台应用程序的根目录方法     方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径     方法2.AppDomain.CurrentD ...

  6. 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0” 提供程序

    我在Web App程序里面用“Microsoft.Jet.OLEDB.4.0”来连接Excel文件,导入到数据库,在Windows 2003+ Office 2007 的环境下正常,但是在Window ...

  7. VC 写注册表

    BOOL Running() { HKEY hKey; LPCTSTR strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion ...

  8. Codeforces Beta Round #27 (Codeforces format, Div. 2) E. Number With The Given Amount Of Divisors 反素数

    E. Number With The Given Amount Of Divisors time limit per test 2 seconds memory limit per test 256 ...

  9. windows live writer 设置默认字体

      下载 Text Template 插件 设置默认字体添加模版. <p style="padding-bottom: 0px; line-height: 130%; margin-t ...

  10. WCF实现REST服务

    REST 表述性状态转移(Representational State Transfer,REST),不是一种标准,而是一种软件架构风格. 基于REST的服务与基于SOAP的服务相比,性能.效率和易用 ...