随机化算法,想尝试自己写一下,最后还是变成了抄代码。。。

代码参考了:POJ 1811 Prime Test(大素数判断和素因子分解) - kuangbin - 博客园

学习链接:

Miller-Rabin素数测试学习小计 - 将狼踩尽 19891101 - 博客园

数论部分第一节:素数与素性测试

Miller_Rabin素数测试[Fermat小定理][二次探测定理][同余式][Wilson定理] - 以中有足乐者... - 博客频道 - CSDN.NET

整数分解费马方法以及Pollard rho_龙-泪之魂_新浪博客

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <time.h>
#include <iostream>
#include <algorithm>
using namespace std;#define i64 __int64//****************************************************************
// Miller_Rabin 算法进行素数测试
//速度快,而且可以判断 <2^63的数
//****************************************************************
const int S=20;//随机算法判定次数,S越大,判错概率越小//计算 (a*b)%c.   a,b都是long long的数,直接相乘可能溢出的
/*
i64 Mult_mod (i64 a,i64 b,i64 c)   //  a,b,c <2^63
{a%=c;b%=c;i64 ret=0;while (b){if (b&1) {ret+=a;ret%=c;}a<<=1;if (a>=c)a%=c;b>>=1;}return ret;
}*/i64 Mult_mod (i64 a,i64 b,i64 c)  //减法实现比取模速度快
{    //返回(a*b) mod c,a,b,c<2^63a%=c;b%=c;i64 ret=0;while (b){if (b&1){ret+=a;if (ret>=c) ret-=c;}a<<=1;if (a>=c) a-=c;b>>=1;}return ret;
}//计算  x^n %c
i64 Pow_mod (i64 x,i64 n,i64 mod) //x^n%c
{if (n==1) return x%mod;x%=mod;i64 tmp=x;i64 ret=1;while (n){if (n&1) ret=Mult_mod(ret,tmp,mod);tmp=Mult_mod(tmp,tmp,mod);n>>=1;}return ret;
}//以a为基,n-1=x*2^t      a^(n-1)=1(mod n)  验证n是不是合数
//一定是合数返回true,不一定返回false
bool Check (i64 a,i64 n,i64 x,i64 t)
{i64 ret=Pow_mod(a,x,n);i64 last=ret;for (int i=1;i<=t;i++){ret=Mult_mod(ret,ret,n);if(ret==1&&last!=1&&last!=n-1) return true; //合数last=ret;}if (ret!=1) return true;return false;
}// Miller_Rabin()算法素数判定
//是素数返回true.(可能是伪素数,但概率极小)
//合数返回false;bool Miller_Rabin (i64 n)
{if (n<2) return false;if (n==2) return true;if ((n&1)==0) return false;//偶数i64 x=n-1;i64 t=0;while ((x&1)==0) {x>>=1;t++;}for (int i=0;i<S;i++){i64 a=rand()%(n-1)+1; //rand()需要stdlib.h头文件if (Check(a,n,x,t))return false;//合数}return true;
}//************************************************
//pollard_rho 算法进行质因数分解
//************************************************i64 factor[100];//质因数分解结果(刚返回时是无序的)
int tol;//质因数的个数。数组下标从0开始i64 Gcd (i64 a,i64 b)
{if (a==0) return 1;  //???????if (a<0) return Gcd(-a,b);while (b){i64 t=a%b;a=b;b=t;}return a;
}i64 Pollard_rho (i64 x,i64 c)
{i64 i=1,k=2;i64 x0=rand()%x;i64 y=x0;while (true){i++;x0=(Mult_mod(x0,x0,x)+c)%x;i64 d=Gcd(y-x0,x);if (d!=1 && d!=x) return d;if (y==x0) return x;if (i==k) {y=x0;k+=k;}}
}
//对n进行素因子分解
void Findfac (i64 n)
{if (Miller_Rabin(n)) //素数{factor[tol++]=n;return;}i64 p=n;while (p>=n) p=Pollard_rho(p,rand()%(n-1)+1);Findfac(p);Findfac(n/p);
}int main ()  // Poj 1811 交G++ 比c++ 快很多
{// srand(time(NULL));//需要time.h头文件  //POJ上G++要去掉这句话int T;scanf("%d",&T);while (T--){i64 n;scanf("%I64d",&n);if (Miller_Rabin(n)){printf("Prime\n");continue;}tol=0;Findfac(n);i64 ans=factor[0];for (int i=1;i<tol;i++)if (factor[i]<ans)ans=factor[i];printf("%I64d\n",ans);}return 0;
}

Poj 1811 Prime Test 素数测试 Miller-Rabin 与 整数的因子分解 Pollard rho相关推荐

  1. 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 ...

  2. 与数论的厮守01:素数的测试——Miller Rabin

    看一个数是否为质数,我们通常会用那个O(√N)的算法来做,那个算法叫试除法.然而当这个数非常大的时候,这个高增长率的时间复杂度就不够这个数跑了. 为了解决这个问题,我们先来看看费马小定理:若n为素数, ...

  3. GCD and LCM Aizu - 0005(辗转相除)+GCD LCM Inverse POJ - 2429(java或【Miller Rabin素数測试】+【Pollar Rho整数分解】)

    题目:GCD and LCM Aizu - 0005 Write a program which computes the greatest common divisor (GCD) and the ...

  4. POJ 1811 Prime Test

    题意:对于一个大整数,判断是否质数,如果不是质数输出最小质因子. 解法:判断质数使用Miller-Rabin测试,分解质因子使用Pollard-Rho,Miller-Rabin测试用的红书模板,将测试 ...

  5. POJ - 2689 Prime Distance(素数区间筛模板)

    题目链接:点击查看 题目大意:给出一段闭区间[l,r],求区间内相邻距离最大的素数对和相邻距离最小的素数对,题目保证r-l<=1e6,1<=l<=r<= 题目分析:因为我们要求 ...

  6. POJ 1811 Prime Test

    Miller-Rabin测试 + Pollard-rho因子分解.关于Pollard-rho的总结以后会写上,Miller-Tabin测试见:http://www.cnblogs.com/vongan ...

  7. 素数判定质因数分解(数论)(Miller Rabin)(Pollard Rho)

    太玄学了! 我真的被概率的魅力折服了.此前我认为1便是1,0.9999999999-便是0.9999999999-. 但实际上它们有着千丝万缕的关系. 试想,如果一件事发生的概率是0.99999999 ...

  8. (Miller Rabin算法)判断一个数是否为素数

    (Miller Rabin算法)判断一个数是否为素数 1.约定 x%y为x取模y,即x除以y所得的余数,当x<y时,x%y=x,所有取模的运算对象都为整数. x^y表示x的y次方.乘方运算的优先 ...

  9. FZU 1649 Prime number or not (大素数测试)

    题目链接 Problem 1649 Prime number or not Accept: 661    Submit: 3016 Time Limit: 2000 mSec    Memory Li ...

最新文章

  1. linux qt5.7下打地鼠源程序,基于QT的打地鼠游戏
  2. 一文告诉你,Intellij IDEA神器隐藏的11种实用小技巧!
  3. java解析nes_Java 读写 excel 实战完全解析
  4. 爱数之介质服务器及介质同步技术
  5. Linux命令:和输出
  6. 【Device Tree】设备树(一)——GPIO
  7. nssl1155-遨游【二分答案,SPFA】
  8. Python深入05 装饰器
  9. kafka manager 2.0 工具下载 已打包完成
  10. shell(希尔)排序
  11. 《Java并发编程实践》读书笔记
  12. Microsemi Libero使用技巧3——使用FlashPro单独下载程序
  13. 网易云邮箱验证码注册及修改密码
  14. 网易邮箱异常信息说明
  15. 检验图片有没有被 P 过
  16. 斗鱼已公开的运维技术和架构分析
  17. uniApp和微信小程序好看的我的页面(有源码)
  18. 联想台式修复计算机,联想台式电脑如何一键恢复出厂设置
  19. ESP8266-Arduino编程实例-ILI9341-TFT LCD驱动(基于TFT_eSPI库)
  20. lyapunov函数

热门文章

  1. 企业微信企业互联功能怎么用?
  2. Vue2的router-view中子组件与父组件传值
  3. 在Centos7.X上安装中文字体及相关配置
  4. Topaz DeNoise AI 1.1.1 特别版 Mac AI图片降噪软件
  5. 志勤美集与物流客户的EDI连接
  6. Understanding of Hilbert-Schmidt Independence Criterion (HSIC)
  7. 求职无忧之 JavaScript 每日 3 道 面试题
  8. linux awk学习(每日一令之十五)
  9. VTK学习笔记(九)VTK中的各个模块
  10. YTU OJ 2377: Doorman