[poj1811]Prime Test(Pollard-Rho大整数分解)
问题描述:素性测试兼质因子分解
解题关键:pollard-rho质因数分解,在RSA的破译中也起到了很大的作用
期望复杂度:$O({n^{\frac{1}{4}}})$
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<vector>
#define inf 1ll<<61
typedef long long ll;
using namespace std;
const int S=;
ll fac[],tol,mi;
ll mod_mul(ll a,ll b,ll p){
ll res=;
a%=p,b%=p;
while(b){
if(b&)res=(res+a)%p;
a=(a<<)%p;
b>>=;
}
return res;
}
ll mod_pow(ll x,ll n,ll p){
ll res=;
while(n){
if(n&)res=mod_mul(res,x,p);
x=mod_mul(x,x,p);
n>>=;
}
return res;
} bool check(ll a,ll n,ll x,ll t){//判断是否为合数
ll ret=mod_pow(a,x,n);
ll last=ret;
for(int i=;i<=t;i++){
ret=mod_mul(ret,ret,n);
if(ret==&&last!=&&last!=n-)return ;
last=ret;
}
if(ret!=)return ;//fermat测试
return ;
} bool Miller_Rabin(ll n){
if(n<)return ;
if(n==)return ;
if((n&)==)return ;
ll x=n-,t=;
while((x&)==)x>>=,t++;
for(int i=;i<S;i++){
ll a=rand()%(n-)+;
if(check(a,n,x,t))return ;//合数
}
return ;
} ll Pollard_Rho(ll n,ll c){//返回值n的因子
ll i=,j=,x=rand()%(n-)+,y=x;
while(){
i++,x=(mod_mul(x,x,n)+c)%n;
ll p=__gcd((y-x+n)%n,n);
if(p!=&&p!=n)return p;//p本身是合数,分解为本身就无意义了
if(y==x)return n;//循环节只有1,不符合条件,同时也判圈了
if(i==j)y=x,j<<=;//这里控制1步和2步
}
}
void find1(ll n,ll c){//找因子主体
if(n==) return;
if(Miller_Rabin(n)){
fac[tol++]=n;
mi=min(mi,n);
return;
}
ll p=n,k=c;
while(p>=n)p=Pollard_Rho(p,c--);//返回的是小于n但不一定为素数的因子
find1(p,k);
find1(n/p,k);
}
int main() {
int t;
scanf("%d",&t);
while(t--){
long long n;
scanf("%lld",&n);
mi=n;
if(Miller_Rabin(n)) cout<<"Prime"<<endl;
else{
find1(n,);
cout<<mi<<endl;
}
}
return ;
}
[poj1811]Prime Test(Pollard-Rho大整数分解)的更多相关文章
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
- Miller-Rabin 素性测试 与 Pollard Rho 大整数分解
\(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...
- 整数(质因子)分解(Pollard rho大整数分解)
整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...
- HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解
链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...
- Pollard Rho大质数分解学习笔记
目录 问题 流程 代码 生日悖论 end 问题 给定n,要求对n质因数分解 普通的试除法已经不能应用于大整数了,我们需要更快的算法 流程 大概就是找出\(n=c*d\) 如果\(c\)是素数,结束,不 ...
- 大整数分解质因数(Pollard rho算法)
#include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> ...
- 【HDU - 4344】Mark the Rope(大整数分解)
BUPT2017 wintertraining(15) #8E 题意 长度为n(\(n<2^{63}\))的绳子,每隔长度L(1<L<n)做一次标记,标记值就是L,L是n的约数. 每 ...
- POJ2429 GCD & LCM Inverse pollard_rho大整数分解
Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...
- POJ 1811 Prime Test( Pollard-rho整数分解经典题 )
链接:传送门 题意:输入 n ,判断 n 是否为素数,如果是合数输出 n 的最素因子 思路:Pollard-rho经典题 /************************************** ...
随机推荐
- Linux将进程写入开机自启动
只需将启动的命令写入/etc/rc.local 如让mongodb开机自启动: echo "/usr/local/mongodb/bin/mongod --dbpath=/usr/local ...
- c++ 通过数据流方式实现文件拷贝
#include "stdafx.h"#include <iostream>#include<fstream>using namespace std;voi ...
- myeclipse中disable maven nature怎么恢复
eclipse更新maven的时候,不小心手一抖,点上了Disable Maven Nature,然后工程右键菜单中的Maven栏就不见了! 其实这是把maven工程转换成了一般工程,再转回来就好了. ...
- android菜鸟学习笔记16----Android项目打包安装过程(Run as Android Application)
右击项目名称,Run as Android Appication之后,Android项目打包安装过程: 1.打包生成.apk文件: 1)把源码中的.java文件编译生成.class文件 2)将所有的. ...
- CentOS、乌班图设置固定静态IP
CentOS.乌班图设置固定静态IP 一.centOS 1.编辑 ifcfg-eth0 文件 # vim /etc/sysconfig/network-scripts/ifcfg-eth0 2,在文件 ...
- JavaScript演示如何访问Search字段
<!DOCTYPE html> <html> <body> <h3>演示如何访问Search字段</h3> <input type=& ...
- EF学习和使用(三)Code First
Code First模式我们称之为"代码优先"模式.从某种角度来看.其实"Code First"和"Model First"区别并非太明显. ...
- 【AWS】亚马逊云常用服务解释
新公司使用的是亚马逊服务,刚开始的时候,对很多名词不太明白,总结了一下如下 1,EC2 这个是亚马逊的一种服务器服务,可以理解为跟vmware差不多,EC2为虚拟机提供载体,EC2上跑虚拟机服务器. ...
- Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- python获取本机IP地址
方法一 通常使用socket.gethostname()方法即可获取本机IP地址,但有时候获取不到(比如没有正确设置主机名称) import socket #获取计算机名称hostname=socke ...