poj2429 GCD & LCM Inverse(pollard_rho大整数分解)

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

参考代码

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
#include<stdlib.h>
#include<time.h>
using namespace std;

typedef long long ll;

ll gc,lc;
map<ll,int>mp;

ll gcd(ll a,ll b){
    if(b==0)return a;
    return gcd(b,a%b);
}

ll qmul(ll a,ll b,ll mod){
    ll res=0;
    while(b>0){
        if(b&1)res=(res+a)%mod;
        a=(a+a)%mod;
        b>>=1;
    }
    return res;
}

ll mod_pow(ll x,ll n,ll mod){
    ll res=1;
    while(n>0){
        if(n&1)res=qmul(res,x,mod);
        x=qmul(x,x,mod);
        n>>=1;
    }
    return res;
}

ll random(ll n){
    return (double)rand()/RAND_MAX*n;
}

bool miller_rabin(ll n){
    if(n==2)return true;
    if(n<2||n%2==0)return false;
    ll p=n-1;
    int k=0;
    while(p%2==0){
        p/=2;k++;
    }
    for(int i=0;i<10;i++){
        ll a=random(n-2)+1;
        ll x=mod_pow(a,p,n),y;
        for(int j=0;j<k;j++){
            y=qmul(x,x,n);
            if(y==1&&x!=1&&x!=n-1)return false;
            x=y;
        }
        if(y!=1)return false;
    }
    return true;
}

ll pollard_rho(ll n,ll c){
    ll x,y,i=1,k=2;
    x=random(n-2)+1;
    y=x;
    while(1){
        i++;
        x=(qmul(x,x,n)+c)%n;
        ll d=gcd((y-x+n)%n,n);
        if(d>1&&d<n)return d;
        if(x==y)return n;
        if(i==k){
            y=x;
            k<<=1;
        }
    }
}

void  find(ll n,ll c=120){
    if(n==1)return;
    if(miller_rabin(n)){
        mp[n]++;
        return;
    }
    ll p=n;
    ll k=c;
    while(p>=n)p=pollard_rho(n,c--);
    find(p,k);
    find(n/p,k);
}

ll r1,r2;
void dfs(map<ll,int>::iterator it,ll a,ll b){
    if(it==mp.end()){
        if(a+b<r1+r2){
            r1=a;r2=b;
        }
        return;
    }
    ll res=1,x=it->first,n=it->second;
    while(n--)res*=x;
    it++;
    dfs(it,a*res,b);
    dfs(it,a,b*res);
}

int main(){
    while(scanf("%lld%lld",&gc,&lc)!=EOF){
        mp.clear();
        find(lc/gc);
        r1=r2=(1ll<<61);
        dfs(mp.begin(),1,1);
        if(r1>r2)swap(r1,r2);
        printf("%lld %lld\n",r1*gc,r2*gc);
    }
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值