1. 程式人生 > >GCD&LCM

GCD&LCM

Description 

Consider 2 integers a,b,and gcd(a,b)=n,lcm(a,b)=m.Now give you 2 integers n,m,there may exist multiple (a,b) meet requirements,Please tell me the pair which has the minimum sum.

Input 

The input consists of multiple test cases. Each contains 2 integers n,m.(0 < n ,m<=10^12)。

Output 

For each case,output 2 integer x,y.(x<=y).The pair which has the minimum sum.

3 60

12 15

題目連結

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef long long ll;
const ll INF=1e13;
ll N, M, p, q;      //gcd==N; lcm==M; gcd(pN, qN)==N, lcm(pN, qN)==M
ll little, large, ans_little, ans_large;
ll gcd(ll a, ll b)
{
    return b==0?a:gcd(b, a%b);
}
int main()
{
    while(scanf("%lld%lld", &N, &M)!=EOF)
    {
        if(M%N) continue;
        little=large=ans_large=ans_little=INF;
        M/=N;
        for(ll i=1; i*i<=M; i++)
        {
            if(M%i==0 && gcd(i, M/i)==1)
            {
                ans_little=i;
                ans_large=M/i;
            }
        }
        printf("%lld %lld\n", ans_little*N, ans_large*N);
    }
    return 0;
}