整数(质因子)分解(Pollard rho大整数分解)
整数分解,又称质因子分解。在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式。
(每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数。)
.试除法(适用于范围比较小)
无论素数判定还是因子分解,试除法(Trial Division)都是首先要进行的步骤。令m=n,从2~根n一一枚举,如果当前数能够整除m,那么当前数就是n的素数因子,并用整数m
将当前数除尽为止。
若循环结束后m是大于1的整数,那么此时m也是n的素数因子。
事例如HDU1164:15mm
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define N 65535
using namespace std;
int factor[N],top;
void divide(int n)
{
for(int i=; i<=sqrt(n+0.0); i++)
{
while(n%i==)
{
top++;
factor[top]=i;
n/=i;
}
}
if(n!=)
{
top++;
factor[top]=n;
}
for(int i=; i<=top-; i++)
{
printf("%d*",factor[i]);
}
printf("%d\n",factor[top]);
return ;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
top=;
divide(n);
}
return ;
}
2.筛选法对整数分解
试除法进行了许多不必要的运算,先将2~根n的所有素数打表,然后对应素数表一一试除将会大大节约时间。
事例如HDU1164:0mm
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define N 65540
using namespace std;
int factor[N],top,cnt,prime[N];
bool b[N];
void make_prime()
{
top=;
b[]=b[]=false;
b[]=true;
prime[++top]=;
for(int i=; i<N; i++)
if(i%==) b[i]=false;
else b[i]=true;
double t=sqrt(*1.0);
for(int i=; i<=t; i++)
{
if(b[i])
{
prime[++top]=i;
for(int j=i*i; j<N; j=j+i)
{
b[j]=false;
}
}
}
}
void divide(int n)
{
cnt=;
int temp=sqrt(n+0.0);
for(int i=; i<=top; i++)
{
if(prime[i]>temp)
break;
while(n%prime[i]==)
{
cnt++;
factor[cnt]=prime[i];
n/=prime[i];
}
}
if(n!=)
{
cnt++;
factor[cnt]=n;
}
for(int i=; i<=cnt-; i++)
{
printf("%d*",factor[i]);
}
printf("%d\n",factor[cnt]);
return ;
}
int main()
{
int n;
make_prime();
while(scanf("%d",&n)!=EOF)
{
divide(n);
}
return ;
}
3.pollard rho快速因数分解(没看懂,仅当模版用)针对于比较大的整数分解
模版:
POJ1181:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <algorithm>
typedef long long ll;
#define Time 15 //随机算法判定次数,Time越大,判错概率越小
using namespace std;
ll n,ans,factor[];//质因数分解结果(刚返回时是无序的)
ll tol;//质因数的个数,数组下标从0开始
//****************************************************************
// Miller_Rabin 算法进行素数测试
//速度快,而且可以判断 <2^63的数
//****************************************************************
long long mult_mod(ll a,ll b,ll c)//计算 (a*b)%c. a,b都是ll的数,直接相乘可能溢出的
{
a%=c;// 利用二分思想减少相乘的时间
b%=c;
ll ret=;
while(b)
{
if(b&)
{
ret+=a;
ret%=c;
}
a<<=;
if(a>=c)a%=c;
b>>=;
}
return ret;
}
ll pow_mod(ll x,ll n,ll mod)//x^n%n
{
if(n==)return x%mod;
x%=mod;
ll tmp=x;
ll ret=;
while(n)
{
if(n&) ret=mult_mod(ret,tmp,mod);
tmp=mult_mod(tmp,tmp,mod);
n>>=;
}
return ret;
}
//以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
//一定是合数返回true,不一定返回false
//二次探测
bool check(ll a,ll n,ll x,ll t)
{
ll ret=pow_mod(a,x,n);
ll last=ret;
for(int i=; i<=t; i++)
{
ret=mult_mod(ret,ret,n);
if(ret==&&last!=&&last!=n-) return true;//合数
last=ret;
}
if(ret!=) return true;
return false;
} // Miller_Rabin()算法素数判定
//是素数返回true.(可能是伪素数,但概率极小)
//合数返回false;
bool Miller_Rabin(ll n)
{
if(n<)return false;
if(n==||n==||n==||n==)return true;
if(n==||(n%==)||(n%==)||(n%==)||(n%==)) return false;//偶数
ll x=n-;
ll t=;
while((x&)==)
{
x>>=;
t++;
}
for(int i=; i<Time; i++)
{
ll a=rand()%(n-)+;//rand()需要stdlib.h头文件
if(check(a,n,x,t))
return false;//合数
}
return true;
}
//************************************************
//pollard_rho 算法进行质因数分解
//************************************************
ll gcd(ll a,ll b)
{
if(a==)return ;
if(a<) return gcd(-a,b);
while(b)
{
long long t=a%b;
a=b;
b=t;
}
return a;
}
ll Pollard_rho(ll x,ll c)
{
ll i=,k=;
ll x0=rand()%x;
ll y=x0;
while()
{
i++;
x0=(mult_mod(x0,x0,x)+c)%x;
long long d=gcd(y-x0,x);
if(d!=&&d!=x) return d;
if(y==x0) return x;
if(i==k)
{
y=x0;
k+=k;
}
}
}
//对n进行素因子分解
void findfac(ll n)
{
if(Miller_Rabin(n))//素数
{
factor[tol++]=n;
return;
}
ll p=n;
while(p>=n) p=Pollard_rho(p,rand()%(n-)+);
findfac(p);//递归调用
findfac(n/p);
}
int main()
{
int T;
//srand(time(NULL));加上RE不懂
scanf("%d",&T);
while(T--)
{
scanf("%lld",&n);//(n>=2)
/*if(n==1)
{
printf("1\n");
continue;
}*/
if(Miller_Rabin(n))
{
printf("Prime\n");
continue;
}
tol=;
findfac(n);//对n分解质因子
ll ans=factor[];
for(int i=; i<tol; i++)
if(factor[i]<ans)
ans=factor[i];
/*for(int i=0;i<tol;i++)
{
printf("%lld\n",factor[i]);
}*/
printf("%lld\n",ans);
}
return ;
}
Kuangbin写的。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std; //****************************************************************
// Miller_Rabin 算法进行素数测试
//速度快,而且可以判断 <2^63的数
//****************************************************************
const int S=;//随机算法判定次数,S越大,判错概率越小 //计算 (a*b)%c. a,b都是long long的数,直接相乘可能溢出的
// a,b,c <2^63
long long mult_mod(long long a,long long b,long long c)
{
a%=c;
b%=c;
long long ret=;
while(b)
{
if(b&){ret+=a;ret%=c;}
a<<=;//别手残,这里是a<<=1,不是快速幂的a=a*a;
if(a>=c)a%=c;
b>>=;
}
return ret;
} //计算 x^n %c
long long pow_mod(long long x,long long n,long long mod)//x^n%c
{
if(n==)return x%mod;
x%=mod;
long long tmp=x;
long long ret=;
while(n)
{
if(n&) ret=mult_mod(ret,tmp,mod);
tmp=mult_mod(tmp,tmp,mod);
n>>=;
}
return ret;
} //以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
//一定是合数返回true,不一定返回false
bool check(long long a,long long n,long long x,long long t)
{
long long ret=pow_mod(a,x,n);
long long last=ret;
for(int i=;i<=t;i++)
{
ret=mult_mod(ret,ret,n);
if(ret==&&last!=&&last!=n-) return true;//合数
last=ret;
}
if(ret!=) return true;
return false;
} // Miller_Rabin()算法素数判定
//是素数返回true.(可能是伪素数,但概率极小)
//合数返回false; bool Miller_Rabin(long long n)
{
if(n<)return false;
if(n==)return true;
if((n&)==) return false;//偶数
long long x=n-;
long long t=;
while((x&)==){x>>=;t++;}
for(int i=;i<S;i++)
{
long long a=rand()%(n-)+;//rand()需要stdlib.h头文件
if(check(a,n,x,t))
return false;//合数
}
return true;
} //************************************************
//pollard_rho 算法进行质因数分解
//************************************************
long long factor[];//质因数分解结果(刚返回时是无序的)
int tol;//质因数的个数。数组小标从0开始 long long gcd(long long a,long long b)
{
if(a==)return ;//???????
if(a<) return gcd(-a,b);
while(b)
{
long long t=a%b;
a=b;
b=t;
}
return a;
} long long Pollard_rho(long long x,long long c)
{
long long i=,k=;
long long x0=rand()%x;
long long y=x0;
while()
{
i++;
x0=(mult_mod(x0,x0,x)+c)%x;
long long d=gcd(y-x0,x);
if(d!=&&d!=x) return d;
if(y==x0) return x;
if(i==k){y=x0;k+=k;}
}
}
//对n进行素因子分解
void findfac(long long n)
{
if(Miller_Rabin(n))//素数
{
factor[tol++]=n;
return;
}
long long p=n;
while(p>=n)p=Pollard_rho(p,rand()%(n-)+);
findfac(p);
findfac(n/p);
}
int main()
{
// srand(time(NULL));//需要time.h头文件 //POJ上G++要去掉这句话
int T;
long long n;
scanf("%d",&T);
while(T--)
{
scanf("%I64d",&n);
if(Miller_Rabin(n))
{
printf("Prime\n");
continue;
}
tol=;
findfac(n);
long long ans=factor[];
for(int i=;i<tol;i++)
if(factor[i]<ans)
ans=factor[i];
printf("%I64d\n",ans);
}
return ;
}
整数(质因子)分解(Pollard rho大整数分解)的更多相关文章
- Miller-Rabin 素性测试 与 Pollard Rho 大整数分解
\(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
- Pollard Rho大质数分解学习笔记
目录 问题 流程 代码 生日悖论 end 问题 给定n,要求对n质因数分解 普通的试除法已经不能应用于大整数了,我们需要更快的算法 流程 大概就是找出\(n=c*d\) 如果\(c\)是素数,结束,不 ...
- HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解
链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...
- 大整数加减运算的C语言实现
目录 大整数加减运算的C语言实现 一. 问题提出 二. 代码实现 三. 效果验证 大整数加减运算的C语言实现 标签: 大整数加减 C 一. 问题提出 培训老师给出一个题目:用C语言实现一个大整数计算器 ...
- JAVA版拆分大整数为2幂的和算法
import java.util.ArrayList; import java.util.List; public class StrTest { public static void main(St ...
- Ural 1158. Censored! 有限状态自动机+DP+大整数
Ural1158 看上去很困难的一道题. 原文地址 http://blog.csdn.net/prolightsfxjh/article/details/54729646 题意:给出n个不同的字符,用 ...
- 质因数分解的rho以及miller-rabin
一.前言 质因数分解,是一个在算法竞赛里老生常谈的经典问题.我们在解决许多问题的时候需要用到质因数分解来辅助运算,而且质因数分解牵扯到许许多多经典高效的算法,例如miller-rabin判断素数算法, ...
- 大整数分解质因数(Pollard rho算法)
#include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> ...
随机推荐
- mongoDB的shell数组操作器
http://www.2cto.com/database/201304/205024.html mongoDB数组操作器 $push会向数组末尾加入一个元素,如果数组不存在,则会创建这个数组. 增 ...
- [extjs] ExtJS 4.2 开发环境搭建
到官网下载Extjs ,现在最新版的是Ext5.1. 这里用ext4.2演示开发.http://extjs.org.cn/. EXT4.1 在线API 项目结构中ext4.2导入的资源文件: 第一个页 ...
- ajax 同步
Ajax请求默认的都是异步的如果想同步 async设置为false就可以(默认是true) var html = $.ajax({ url: "some.php", async ...
- Android开发之--常用颜色值
<?xml version="1.0" encoding="utf-8" ?> <resources> <color name=& ...
- 关于直播学习笔记-003-nginx-rtmp、srs、vlc、obs
服务器 1.nginx-rtmp:https://github.com/illuspas/nginx-rtmp-win32 2.srs:https://github.com/illuspas/srs- ...
- Android 使用线性布局LinearLayout和Button实现一个点红块游戏
这个游戏的功能类似打地鼠. 项目地址:https://github.com/moonlightpoet/RedBlock 程序下载试玩地址:https://github.com/moonlightpo ...
- OpenStack三个节点icehouse
一.环境准备 1.架构 创建3台虚拟机,分别作为controll节点.network节点和compute1节点. Controller节点:1processor,2G memory,5G storag ...
- vs删除空行 —— 正则表达式以及其他
\r:return 到当前行的最左边. \n: newline 向下移动一行,并不移动左右. 1. ^\s*\n 查找空行 2. ^\s*(?=\r?$)\n 查找空行即 ...
- LeetCode——Power of Two
Description: Given an integer, write a function to determine if it is a power of two. public class S ...
- {sharepoint} Setting List Item Permissions Programatically in sharepoint
namespace Avinash { class Program { static void Main(string[] args) { SetListItemPermission(); } sta ...