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;
}