Problem Description

Fermat's theorem states that for any prime number p and for any integer a > 1, a^p == 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-a pseudoprimes, have this property for some a. (And some, known as Carmichael Numbers, are base-a pseudoprimes for all a.)

Given 2 < p ≤ 1,000,000,000 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

Author

Gordon V. Cormack

Source

2008-1杭电公开赛(非原创)


思路

就是判断\(a^p\%p==a\),计算\(a^p\)可以用快速幂的方法,快速幂本质也是二分不断加速

代码

#include<bits/stdc++.h>
using namespace std;
typedef __int64 ll; bool isprime(ll x)
{
for(int i=2;i<sqrt(x);i++)
if(x%i==0)
return false;
return true;
}//判断是否为质数 ll quickpower(ll a,ll b,ll c)
{
ll ans =1;
while(b)
{
if(b&1)
ans = (ans*a) % c;
a = (a*a) % c;
b >>= 1;
}
return ans;
}//返回a^b%c的结果
int main()
{
int a,p;
while(cin>>p>>a)
{
if(p==0 && a==0) break;
if(isprime(p))
cout << "no" << endl;
else
{
int ans_power = quickpower(a,p,p);
if(ans_power==a)
cout << "yes" << endl;
else
cout << "no" << endl;
}
}
return 0;
}

Hdoj 1905.Pseudoprime numbers 题解的更多相关文章

  1. Hdoj 1058.Humble Numbers 题解

    Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...

  2. hdu 1905 Pseudoprime numbers

    #include<stdio.h> #include<math.h> #define ll long long ll mod; bool Judge(int x) { ;i&l ...

  3. HDU 3641 Pseudoprime numbers(快速幂)

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

  4. 找规律/数位DP HDOJ 4722 Good Numbers

    题目传送门 /* 找规律/数位DP:我做的时候差一点做出来了,只是不知道最后的 is_one () http://www.cnblogs.com/crazyapple/p/3315436.html 数 ...

  5. poj 3641 Pseudoprime numbers

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

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

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

  7. poj Pseudoprime numbers 3641

    Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10903   Accepted: 4 ...

  8. 【POJ - 3641】Pseudoprime numbers (快速幂)

    Pseudoprime numbers Descriptions 费马定理指出,对于任意的素数 p 和任意的整数 a > 1,满足 ap = a (mod p) .也就是说,a的 p 次幂除以  ...

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

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

随机推荐

  1. mysql有多大内存?能存多少数据?

    Mysql: MySQL 3.22 限制的表大小为4GB. MyISAM 存储引擎: 最大表尺寸增加到了65536TB(2567 – 1字节).由于允许的表尺寸更大,MySQL数据库的最大有效表尺寸通 ...

  2. WinRAR从入门到高级的操作技巧集合

    一.基础技巧 1.批量建立文件夹 如果在工作中,经常要建立很多相同文件夹结构(如在备份数据时).那可以把这个繁琐的工作让WinRAR完成:先在“资源管理器”中把多个文件夹结构建好(包括其下的子文件夹) ...

  3. js 翻牌活动效果

    直接上代码 html: <div class="index_main"> <ul class="index_card"> <li ...

  4. php 删除一维数组中某一个值元素的操作方法

    1. 自己写for循环 从array里去掉$tmp这个元素的值 ? 1 2 3 4 5 6 7 8 9 10 <?php $tmp = '324'; $arr = array( '0' => ...

  5. Docker操作删除所有容器镜像

    借鉴博客:https://www.cnblogs.com/yanyouqiang/p/8301856.html https://blog.csdn.net/wy_97/article/details/ ...

  6. 谈谈B-树和B+树及其应用

    待更!!! B-树和B+树的应用:数据搜索和数据库索引 B+/-Tree原理及mysql的索引分析 从B树.B+树.B*树谈到R 树 B树.B-树.B+树.B*树

  7. Django Rest framework 框架之认证使用和源码执行流程

    用这个框架需要先安装: pip3 install djangorestframework 如果写了一个CBV的东西,继承了View. # 继承Django里面View class APIView(Vi ...

  8. windos安装maven

    1.下载好maven压缩包,并解压到相应位置,本次安装在D: 2.配置环境变量 MAVEN_HOME=D:\apache-maven-3.0.5 path=%MAVEN_HOME% 3.生成maven ...

  9. django restframework PrimaryKeyRelatedField筛选的困惑

    一.在开发某运动app时,遇见以下情况 1.部分表内容如下: class Sports(models.Model): ''' 运动表 ''' school = models.ForeignKey(Sc ...

  10. SQL Server2012中如何通过bak文件还原SQL Server2012数据库

    1 登陆完数据库后,不要新建数据库,直接点击“数据库”然后右击"还原数据库". 2 在"源"选项中选择"设备". 3 选择相应的bak文件并 ...