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

96 篇文章 0 订阅
GCD & LCM Inverse
Time Limit: 2000MS Memory Limit: 65536K
Total 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值