POJ 2429 GCD & LCM Inverse 解题报告(大数因式分解)

来源:互联网 发布:网络出版与电子出版 编辑:程序博客网 时间:2024/06/15 03:21
GCD & LCM Inverse
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 8910 Accepted: 1675

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

    解题报告:分解(b/a),将pi^ei分配到两个数里且两个数和最小。另外当lcm==gcd时,直接输出lcm,gcd。
    大数分解用Pollard rho算法。代码如下:
#include<cstdio>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<map>using namespace std;typedef long long LL;const int S=40;LL mul_mod(LL a, LL b, LL mod){    a%=mod;    b%=mod;    LL res=0;    while(b)    {        if(b&1)            res+=a, res%=mod;        a<<=1, a%=mod;        b>>=1;    }    return res;}LL pow_mod(LL a, LL b, LL mod){    a%=mod;    LL res=1;    while(b)    {        if(b&1)            res=mul_mod(res, a, mod);        a=mul_mod(a, a, mod);        b>>=1;    }    return res;}bool check(LL a, LL n, LL x, LL t){    LL res = pow_mod(a, x, n);    if(res==1) return false;    LL last=res;    for(int i=1;i<=t;i++)    {        res=mul_mod(res, res, n);        if(res==1 && last!=1 && last!=n-1) return true;        last=res;    }    if(res!=1) return true;    return false;}bool Miller_Rabin(LL n){    if(n<2) return false;    if(n==2) return true;    if((n&1)==0) return false;    LL x=n-1;    LL t=0;    while((x&1)==0) x>>=1,t++;    for(int i=0;i<S;i++)        if(check(rand()%(n-1)+1, n, x, t))            return false;    return true;}long long factor[100];//质因数分解结果(刚返回时是无序的)int tol;//质因数的个数。数组小标从0开始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, k=2;    LL x0=rand()%x;    LL y=x0;    while(1)    {        i++;        x0=(mul_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<<=1;    }}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);}LL num[500];LL powLL(LL a, int b){    LL res=1;    while(b)    {        if(b&1)            res*=a;        a*=a;        b>>=1;    }    return res;}void work(LL a, LL b){    if(a==b)    {        printf("%lld %lld\n", a, b);        return;    }    if(a>b) swap(a, b);    tol=0;    findfac(b/a);    map<LL, LL> mm;    for(int i=0;i<tol;i++) mm[factor[i]]++;    sort(factor, factor+tol);    tol=unique(factor, factor+tol)-factor;    int tol2=0;    for(int i=0;i<tol;i++)        num[tol2++]=powLL(factor[i], mm[factor[i]]);    sort(num, num+tol2);    LL pro=1;    for(int i=0;i<tol2;i++) pro*=num[i];    LL sum=1+b/a;    LL mi=1;    for(int i=1;i<(1<<(tol2-1));i++)    {        LL tmp=1;        for(int j=0;j<tol2;j++) if(i&(1<<j))            tmp*=num[j];        if(tmp+pro/tmp<sum)        {            sum=tmp+pro/tmp;            mi=min(tmp, pro/tmp);        }    }    printf("%lld %lld\n", mi*a, pro/mi*a);}int main(){    LL a, b;    while(~scanf("%lld%lld", &a, &b))        work(a, b);}



0 0
原创粉丝点击