zoukankan      html  css  js  c++  java
  • poj 2429 Pollard_rho大数分解

    先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小。

    这里能够直接搜索,注意一个问题,因为同样因子不能分配给两边(会改变gcd)所以能够将同样因子合并,这种话,搜索的层数也变的非常少了。

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<math.h>
    #include<stdlib.h>
    #include<time.h>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    #define maxn 10000
    LL factor[maxn];
    int tot;
    const int S=10;                 //測试次数
    LL muti_mod(LL a,LL b,LL c)
    {
        a%=c;b%=c;
        LL ret=0;
        while (b){
            if (b&1){
                ret+=a;
                if (ret>=c) ret-=c;
            }
            a<<=1;
            if (a>=c) a-=c;
            b>>=1;
        }
        return ret;
    }
    LL pow_mod(LL x,LL n,LL mod)
    {
        if (n==1) return x%mod;
        int bit[90],k=0;
        while (n){
            bit[k++]=n&1;
            n>>=1;
        }
        LL ret=1;
        for (k=k-1;k>=0;k--){
            ret=muti_mod(ret,ret,mod);
            if (bit[k]==1) ret=muti_mod(ret,x,mod);
        }
        return ret;
    }
    bool check(LL a,LL n,LL x,LL t){   //以a为基,n-1=x*2^t,检验n是不是合数
        LL ret=pow_mod(a,x,n),last=ret;
        for (int i=1;i<=t;i++){
            ret=muti_mod(ret,ret,n);
            if (ret==1 && last!=1 && last!=n-1) return 1;
            last=ret;
        }
        if (ret!=1) return 1;
        return 0;
    }
    bool Miller_Rabin(LL n){    //是素数返回0,合数返回1
        LL x=n-1,t=0;
        while ((x&1)==0) x>>=1,t++;
        bool flag=1;
        if (t>=1 && (x&1)==1){
            for (int k=0;k<S;k++){
                LL a=rand()%(n-1)+1;
                if (check(a,n,x,t)) {flag=1;break;}
                flag=0;
            }
        }
        if (!flag || n==2) return 0;
        return 1;
    }
    
    LL gcd(LL a,LL b){
        if (a==0) return 1;
        if (a<0) return gcd(-a,b);
        while (b){
            LL t=a%b; a=b; b=t;
        }
        return a;
    }
    LL Pollard_rho(LL x,LL c){
        LL i=1,x0=rand()%x,y=x0,k=2;
        while (1){
            i++;
            x0=(muti_mod(x0,x0,x)+c)%x;
            LL 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 findfac(LL n)//质因数分解,存在factor里
    {
        if (!Miller_Rabin(n)){
            factor[tot++] = n;
            return;
        }
        LL p=n;
        while (p>=n) p=Pollard_rho(p,rand() % (n-1) +1);
        findfac(p);
        findfac(n/p);
    }
    LL mins,aa,bb;
    int top;
    void dfs(LL a,LL b,int p)
    {
        if(a+b>=mins) return;
        if(p==top)
        {
            if(a+b<mins)
            {
                mins=a+b;
                aa=a;
                bb=b;
            }
            return;
        }
        dfs(a*factor[p],b,p+1);
        dfs(a,b*factor[p],p+1);
    }
    int main()
    {
        LL a,b,c;
        while(~scanf("%lld%lld",&a,&b))
        {
            if(a==b) {printf("%lld %lld
    ",a,b);continue;}
            mins=~0ull>>1;
            c=b/a;
            tot=0;
            findfac(c);
            sort(factor,factor+tot);
            top=0;
            for(int i=0;i<tot;i++)
            {
                if(i==0) factor[top++]=factor[i];
                else if(factor[i]==factor[i-1]) factor[top-1]*=factor[i];
                else factor[top++]=factor[i];
            }
            dfs(a,a,0);
            if(aa>bb) swap(aa,bb);
            printf("%lld %lld
    ",aa,bb);
        }
        return 0;
    }
    


  • 相关阅读:
    执行序列oracle存储过程和序列化写的demo
    快捷键列表myeclipse 设置代码提示快捷键
    鼠标管理解决win8 插上usb/鼠标蓝屏或无效方法
    JQuery实现拼图数字游戏
    Django的admin定制
    Django报:AttributeError: tuple object has no attribute get
    Django的models方法返回值异常,待解决
    主页跳转子页面的时候,模板语句中的数据未返回到页面(子页面空白)
    Django报:builtin_function_or_method' object is not iterable
    Windows Azure SDK 1.5、Windows Azure Tools for Microsoft Visual Studio 2010和新的服务管理功能发布了
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4077069.html
Copyright © 2011-2022 走看看