POJ 2429 GCD&LCM Inverse [pollard_rho]【数论】

题目链接:http://poj.org/problem?id=2429

—————————–.
GCD & LCM Inverse
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 13908 Accepted: 2572
Description

Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b. But what about the inverse? That is: given GCD and LCM, finding a and b.
Input

The input contains multiple test cases, each of which contains two positive integers, the GCD and the LCM. You can assume that these two numbers are both less than 2^63.
Output

For each test case, output a and b in ascending order. If there are multiple solutions, output the pair with smallest a + b.
Sample Input

3 60
Sample Output

12 15

———————————————.
题目大意:
就是给你两个数的GCD和LCM值 让你求出和最小的这两个数

解题思路:
lcm=a/gcd*b;
lcm/gcd=a/gcd*b/gcd;

然后把lcm/gcd 这个结果质因子分解一下就行了
然后DFS求解和最小的值..

附本题代码
——————————–.

/*******************Miller_Rabin素数测试&&Pollard_rho整数分解**************************/ 
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#define Times 11
#define MAX ((long long)1<<61) 
#define N 501
#define C 201
#define LL long long
using namespace std;


int ct,cnt;
LL mini,jl[N],factor[N],num[N];
LL mina,minb,ans,n,m;

LL gcd(LL a,LL b){
    return b==0?a:gcd(b,a%b);
}

LL random(LL n){
    return (LL)((double)rand()/RAND_MAX*n+0.5);
}

LL multi(LL m,LL n,LL k){
    LL b=0;
    while(n){
        if(n&1) b=(b+m)%k;
        n>>=1;
        m=(m<<1)%k;
    }
    return b;
}

LL quick_mod(LL m,LL n,LL k){
    LL b=1;
    m%=k; 
    while(n){
        if(n&1) b=multi(b,m,k);
        n/=2;
        m=multi(m,m,k);
    }
    return b;
}

bool Witness(LL a,LL n){
    LL m=n-1;
    int j=0;
    while(!(m&1)){
        j++;
        m>>=1;
    }
    LL x=quick_mod(a,m,n);
    if(x==1||x==n-1) return false;
    while(j--){
        x=x*x%n;
        if(x==n-1) return false;
    }
    return true;
}

bool Miller_Rabin(LL n){
    if(n<2) return false;
    if(n==2) return true;
    if(!(n&1)) return false;
    for(int i=1;i<=Times;i++){
        LL a=random(n-2)+1;
        if(Witness(a,n)) return false;
    }
    return true;
}

LL Pollard_rho(LL n,int c){
    LL x,y,d,i=1,k=2;
    x=random(n-1)+1;
    y=x;
    while(1){
        i++;
        x=(multi(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(LL n,int k){
    if(n==1) return ;
    if(Miller_Rabin(n)){
        jl[++ct]=n;
        return ;
    }
    LL p=n;
    while(p>=n) p=Pollard_rho(p,k--);
    find(p,k);
    find(n/p,k);
}

void dfs(LL c,LL value){
    LL s=1,a,b;
    if(c==cnt+1){
        a=value;
        b=ans/a;
        if(gcd(a,b)==1){
            a*=n;
            b*=n;
            if(a+b<mini){
                mini=a+b;
                mina=a; minb=b;
            }
        }
        return ;
    }
    for(LL i=0;i<=num[c];i++){
        if(s*value>mini) return ;
        dfs(c+1,s*value);
        s*=factor[c];
    }
}

int main(){

//    freopen("data.in","r",stdin);
//    freopen("data.out","w",stdout);

//    srand(time(NULL));  
    while(scanf("%lld%lld",&n,&m)!=EOF){
        if(n==m) {printf("%lld %lld\n",n,m); continue;}
        mini=MAX;
        ct=cnt=0;
        ans=m/n; 
        find(ans,C);
        sort(jl+1,jl+ct+1);
        memset(factor,0,sizeof(factor));
        memset(num,0,sizeof(num));
        num[0]=1;
        factor[0]=jl[1];
        for(int i=2;i<=ct;i++){
            if(jl[i]!=jl[i-1]) factor[++cnt]=jl[i];
            num[cnt]++;
        }
        dfs(0,1);
        if(mina>minb) swap(mina,minb);
        printf("%lld %lld\n",mina,minb);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值