POJ 2429 GCD & LCM Inverse(Miller-Robbin算法+Pollard Rho算法)

传送门:http://poj.org/problem?id=2429

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(a,b),lcm(a,b),求解 使a+b的值 最小的 a,b;

思路

  令:g = gcd(a,b);l = lcm(a,b);
   l g {{l} \over {g}} gl = a g {{a} \over {g}} ga * b g {{b} \over {g}} gb,很明显 a g {{a} \over {g}} ga b g {{b} \over {g}} gb是互质的,所以搜索 l g {{l} \over {g}} gl 的所有质因子组成的数,找寻和最小的那一对(m,n),然后输出(mg,ng)即可。
   注意这里找寻的是所有质因子,所以某种质因子可能有多个,又因为保证 a g {{a} \over {g}} ga b g {{b} \over {g}} gb互质,所以就把相同的质因子合并(相乘)

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<ctime>
#define ll long long
#define ld long double
#define ull unsigned long long
using namespace std;
const int maxn = 1000010;
const ll lnf = 0x3f3f3f3f3f3f3f;
int t,tot;
ll a[] = {2,3,5,7,11,61,13,17};
ll g,l,p,ans[maxn];
int nmb[maxn];
ll fl1,fl2,_min;
ll mul(ll x,ll y,ll z)
{
    ll sm=(ld)x/z*y;
    return ((ull)x*y-(ull)sm*z+z)%z;
}
ll ksm(ll x,ll y,ll z)
{
  ll ans=1;
  x %= z;
  for(ll i=y;i;i>>=1){
        if(i&1) ans=mul(ans,x,z);
        x=mul(x,x,z);
    }
    return ans%z;
}
ll gcd(ll x,ll y)
{
    return y==0 ? x : gcd(y,x%y);
}
bool rabin(ll x,ll y)
{
    if(ksm(x,y-1,y)!=1)
        return 0;
    ll z=y-1,sm;
    while(!(z&1)){
        z>>=1;
        sm=ksm(x,z,y);
        if(sm!=1&&sm!=y-1)  return 0;
        if(sm==y-1) return 1;
    }
    return 1;
}
bool miller(ll x)
{
    bool ans = true;
    for(int i=0;i<8;i++){
        if(a[i]==x) return 1;
        ans &= rabin(a[i],x);
    }
    return ans;
}
ll Pollard(ll x)
{
    ll a,b,d,g,y,i,j;
    while(1){
        a = b = rand()%x;
        g = rand()%x;
        y=1;i=0;j=1;
        while(++i){
            a = (mul(a,a,x)+g) % x;
            if(a>b) y = mul(y,a-b,x);
            else    y = mul(y,b-a,x);
            if(a==b||!y)    break;
            if(i<127||i==j){
                d=gcd(x,y);
                if(d>1&&d!=x)   return d;
                if(i==j){
                    b=a;
                    j<<=1;
                }
            }
        }
    }
}
void Find(ll x)
{
    if(x<=1)    return ;
    if(miller(x)){
        ans[tot++] = x;
        return;
    }
    ll y=Pollard(x);
    if(x%y==0)   x/=y;	//这里找寻的是所有质因子,
    //while(x%y==0)   x/=y;	//找寻种类
    Find(y);Find(x);
}
void DFS(int pos,ll a,ll b)
{
    if(pos>=tot){
        if(a+b<_min){
            _min = a+b;
            fl1 = a;
            fl2 = b;
        }
        return ;
    }
    DFS(pos+1,a*ans[pos],b);
    DFS(pos+1,a,b*ans[pos]);
}
int main()
{
    ll pp;
    int cnt;
    while(~scanf("%lld%lld",&g,&l)){
        if(g==l){	//特判
            printf("%lld %lld\n",g,l);
            continue;
        }
        p = l/g;
        tot = 0;
        Find(p);
        sort(ans,ans+tot);
        int j = 0;
        for(int i=0;i<tot;i++){	//相同的质因子合并
            ll tmp = ans[i];
            while(i+1<tot&&ans[i]==ans[i+1]){
                tmp *= ans[i];
                i++;
            }
            ans[j++] = tmp;
        }
        tot = j;
        _min = lnf;
        DFS(0,1,1);	//搜索
        if(fl1>fl2) swap(fl1,fl2);	
        printf("%lld %lld\n",fl1*g,fl2*g);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逃夭丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值