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

  1. 3 2
  2. 10 3
  3. 341 2
  4. 341 3
  5. 1105 2
  6. 1105 3
  7. 0 0

Sample Output

  1. no
  2. no
  3. yes
  4. no
  5. yes
  6. yes

Author

Gordon V. Cormack

Source

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


思路

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

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef __int64 ll;
  4. bool isprime(ll x)
  5. {
  6. for(int i=2;i<sqrt(x);i++)
  7. if(x%i==0)
  8. return false;
  9. return true;
  10. }//判断是否为质数
  11. ll quickpower(ll a,ll b,ll c)
  12. {
  13. ll ans =1;
  14. while(b)
  15. {
  16. if(b&1)
  17. ans = (ans*a) % c;
  18. a = (a*a) % c;
  19. b >>= 1;
  20. }
  21. return ans;
  22. }//返回a^b%c的结果
  23. int main()
  24. {
  25. int a,p;
  26. while(cin>>p>>a)
  27. {
  28. if(p==0 && a==0) break;
  29. if(isprime(p))
  30. cout << "no" << endl;
  31. else
  32. {
  33. int ans_power = quickpower(a,p,p);
  34. if(ans_power==a)
  35. cout << "yes" << endl;
  36. else
  37. cout << "no" << endl;
  38. }
  39. }
  40. return 0;
  41. }

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. 环同态p64推论

    1.为什么属于f(x)∈f(I),那么 2.为什么x属于ker,那么f(x)属于f(I)?

  2. StackWalk64

    #include <Windows.h>   #define  PULONG_PTR ULONG** #define  PULONG ULONG* #define  ULONG_PTR U ...

  3. webdriver原理、协议

    1.webdriver client的原理是什么? 当测试脚本启动firefox的时候,selenium-webdriver 会首先在新线程中启动firefox浏览器.如果测试脚本指定了firefox ...

  4. HashMap深度解析(转载)

    原文地址:http://blog.csdn.net/ghsau/article/details/16890151 实现原理:用一个数组来存储元素,但是这个数组存储的不是基本数据类型.HashMap实现 ...

  5. [转帖]cmd批处理常用符号详解

    cmd批处理常用符号详解 https://www.jb51.net/article/32866.htm 很多符号 还是不清楚的.. 批处理能够极大的提高 工作效率 需要加强深入学习.   1.@一般在 ...

  6. Docker安装部署redis

    借鉴博客:https://my.oschina.net/u/3489495/blog/1825335 待续... >>>>>>>>>docker安 ...

  7. css3 text-shadow字体阴影讲解

    text-shadow:为字体添加阴影, 可以通过对text-shadow属性设置相关的属性值,来实现现一些需要的字体阴影效果,减少了图片的使用. 基础说明:    text-shadow: X轴  ...

  8. Spring Boot+Jsp启动异常

    No Java compiler available for configuration options compilerClassName 加入maven配置 <dependency> ...

  9. 使用Elasticsearch 出现的拒绝连接

    pom 文件 spring: elasticsearch: jest: uris: http://192.168.124.142:9201 # data: # elasticsearch: # clu ...

  10. springmvc中model可以封装的数据类型

    查看源码可以知道,model中可以存放的数据类型 Model addAttribute(String var1, @Nullable Object var2); Model addAttribute( ...