Pseudoprime numbers
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11336   Accepted: 4891

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

Source

 
 

题意:p不是素数,且a^p对p取模等于a,输出yes,其他的输出no。

题解:判断p是否是素数那部分直接蛮力求就好。

 #include <iostream>
using namespace std;
typedef long long ll;
bool is_prime(ll x)
{
int i;
if (x == ) return ;
if (x == ) return ;
for (i = ; i*i < x; i++)
{
if (x %i == )
return ;
}
return ;
}
int main()
{
ll a, p;
while (cin >> p>>a)//p=n
{
if (a == && p == ) break;
if (is_prime(p))
{
cout << "no" << endl;
continue;
}
ll ans = ;
ll k = p;
ll x = a;
while (p > )
{
if (p & ) ans = (ans * a)%k;
a = (a * a)%k;
p >>= ; }
if (ans%k == x) cout << "yes" << endl;
else cout << "no" << endl;
}
return ;
}

HDU 3641 Pseudoprime numbers(快速幂)的更多相关文章

  1. poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题

    Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...

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

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

  3. poj 3641 Pseudoprime numbers

    题目连接 http://poj.org/problem?id=3641 Pseudoprime numbers Description Fermat's theorem states that for ...

  4. HDU 1061 Rightmost Digit --- 快速幂取模

    HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...

  5. HDU.2640 Queuing (矩阵快速幂)

    HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...

  6. HDU 5667 构造矩阵快速幂

    HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...

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

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

  8. pojPseudoprime numbers (快速幂)

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

  9. POJ1995 Raising Modulo Numbers(快速幂)

    POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时0 ...

随机推荐

  1. UOJ #131 【NOI2015】 品酒大会

    题目链接:品酒大会 学了后缀自动机之后再来写这道题就轻松多了…… 首先,题面中的两杯酒\(r\)相似就是这两个后缀的最长公共前缀大于等于\(r\).把串翻转过来之后就变成了两个前缀的最长公共后缀……然 ...

  2. MAC OSX Docker安装教程2018

    1.首先打开Docker商店 Docker商店地址:https://store.docker.com/editions/community/docker-ce-desktop-mac 2.打开后界面如 ...

  3. Message: dlopen failed for module ‘x’: file not found

    这是未安装bochs-x的缘故 解决方案: sudo apt-get install bochs以后接着安装bochs-x. sudo apt-get install bochs-x 2.bx_dbg ...

  4. redis缓存穿透、缓存击穿、缓存雪崩

    缓存穿透 缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中时需要从数据库查询,查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到数据库去查询,造成缓存穿透. 解决办法: 预校验 在控 ...

  5. gif&png&jpg&webp

    几种图片格式的区别和联系 1.http://www.tuicool.com/articles/AbUvI3A

  6. thinkphp5中的配置如何使用

    thinkphp5中的配置如何使用 一.总结 一句话总结:先加载配置,然后读取配置即可 加载配置 读取配置 Config::load(APP_PATH.'fry_config.php');\\加载配置 ...

  7. TVD$XTAT在linux下安装使用详解

    扩展跟踪文件分析工具 (TVD$XTAT)是个命令行工具.和TKPROF一样,主要目的是把原始跟踪文件作为输入内容生成一个格式化的文件作为输出内容.输出文件可以是HTML或是文本文件.界面非常友好,推 ...

  8. 数据库使用B+树原理

    转载:http://zhuanlan.51cto.com/art/201808/582078.htm https://www.cnblogs.com/vincently/p/4526560.html( ...

  9. Reverse engineer powerdesigner link odbc

    Reverse engineer powerdesigner link odbc           Option Explicit ValidationMode = True Interactive ...

  10. 《转》快速导出SSRS之RDL文件

    select name,[path],cast(cast(content AS varbinary(max)) as xml) as RDLDef from dbo.[Catalog] where t ...