Aizu 0005 GCD and LCM(欧几里得)

Aizu 0005 GCD and LCM(欧几里得)

链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0005


题目

ime Limit:1000MS Memory Limit:65536KB
Description
Write a program which computes the greatest common divisor (GCD) and the least common multiple (LCM) of given a and b.

Input
Input consists of several data sets. Each data set contains a and b separated by a single space in a line. The input terminates with EOF.

Constraints

  • 0 < a, b ≤ 2,000,000,000
  • LCM(a, b) ≤ 2,000,000,000
  • The number of data sets ≤ 50

Output
For each data set, print GCD and LCM separated by a single space in a line.

Sample Input
8 6
50000000 30000000

Sample Output
2 24
10000000 150000000


题意

求两数最大公约数。


分析

此题为欧几里得水题,熟悉gcd使用。只需要注意数据范围就行了。会爆int,建议unsigned long long


源码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<sstream>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<utility>
#include<sstream>
#define mem0(x) memset(x,0,sizeof x)
#define mem1(x) memset(x,-1,sizeof x)
#define dbug cout<<"here"<<endl;
//#define LOCAL

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6+10;
const int MOD = 1000000007;

ull n,m;

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

int main(){
    #ifdef LOCAL
        freopen("C:\\Users\\asus-z\\Desktop\\input.txt","r",stdin);
        freopen("C:\\Users\\asus-z\\Desktop\\output.txt","w",stdout);
    #endif
    while(cin >> n >> m){
        ull tmp = gcd(n, m);
        cout << tmp << " " << n*m/tmp << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值