Description

Given a big integer number, you are required to find out whether it's a prime number.

Input

The first line contains the number of test cases T (1 <= T <= 20 ), then the following T lines each contains an integer number N (2 <= N < 254).

Output

For each test case, if N is a prime number, output a line containing the word "Prime", otherwise, output a line containing the smallest prime factor of N.
 
题目大意:给一个数n,问是不是素数,若是输出Prime,若不是输出其最小的非1因子。
思路:http://www.2cto.com/kf/201310/249381.html
模板盗自:http://vfleaking.blog.163.com/blog/static/1748076342013231104455989/
 
代码(375MS):
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <iterator>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. typedef long long LL;
  10.  
  11. LL modplus(LL a, LL b, LL mod) {
  12. LL res = a + b;
  13. return res < mod ? res : res - mod;
  14. }
  15.  
  16. LL modminus(LL a, LL b, LL mod) {
  17. LL res = a - b;
  18. return res >= ? res : res + mod;
  19. }
  20.  
  21. LL mult(LL x, LL p, LL mod) {
  22. LL res = ;
  23. while(p) {
  24. if(p & ) res = modplus(res, x, mod);
  25. x = modplus(x, x, mod);
  26. p >>= ;
  27. }
  28. return res;
  29. }
  30.  
  31. LL power(LL x, LL p, LL mod) {
  32. LL res = ;
  33. while(p) {
  34. if(p & ) res = mult(res, x, mod);
  35. x = mult(x, x, mod);
  36. p >>= ;
  37. }
  38. return res;
  39. }
  40.  
  41. bool witness(LL n, LL p) {
  42. int t = __builtin_ctz(n - );
  43. LL x = power(p % n, (n - ) >> t, n), last;
  44. while(t--) {
  45. last = x, x = mult(x, x, n);
  46. if(x == && last != && last != n - ) return false;
  47. }
  48. return x == ;
  49. }
  50.  
  51. const int prime_n = ;
  52. int prime[prime_n] = {, , , , };
  53.  
  54. bool isPrime(LL n) {
  55. if(n == ) return false;
  56. if(find(prime, prime + prime_n, n) != prime + prime_n) return true;
  57. if(n % == ) return false;
  58. for(int i = ; i < prime_n; i++)
  59. if(!witness(n, prime[i])) return false;
  60. return true;
  61. }
  62.  
  63. LL getDivisor(LL n) {
  64. int c = ;
  65. while (true) {
  66. int i = , k = ;
  67. LL x1 = , x2 = ;
  68. while(true) {
  69. x1 = modplus(mult(x1, x1, n), c, n);
  70. LL d = __gcd(modminus(x1, x2, n), n);
  71. if(d != && d != n) return d;
  72. if(x1 == x2) break;
  73. i++;
  74. if(i == k) x2 = x1, k <<= ;
  75. }
  76. c++;
  77. }
  78. }
  79.  
  80. void getFactor(LL n, vector<LL> &ans) {
  81. if(isPrime(n)) return ans.push_back(n);
  82. LL d = getDivisor(n);
  83. getFactor(d, ans);
  84. getFactor(n / d, ans);
  85. }
  86.  
  87. int main() {
  88. int T; LL n;
  89. scanf("%d", &T);
  90. while(scanf("%I64d", &n) != EOF) {
  91. if(isPrime(n)) puts("Prime");
  92. else {
  93. vector<LL> ans;
  94. getFactor(n, ans);
  95. printf("%I64d\n", *min_element(ans.begin(), ans.end()));
  96. }
  97. }
  98. }

POJ 1811 Prime Test(Miller-Rabin & Pollard-rho素数测试)的更多相关文章

  1. Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test

    POJ 1811 Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 32534   Accepted: 8 ...

  2. POJ 1811 Prime Test (Pollard rho 大整数分解)

    题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...

  3. POJ1811- Prime Test(Miller–Rabin+Pollard's rho)

    题目大意 给你一个非常大的整数,判断它是不是素数,如果不是则输出它的最小的因子 题解 看了一整天<初等数论及其应用>相关部分,终于把Miller–Rabin和Pollard's rho这两 ...

  4. POJ2429 - GCD & LCM Inverse(Miller–Rabin+Pollard's rho)

    题目大意 给定两个数a,b的GCD和LCM,要求你求出a+b最小的a,b 题解 GCD(a,b)=G GCD(a/G,b/G)=1 LCM(a/G,b/G)=a/G*b/G=a*b/G^2=L/G 这 ...

  5. POJ 1811 Prime Test (Rabin-Miller强伪素数测试 和Pollard-rho 因数分解)

    题目链接 Description Given a big integer number, you are required to find out whether it's a prime numbe ...

  6. POJ 1811 Prime Test 素性测试 分解素因子

    题意: 给你一个数n(n <= 2^54),判断n是不是素数,如果是输出Prime,否则输出n最小的素因子 解题思路: 自然数素性测试可以看看Matrix67的  素数与素性测试 素因子分解利用 ...

  7. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  8. poj 1811 Prime Test 大数素数测试+大数因子分解

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 27129   Accepted: 6713 Case ...

  9. POJ 1811 Prime Test( Pollard-rho整数分解经典题 )

    链接:传送门 题意:输入 n ,判断 n 是否为素数,如果是合数输出 n 的最素因子 思路:Pollard-rho经典题 /************************************** ...

  10. Miller&&Pollard POJ 1811 Prime Test

    题目传送门 题意:素性测试和大整数分解, N (2 <= N < 254). 分析:没啥好讲的,套个模板,POJ上C++提交 收获:写完这题得到模板 代码: /************** ...

随机推荐

  1. phpcms v9 模板调用代码大全

    另:每个栏目会对应当前所选模型的三个模板文件:  这些模板文件所在位置:phpcms/templates/default/content/ 目录下,如果想修改模板文件,只需要到此目录下找到对应的模板文 ...

  2. docker jenkins

    https://segmentfault.com/a/1190000003732967

  3. HBase的属性

    一:基本属性 1.查看属性 2.解释属性 NAME:列簇名 BLOOMFILTER:布隆过滤器,用于对storefile的过滤 共有三种类型: ROW:行健过滤 ROWCOL:行列过滤 NONE:无 ...

  4. GetLogicalProcessorInformation(XP3才支持)和GetLogicalProcessorInformationEx(WIN7才支持)

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms683194(v=vs.85).aspx 例子执行结果如下: https://ms ...

  5. Bulk Insert & BCP执行效率对比(续)

    上回由于磁盘空间(约70G)不足,导致Bulk Insert和BCP导入中途失败:今次统一一些操作,以得到Bulk insert与BCP分别执行效率: 1. 15435390笔数据,21.7G csv ...

  6. 使用Dom的Range对象处理chrome和IE文本光标位置

    有这样一段js: var sel = obj.createTextRange(); sel.move('character', num); sel.collapse(); sel.select(); ...

  7. 使用多种客户端消费WCF RestFul服务(四)——Jquery篇

    Jquery篇 互联网开发中少不了各类前端开发框架,其中JQUERY就是最流行之一,本篇我们就采用JQUERY来消费WCF RestFul服务,其中用到JSON基础知识,如果有想了解的朋友,请访问:& ...

  8. 从高版本JDK换成低版本JDK报错Unsupported major.minor version 52.0

    ava.lang.UnsupportedClassVersionError: PR/Sort : Unsupported major.minor version 52.0这个错误是由于高版本的java ...

  9. 控制台打印出event对象时,对象里面的currentTarget为null

    但是MouseEvent对象展开时 附上老外的解释~~~: http://stackoverflow.com/questions/26496176/when-loggin-an-event-objec ...

  10. boost.compressed_pair源码剖析

    意义 当compressed_pair的某一个模板参数为一个空类的时候将对其进行“空基类优化”,这样可以使得compressed_pair占用的空间比std::pair的更小. 参考如下代码: #in ...