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. HDFS中高可用性HA的讲解

    HDFS Using QJM HA使用的是分布式的日志管理方式 一:概述 1.背景 如果namenode出现问题,整个HDFS集群将不能使用. 是不是可以有两个namenode呢 一个为对外服务-&g ...

  2. Decimal、 Float、 Double 使用

    一.Java 1.float型定义的数据末尾必须 有"f "或"F",为了和double区别.例float x=123.456f,  y=2e20f; publ ...

  3. frameset、frame、noframes和iframe的区别

    原网站地址:http://nmyun.blog.51cto.com/448726/155268 ■ 框架概念 :所谓框架便是网页画面分成几个框窗,同时取得多个 URL.只需要 <frameset ...

  4. Win7局域网文件共享方法

      右击桌面网络----属性----更改高级共享设置 (注释:查看当前网络 比如:家庭网络.公共网络 等!) "我这里为公共网络"   选择 公共网络---选择以下选项:启动网络发 ...

  5. Eclipse插件项目 引用其他类库的方法(jar)

    这两天搞了个Eclipse插件项目,用来监测ios.android设备和电脑的连接,安装apk/ipa到对应设备等等功能. 遇到了build path下的library引入编译正常,运行时报Class ...

  6. 1066 Bash游戏

    1066 Bash游戏 基准时间限制:1 秒 空间限制:131072 KB 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次最少拿1颗,最多拿K颗,拿到最后1颗石子的人获胜.假设A B都非常聪明, ...

  7. jQuery判断某个元素是否存在某个样式

    <input type="button" class="new"/> $("input[name=new]").hasClass ...

  8. Speed-BI 多事实表与表间计算的应用:销售目标达成分析 另一种实现方法

    在前一篇<Speed-BI多事实表与表间计算的应用(excel多Sheet关联分析):销售目标达成分析>http://www.powerbibbs.com/forum. ... 7583& ...

  9. lua metatable 和 _index 实验

    lua metatable 和 _index 中文博客解释: http://www.cnblogs.com/simonw/archive/2007/01/17/622032.html metatabl ...

  10. python MySQLdb中文乱码

    Python操作MySQL需要安装Python-MySQL可以从网上搜索一下,和一般的Python包一样安装 安装好之后,模块名字叫做MySQLdb ,在Window和Linux环境下都可以使用,试验 ...