BOJ/수학
[C/C++] 백준 - 5347번 : LCM
JWonK
2021. 9. 20. 22:13
728x90
반응형
https://www.acmicpc.net/problem/5347
5347번: LCM
첫째 줄에 테스트 케이스의 개수 n이 주어진다. 다음 n개 줄에는 a와 b가 주어진다. a와 b사이에는 공백이 하나 이상 있다. 두 수는 백만보다 작거나 같은 자연수이다.
www.acmicpc.net
최소공배수를 구하는 문제이다.
유클리드 호제법을 통해 최대공약수를 먼저 구한 후 두 수의 곱을 최대 공약수로 나누어주면 된다.
단, 최소 공배수의 크기가 32비트 정수형을 넘을 수 있다는 점을 주의한다.
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
#include <map>
#include <algorithm>
#include <cmath>
#define CUNLINK ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ENDL cout << endl
#define ll long long
#define INF 987654321
#define Mod 10007
#define endl '\n'
#define pil pair<int,int>
using namespace std;
ll gcd(ll x, ll y) {
if (y == 0) return x;
return gcd(y, x % y);
}
ll lcm(ll x, ll y, ll GCD) {
return x * y / GCD;
}
int main() {
int testCase;
cin >> testCase;
while (testCase--) {
ll a, b;
cin >> a >> b;
cout << lcm(a, b, gcd(a,b)) << endl;
}
return 0;
}
728x90
반응형