整數(質因子)分解(Pollard rho大整數分解)


整數分解,稱質因子分解。在數學中,整數分解問題是指:給出一個正整數,將其寫成幾個素數的乘積的形式。

(每個合數都可以寫成幾個質數相乘的形式,這幾個質數就都叫做這個合數的質因數)

1.試除法(適用於范圍比較小)

   無論素數判定還是因子分解,試除法(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=2; i<=sqrt(n+0.0); i++)
    {
        while(n%i==0)
        {
            top++;
            factor[top]=i;
            n/=i;
        }
    }
    if(n!=1)
    {
        top++;
        factor[top]=n;
    }
    for(int i=1; i<=top-1; i++)
    {
        printf("%d*",factor[i]);
    }
    printf("%d\n",factor[top]);
    return ;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        top=0;
        divide(n);
    }
    return 0;
}
View Code

 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=0;
    b[0]=b[1]=false;
    b[2]=true;
    prime[++top]=2;
    for(int i=3; i<N; i++)
        if(i%2==0) b[i]=false;
        else b[i]=true;
    double t=sqrt(1000000*1.0);
    for(int i=3; 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=0;
    int temp=sqrt(n+0.0);
    for(int i=1; i<=top; i++)
    {
        if(prime[i]>temp)
            break;
        while(n%prime[i]==0)
        {
            cnt++;
            factor[cnt]=prime[i];
            n/=prime[i];
        }
    }
    if(n!=1)
    {
        cnt++;
        factor[cnt]=n;
    }
    for(int i=1; i<=cnt-1; 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 0;
}
View Code

 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[10001];//質因數分解結果(剛返回時是無序的)
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=0;
    while(b)
    {
        if(b&1)
        {
            ret+=a;
            ret%=c;
        }
        a<<=1;
        if(a>=c)a%=c;
        b>>=1;
    }
    return ret;
}
ll pow_mod(ll x,ll n,ll mod)//x^n%n
{
    if(n==1)return x%mod;
    x%=mod;
    ll tmp=x;
    ll 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(ll a,ll n,ll x,ll t)
{
    ll ret=pow_mod(a,x,n);
    ll 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(ll n)
{
    if(n<2)return false;
    if(n==2||n==3||n==5||n==7)return true;
    if(n==1||(n%2==0)||(n%3==0)||(n%5==0)||(n%7==0)) return false;//偶數
    ll x=n-1;
    ll t=0;
    while((x&1)==0)
    {
        x>>=1;
        t++;
    }
    for(int i=0; i<Time; i++)
    {
        ll a=rand()%(n-1)+1;//rand()需要stdlib.h頭文件
        if(check(a,n,x,t))
            return false;//合數
    }
    return true;
}
//************************************************
//pollard_rho 算法進行質因數分解
//************************************************
ll gcd(ll a,ll b)
{
    if(a==0)return 1;
    if(a<0) 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=1,k=2;
    ll x0=rand()%x;
    ll y=x0;
    while(1)
    {
        i++;
        x0=(mult_mod(x0,x0,x)+c)%x;
        long long 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(ll n)
{
    if(Miller_Rabin(n))//素數
    {
        factor[tol++]=n;
        return;
    }
    ll p=n;
    while(p>=n) p=Pollard_rho(p,rand()%(n-1)+1);
    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=0;
        findfac(n);//對n分解質因子
        ll ans=factor[0];
        for(int i=1; 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 0;
}
View Code

 

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=20;//隨機算法判定次數,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=0;
    while(b)
    {
        if(b&1){ret+=a;ret%=c;}
        a<<=1;//別手殘,這里是a<<=1,不是快速冪的a=a*a;
        if(a>=c)a%=c;
        b>>=1;
    }
    return ret;
}



//計算  x^n %c
long long pow_mod(long long x,long long n,long long mod)//x^n%c
{
    if(n==1)return x%mod;
    x%=mod;
    long long tmp=x;
    long long 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(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=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(long long n)
{
    if(n<2)return false;
    if(n==2)return true;
    if((n&1)==0) return false;//偶數
    long long x=n-1;
    long long t=0;
    while((x&1)==0){x>>=1;t++;}
    for(int i=0;i<S;i++)
    {
        long long a=rand()%(n-1)+1;//rand()需要stdlib.h頭文件
        if(check(a,n,x,t))
            return false;//合數
    }
    return true;
}


//************************************************
//pollard_rho 算法進行質因數分解
//************************************************
long long factor[100];//質因數分解結果(剛返回時是無序的)
int tol;//質因數的個數。數組小標從0開始

long long gcd(long long a,long long b)
{
    if(a==0)return 1;//???????
    if(a<0) 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=1,k=2;
    long long x0=rand()%x;
    long long y=x0;
    while(1)
    {
        i++;
        x0=(mult_mod(x0,x0,x)+c)%x;
        long long 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(long long n)
{
    if(Miller_Rabin(n))//素數
    {
        factor[tol++]=n;
        return;
    }
    long long p=n;
    while(p>=n)p=Pollard_rho(p,rand()%(n-1)+1);
    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=0;
        findfac(n);
        long long ans=factor[0];
        for(int i=1;i<tol;i++)
          if(factor[i]<ans)
             ans=factor[i];
        printf("%I64d\n",ans);
    }
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM