• 大素数判断和素因子分解(miller-rabin,Pollard_rho算法) 玄学快


     

    大数因数分解Pollard_rho 算法

    复杂度o^(1/4)

    #include <iostream> 
    #include <cstdio> 
    #include <algorithm>  
    #include <cmath>  
    #include <cstring>  
    #include <map>  
    using namespace std;
     
    const int times = 50;
    int number = 0;
     
    map<long long, int>m;
    long long Random( long long n )
    {
        return ((double)rand( ) / RAND_MAX*n + 0.5);
    }
     
    long long q_mul( long long a, long long b, long long mod ) //快速乘法取模
    {
        long long ans = 0;
        while(b)
        {
            if(b & 1)
            {
                ans += a;
            }
            b /= 2;
            a = (a + a) % mod;
     
        }
        return ans;
    }
     
    long long q_pow( long long a, long long b, long long mod ) //快速乘法下的快速幂,叼
    {
        long long ans = 1;
        while(b)
        {
            if(b & 1)
            {
                ans = q_mul( ans, a, mod );
            }
            b /= 2;
            a = q_mul( a, a, mod );
        }
        return ans;
    }
     
    bool witness( long long a, long long n )//miller_rabin算法的精华
    {
        long long tem = n - 1;
        int j = 0;
        while(tem % 2 == 0)
        {
            tem /= 2;
            j++;
        }
        
        long long x = q_pow( a, tem, n ); //得到a^(n-1) mod n
        if(x == 1 || x == n - 1) return true;
        while(j--)
        {
            x = q_mul( x, x, n );
            if(x = n - 1) return true;
        }
        return false;
    }
     
    bool miller_rabin( long long n )  //检验n是否是素数
    {
     
        if(n == 2)
            return true;
        if(n < 2 || n % 2 == 0)
            return false;
     
        for(int i = 1; i <= times; i++)  //做times次随机检验
        {
            long long a = Random( n - 2 ) + 1; //得到随机检验算子 a
            if(!witness( a, n ))  //用a检验n是否是素数
                return false;
        }
        return true;
    }
     
    long long gcd( long long a, long long b )
    {
        if(b == 0)
            return a;
        return gcd( b, a%b );
    }
     
    long long pollard_rho( long long n, long long c )//找到n的一个因子
    {
        long long x, y, d, i = 1, k = 2;
        x = Random( n - 1 ) + 1;
        y = x;
        while(1)
        {
            i++;
            x = (q_mul( x, x, n ) + c) % n;
            d = gcd( y - x, n );
            if(1<d&&d<n)
                return d;
            if(y == x)//找到循环,选取失败,重新来
                return n;
            if(i == k) //似乎是一个优化,但是不是很清楚
            {
                y = x;
                k <<= 1;
            }
        }
    }
     
    void find( long long n, long long c )
    {
        if(n == 1)
            return;
        if(miller_rabin( n ))
        {
            m[n]++;
            number++;
            return;
        }
     
        long long p = n;
        while(p >= n)
            p = pollard_rho( p, c-- );
        find( p, c );
        find( n / p, c );
    }
     
    int main( )
    {
        long long tar;
        while(cin >> tar)
        {
            number = 0;
            m.clear();
            find( tar, 2137342 );
            printf( "%lld = ", tar );
            if(m.empty())
            {
                printf( "%lld
    ", tar );
            }
            for(map<long long, int>::iterator c = m.begin(); c != m.end();)
            {
                printf( "%lld^%d", c->first, c->second );
                if((++c) != m.end())
                    printf( " * " );
            }
            printf( "
    " );
        }
        return 0;
    }
    View Code
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    #include<ctime>
    using namespace std;
    long long factor[1000005];
    int tot;
    const int S=50;
    
    long long mult(long long a,long long b,long long c)
    {
        a%=c;
        b%=c;
        long long ret=0;
        while(b)
        {
            if(b&1)
            {
                ret=(ret+a)%c;
            }
            b>>=1;
            a<<=1;
            if(a>=c)a%=c;
        }
        return ret;
    }
    
    long long pow(long long x,long long n,long long mod)
    {
        if(n==1)return x%mod;
        x%=mod;
        long long tmp=x;
        long long ret=1;
        while(n)
        {
            if(n&1)
            {
                ret=mult(ret,tmp,mod);
            }
            tmp=mult(tmp,tmp,mod);
            n>>=1;
        }
        return ret;
    }
    
    bool check(long long a,long long n,long long x,long long t)
    {
        long long ret=pow(a,x,n);
        long long last=ret;
        for(int i=1;i<=t;i++)
        {
            ret=mult(ret,ret,n);
            if(ret==1&&last!=1&&last!=n-1)return true;//是合数
            last=ret;
        }
        if(ret!=1)return true;
        return 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;
            if(check(a,n,x,t))//如果检查出来是合数
            return false;
        }
        return true;
    }
    
    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 x,long long c)
    {
        long long i=1,k=2;
        long long x0=rand()%x;
        long long y=x0;
        while(1)
        {
            i++;
            x0=(mult(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;
            }
        }
    }
        void findphi(long long n)
        {
            if(miller_rabin(n))
            {
                factor[tot++]=n;
                return;
            }
            long long p=n;
            while(p>=n)
            {
                p=pollard_rho(p,rand()%(n-1)+1);
    
            }
            findphi(p);
            findphi(n/p);
        }
    
        int main()
        {
            long long n;
            while(scanf("%I64d",&n)!=EOF)
            {
                tot=0;
                findphi(n);
                for(int i=0;i<tot;i++)
                printf("%I64d",factor[i]),printf("
    ");
                if(miller_rabin(n))printf("yes
    ");
                else printf("no
    ");
            }
            return 0;
        }
    View Code
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<time.h>
    #include<iostream>
    #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;
            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++不能加这句话
        long long n;
        while(scanf("%I64d",&n)!=EOF)
        {
            tol=0;
            findfac(n);
            for(int i=0;i<tol;i++)printf("%I64d ",factor[i]);
            printf("
    ");
            if(Miller_Rabin(n))printf("Yes
    ");
            else printf("No
    ");
        }
        return 0;
    }
    k神模板

    详细可以看这

  • 相关阅读:
    数字精确运算BigDecimal经常用法
    C3P0数据库连接池使用
    Theano学习笔记(四)——导数
    Leetcode--Merge Intervals
    1191 数轴染色
    P1021 邮票面值设计
    P1032 字串变换
    P1294 高手去散步
    P1832 A+B Problem(再升级)
    P1332 血色先锋队
  • 原文地址:https://www.cnblogs.com/shuaihui520/p/10052974.html
Copyright © 2020-2023  润新知