JJery学长刚刚结束了计算两个数的最小公倍数的问题,又想转型计算复杂一些的问题,JJery学长在想,如果有n个数,我应该怎么求他的最小公倍数呢?JJery学长冥思苦想,终于算出来怎么求解了,JJery学长已经知道怎么求解了,聪明的ACMer,你们在知道?
Input
输入包含多组测试样例,输入的第一行将包含一个正整数T,表示测试样例的数量。每组样例由两行组成,第一行是一个正整数n,第二行是n个正整数。所有正整数都在int整数的范围内。
Output
对于每个样例,输出一行,表示n个正整数的最小公倍数。所有的结果int整数的范围内。
Sample Input
2
3
5 7 15
6
4 10296 936 1287 792 1
Sample Output
105
10296
题目大意:求n个数的最小公倍数。(等vjudge时间到了 补个链接)
思路:a和b的最小公倍数=a*b/a和b的最大公约数。读入0的话就没必要算下去了,虽然好像输出的数据都是正整数。
#include<iostream>
#include<cstdio>
#include<stack>
#include<cmath>
#include<cstring>
#include<queue>
#include<set>
#include<algorithm>
#include<iterator>
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
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 t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
ll temp;
ll sum;
scanf("%lld",&sum);
if(sum==0)
;
else
{
for(int i=1;i<n;i++)
{
scanf("%lld",&temp);
if(temp==0)
{
sum=0;
break;
}
sum=lcm(sum,temp);
}
}
printf("%lld\n",sum);
}
return 0;
}