hdu1019 Least Common Multiple(lcm)(数论)
题目链接: hdu1019 Least Common Multiple
题目大意: 求多个数的最小公倍数
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b){
return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b){
return a * b / gcd(a, b);
}
int main()
{
int m, n;
ll a;
ll b = 0; //用long long才能通过
cin >> n;
while(n--){
cin >> m;
b = 1; //定义开始的为1,如果后面只有一个数那么就是1和这个数的最小公倍数那就是那个数,所以是1.
for(int i = 0; i < m; i++){
cin >> a;
b = lcm(a, b);
}
cout << b << endl;
}
return 0;
}