HDU 1019 - Least Common Multiple

入门题,好多数的 LCM

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 #define LL long long
 5 int t,n;
 6 LL a[100000000];
 7 LL ans;
 8 LL gcd(LL a,LL b)
 9 {
10     if(a<b) swap(a,b);
11     LL r=a%b;
12     while(r)
13     {
14         a=b;
15         b=r;
16         r=a%b;
17     }
18     return b;
19 }
20 int main()
21 {
22     scanf("%d",&t);
23     while(t--)
24     {
25         scanf("%d",&n);
26         for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
27         if(n==1){
28              printf("%lld\n",a[1]); continue;
29         }
30         ans=a[1]*a[2]/gcd(a[1],a[2]);
31         for(int i=3;i<=n;i++)
32         {
33             ans=ans*a[i]/gcd(ans,a[i]);
34         }
35         printf("%lld\n",ans);
36     }
37 }//2016-04-22 17:12:29

 

posted @ 2016-04-22 17:15  nicetomeetu  阅读(146)  评论(0编辑  收藏  举报