整数分解,又称质因子分解。在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式。

(每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数)

.试除法(适用于范围比较小)

无论素数判定还是因子分解,试除法(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快速因数分解(没看懂,仅当模版用)针对于比较大的整数分解

1975年,John M. Pollard提出了第二种因数分解的方法,Pollard Rho快速因数分解。该算法时间复杂度为O(n^(1/4))。
对于因子很少,因子值却很大的数n,该方法不是很有效。

模版:

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大整数分解)的更多相关文章

  1. Miller-Rabin 素性测试 与 Pollard Rho 大整数分解

    \(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...

  2. POJ 1811 Prime Test (Pollard rho 大整数分解)

    题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...

  3. Pollard Rho大质数分解学习笔记

    目录 问题 流程 代码 生日悖论 end 问题 给定n,要求对n质因数分解 普通的试除法已经不能应用于大整数了,我们需要更快的算法 流程 大概就是找出\(n=c*d\) 如果\(c\)是素数,结束,不 ...

  4. HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...

  5. 大整数加减运算的C语言实现

    目录 大整数加减运算的C语言实现 一. 问题提出 二. 代码实现 三. 效果验证 大整数加减运算的C语言实现 标签: 大整数加减 C 一. 问题提出 培训老师给出一个题目:用C语言实现一个大整数计算器 ...

  6. JAVA版拆分大整数为2幂的和算法

    import java.util.ArrayList; import java.util.List; public class StrTest { public static void main(St ...

  7. Ural 1158. Censored! 有限状态自动机+DP+大整数

    Ural1158 看上去很困难的一道题. 原文地址 http://blog.csdn.net/prolightsfxjh/article/details/54729646 题意:给出n个不同的字符,用 ...

  8. 质因数分解的rho以及miller-rabin

    一.前言 质因数分解,是一个在算法竞赛里老生常谈的经典问题.我们在解决许多问题的时候需要用到质因数分解来辅助运算,而且质因数分解牵扯到许许多多经典高效的算法,例如miller-rabin判断素数算法, ...

  9. 大整数分解质因数(Pollard rho算法)

    #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> ...

随机推荐

  1. NLP入门相关——学习笔记

    近义词.一词多义 GPT.ELMO.Bert

  2. PHPCMS v9设置文章的审核功能

    对于新建的站点,如果想设置会员发布的文章必须通过审核后才能发布,则需要以下几步来完成: 1.根据需要自定义管理员角色或选择已有角色. 步骤:设置->管理员设置->角色管理->权限设置 ...

  3. 工具类之数据库工具类:DBUtil(採用反射机制)

    常常操作数据库的码农们一定知道操作数据库是一项非常复杂的工作.它不仅要解决各种乱码的问题还要解决各种数据表的增删改查等的操作. 另外每次操作数据库都要用到数据库连接.运行SQL语句.关闭连接的操作.所 ...

  4. php 关于日期的知识总结

    1.UNIX时间戳   time() echo time(); 2.UNIX时间戳转换为日期用函数: date() 一般形式:date(); 即 echo date(date('Y-m-d H:i:s ...

  5. poj_2441 状态压缩dp

    题目大意 N头牛,M个谷仓,每个牛c都有它喜欢的若干个谷仓,现在要将这N头牛安排进谷仓,使得每个牛都位于它喜欢的谷仓,而每个谷仓只能有一头牛.求安排的方案总数.N, M <= 20 题目分析 将 ...

  6. js offset

    1.offsetParent offsetParent属性返回一个对象的引用,这个对象是距离调用offsetParent的元素最近的(在包含层次中最靠近的),并且是已进行过CSS定位的容器元素. 如果 ...

  7. App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure.

    [摘要: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecur ...

  8. java的bean和map互转的工具类

    import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.Introspector;i ...

  9. java父类可以强制转化成子类吗?

    转自:http://blog.csdn.net/ld422586546/article/details/9707997 Java中父类强制转换成子类的原则:父类型的引用指向的是哪个子类的实例,就能转换 ...

  10. (转)Python学习路径及练手项目合集

    转载自知乎 Wayne Shi,仅仅为了方便收藏查看,侵权删. 阶段1:入门知识 零编程基础的可以先从下面几个教程了解编程及环境入门知识.(已有编程基础直接从阶段2起步) 1. 编程新手指南2. Li ...