先贴一份代码在这。

最近几天实在是太忙了没时间更新了。

代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <cctype>
  7. #include <algorithm>
  8. #include <ctime>
  9. #define rin(i,a,b) for(int i=(a);i<=(b);i++)
  10. #define rec(i,a,b) for(int i=(a);i>=(b);i--)
  11. #define trav(i,a) for(int i=head[(a)];i;i=e[i].nxt)
  12. typedef long long LL;
  13. using std::cin;
  14. using std::cout;
  15. using std::endl;
  16. inline LL read(){
  17. LL x=0,f=1;char ch=getchar();
  18. while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
  19. while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
  20. return x*f;
  21. }
  22. LL n,ans,mi[6]={2,3,5,7,11,13};
  23. inline LL gcd(LL x,LL y){
  24. if(!x||!y) return x+y;
  25. while(y){
  26. std::swap(x,y);
  27. y%=x;
  28. }
  29. return x;
  30. }
  31. inline LL qpow(LL x,LL y,LL mod){
  32. LL ret=1,tt=x%mod;
  33. while(y){
  34. if(y&1) ret=(__int128)ret*tt%mod;
  35. tt=(__int128)tt*tt%mod;
  36. y>>=1;
  37. }
  38. return ret;
  39. }
  40. bool miller_rabin(LL num){
  41. if(num<2) return 0;
  42. if(num==2||num==3||num==5||num==7||num==11||num==13) return 1;
  43. if(num%2==0||num%3==0||num%5==0||num%7==0||num%11==0||num%13==0) return 0;
  44. LL temp=num-1;int cnt2=0;
  45. while(!(temp&1)) temp>>=1,cnt2++;
  46. rin(i,0,5){
  47. LL now=qpow(mi[i],temp,num);
  48. if(now==1||now==num-1) continue;
  49. rin(j,1,cnt2){
  50. now=(__int128)now*now%num;
  51. if(now==num-1) break;
  52. if(j==cnt2) return 0;
  53. }
  54. }
  55. return 1;
  56. }
  57. LL pollard_rho(LL num){
  58. LL fixx=rand()%(num-1)+1,siz=2,x=fixx,fac=1;
  59. LL b=rand()%(num-1)+1;
  60. while(fac==1){
  61. rin(i,1,siz){
  62. x=((__int128)x*x+b)%num;
  63. fac=gcd(std::abs(x-fixx),num);
  64. if(fac>1) return fac;
  65. }
  66. siz<<=1;
  67. fixx=x;
  68. }
  69. }
  70. void getdivisor(LL num){
  71. if(num==1||num<=ans) return;
  72. if(miller_rabin(num)){
  73. ans=std::max(ans,num);
  74. return;
  75. }
  76. LL x=num;
  77. while(x==num) x=pollard_rho(x);
  78. while(num%x==0) num/=x;
  79. if(num<x) std::swap(num,x);
  80. getdivisor(num);
  81. getdivisor(x);
  82. }
  83. int main(){
  84. int T=read();
  85. while(T--){
  86. n=read();
  87. ans=1;
  88. getdivisor(n);
  89. if(ans==n) printf("Prime\n");
  90. else printf("%lld\n",ans);
  91. }
  92. return 0;
  93. }

Pollard's Rho算法简单总结的更多相关文章

  1. Miller-Rabin素性测试|Pollard's Rho算法

    Miller-Rabin 素性测试 Miller-Rabin 素数测试 一本通上的M-R不透彻啊~ Miller-Rabin是利用随机化算法判断一个数是合数还是素数. 首先,如果一个数N是素数,那么他 ...

  2. 【快速因数分解】Pollard's Rho 算法

    Pollard-Rho 是一个很神奇的算法,用于在 $O(n^{\frac{1}4}) $的期望时间复杂度内计算合数 n 的某个非平凡因子(除了1和它本身以外能整除它的数).事书上给出的复杂度是 \( ...

  3. Pollard's rho algorithm和涉及到的两个循环检测算法

    0. 简单介绍 Pollard的\(\rho\)算法是John Pollard在1975年发明的,用于分解质因数[1].假定被分解的数为N,N的最小的质因数为\(p(p\ne N)\),那么该算法可以 ...

  4. 数学基础IV 欧拉函数 Miller Rabin Pollard's rho 欧拉定理 行列式

    找了一些曾经没提到的算法.这应该是数学基础系最后一篇. 曾经的文章: 数学基础I 莫比乌斯反演I 莫比乌斯反演II 数学基础II 生成函数 数学基础III 博弈论 容斥原理(hidden) 线性基(h ...

  5. Pollard Rho 算法简介

    \(\text{update 2019.8.18}\) 由于本人将大部分精力花在了cnblogs上,而不是洛谷博客,评论区提出的一些问题直到今天才解决. 下面给出的Pollard Rho函数已给出散点 ...

  6. Pollard Rho算法浅谈

    Pollard Rho介绍 Pollard Rho算法是Pollard[1]在1975年[2]发明的一种将大整数因数分解的算法 其中Pollard来源于发明者Pollard的姓,Rho则来自内部伪随机 ...

  7. Pollard rho算法+Miller Rabin算法 BZOJ 3668 Rabin-Miller算法

    BZOJ 3667: Rabin-Miller算法 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 1044  Solved: 322[Submit][ ...

  8. 初学Pollard Rho算法

    前言 \(Pollard\ Rho\)是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:\(MillerRabin\)素数测试(关于\(MillerRabin\),可以参考这篇博客:初学Mi ...

  9. Miller Rabin素数检测与Pollard Rho算法

    一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是 ...

随机推荐

  1. [Python3] 026 常用模块 calendar

    目录 calendar 1. calendar.calendar(year, w, l, c, m) 2. calendar.prcal(year, w, l, c, m) 3. calendar.m ...

  2. http请求跨域问题分析

    http请求跨域问题分析 个人认为可能涉及很多http的知识,本人才疏学浅不敢妄自揣测,只是做个笔记为了以后对比理解,从原生fetch说起吧,前提假设有个后端服务,我用express来模拟,如下: v ...

  3. 手撕ES6--Promise

    手撕ES6--Promise:https://www.jianshu.com/p/0925eae38d2c 手写一个Promise,附源码分析:https://blog.csdn.net/weixin ...

  4. MySQL的日志系统

    一.日志类型 逻辑日志:存储了逻辑SQL修改语句 物理日志:存储了数据被修改的值 二.binlog 1.定义 binlog 是 MySQL 的逻辑日志,也叫二进制日志.归档日志,由 MySQL Ser ...

  5. “laravel.log” could not be opened: failed to open stream

    百度了一下,说是要赋权限,按照操作赋了权限也还是报错,其实只要执行第一个就好,但为了保险起见,我都执行了,还是不行 chmod -R /storage chmod -R /storage/logs c ...

  6. 查找元素在list中的位置以及折半查询

    问题 查找某个值在list中的位置 解决思路 能够用折半查询的方法解决此问题. 解决(Python) #! /usr/bin/env python #coding:utf-8 #折半查找某个元素在li ...

  7. mybatis中foreach的用法以及特殊的情况的用法

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. ...

  8. RabbitMQ入门教程(五):扇形交换机发布/订阅(Publish/Subscribe)

    原文:RabbitMQ入门教程(五):扇形交换机发布/订阅(Publish/Subscribe) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. ...

  9. group_concat默认长度限制

    这几天做后台一个订单汇总数据报表时,发现当使用group_concat函数时,发现会漏掉数据,究其原因是因为这个函数有默认长度显示1024 可以修改mysql配置文件my.ini 设置group_co ...

  10. Jquery复习(一)之animate()易忘点

    可以用 animate() 方法来操作所有 CSS 属性吗? 是的,几乎可以!不过,需要记住一件重要的事情:当使用 animate() 时,必须使用 Camel 标记法书写所有的属性名,比如,必须使用 ...