Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13406   Accepted: 5792

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-a 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

  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
  7.  
  8. 题解:

/*通过 p的值判断,若p为素数,就断定不是伪素数,若p不是素数,则判断式子(a^n)%p==a;若相等,
则输出yes,否则输出no;由于数据较大,故容易超时,费马小定理;*/

难受的是sqrt函数返回的是double型,在类型转换的时候poj一直提示compile error,浪费了我好多时间

这里再说一下sqrt()函数:

sqrt()函数,里面的形参是double型的,所以调用的时候,要强制转换成double型。

sqrt()函数都最后返回值是double型,而n是int型,所以要强制转换n=(int)sqrt((double)x);

  1. #include<iostream>
  2. #include<string.h>
  3. #include<math.h>
  4. #define max 0x3f3f3f3f
  5. #define ll long long
  6. #define mod 1000000007
  7. using namespace std;
  8. ll vis[];
  9. ll cnt = ;
  10. int isprime(ll p)
  11. {
  12. int x=(double)sqrt(p)+0.5;
  13. for(ll i=;i<=x;i++)
  14. if(p%i==)
  15. return ;
  16. return ;
  17. }
  18.  
  19. ll f(ll a, ll b, ll n) //定义函数,求a的b次方对n取模
  20. {
  21. ll t, y;
  22. t = ;
  23. y = a;
  24. while (b != )
  25. {
  26. if ((b & ) == )
  27. t = t * y%n;
  28. y = y * y%n;
  29. b = b >> ;
  30. }
  31. return t;
  32. }
  33. int main()
  34. {
  35. ll a, p;
  36. while (cin >> p >> a)
  37. {
  38. if (a == && p == )
  39. break;
  40. else
  41. {
  42. if (isprime(p))
  43. cout << "no" << endl;
  44. else if (a == f(a, p, p))
  45. cout << "yes" << endl;
  46. else
  47. cout << "no" << endl;
  48. }
  49. }
  50. return ;
  51. }

Pseudoprime numbers---费马小定理的更多相关文章

  1. CodeForces 300C Beautiful Numbers(乘法逆元/费马小定理+组合数公式+高速幂)

    C. Beautiful Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. hdu6440 Dream 2018CCPC网络赛C 费马小定理+构造

    题目传送门 题目大意: 给定一个素数p,让你重载加法运算和乘法运算,使(m+n)p=mp+np,并且 存在一个小于p的q,使集合{qk|0<k<p,k∈Z} 等于集合{k|0<k&l ...

  3. HDU——5667Sequence(矩阵快速幂+费马小定理应用)

    Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total S ...

  4. hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)

    题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

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

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

  6. nyoj1000_快速幂_费马小定理

    又见斐波那契数列 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 斐波那契数列大家应该很熟悉了吧.下面给大家引入一种新的斐波那契数列:M斐波那契数列. M斐波那契数列 ...

  7. poj 3734 Blocks 快速幂+费马小定理+组合数学

    题目链接 题意:有一排砖,可以染红蓝绿黄四种不同的颜色,要求红和绿两种颜色砖的个数都是偶数,问一共有多少种方案,结果对10007取余. 题解:刚看这道题第一感觉是组合数学,正向推了一会还没等推出来队友 ...

  8. 数论初步(费马小定理) - Happy 2004

    Description Consider a positive integer X,and let S be the sum of all positive integer divisors of 2 ...

  9. 【BZOJ1951】【SDOI2010】古代猪文 Lucas定理、中国剩余定理、exgcd、费马小定理

    Description “在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心……” ——选自猪王国民歌 很久很久以前,在山的那边 ...

  10. 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum

    Sum Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...

随机推荐

  1. Entity Framework 6.0 Tutorials(11):Download Sample Project

    Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...

  2. Zynq 在Ubuntu上搭建编译环境

    http://bbs.elecfans.com/jishu_487981_1_1.html 以下操作均在root用户下完成1,下载交叉编译器在ubuntu里下载arm-2010.09-62-arm-x ...

  3. 测试中常用到的linux命令

    1. man         格式化以及列出命令的(在线)操作手册.         使用方法                 man [ -options ] name       man man ...

  4. Ubuntu安装开发版pidgin支持lwqq插件

    sudo add-apt-repository ppa:lainme/pidgin-lwqq  """添加pidgin-lwqq源""" s ...

  5. MongoDB整理笔记の索引

    MongoDB 提供了多样性的索引支持,索引信息被保存在system.indexes 中,且默认总是为_id创建索引,它的索引使用基本和MySQL 等关系型数据库一样.其实可以这样说说,索引是凌驾于数 ...

  6. C#生成静态文件

    一般生成文件都是通过读取模板文件,然后替换标签. 这些古老的方法使用起来不但麻烦而且效率还不怎么样. 这里给添加介绍一个方法. 如果你用过asp.net.mvc (Razor),你就应该明白 chtm ...

  7. arp欺骗进行流量截获-2

    上一篇讲了原理,那么这一篇主要讲如何实现.基本上也就是实现上面的两个步骤,这里基于gopacket实现,我会带着大家一步步详细把每个步骤都讲到. ARP 欺骗 首先就是伪造ARP请求,让A和B把数据包 ...

  8. .Net Core .Net Core的学习

    .Net Core 学习 一.什么是.net core? 百度百科: https://baike.baidu.com/item/.net%20core/20130686?fr=aladdin 个人总结 ...

  9. 「JSOI2009」球队收益 / 球队预算

    题目链接 戳我 \(Solution\) 我们发现这道题目并不好做,因为要考虑两个因素对答案的影响.于是我们假设接下来的\(m\)场比赛双方都输了.这要我们就只要考虑赢一场对答案的影响了,那每赢一场输 ...

  10. 第一篇 Python的数据类型

    Python的标准数据类型有五种: (1)字符串 (2)数字(包括整数,浮点数,布尔,复数) (3)列表(list) (4)元组(tuple) (5)字典(dict) 注:使用type函数可以查看对象 ...