CDZSC_2015寒假新人(2)——数学 A

Description

The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. 

 

Input

Input will consist of multiple problem instances. The first line of the input will contain a single integer indicating the number of problem instances. Each instance will consist of a single line of the form m n1 n2 n3 ... nm where m is the number of integers in the set and n1 ... nm are the integers. All integers will be positive and lie within the range of a 32-bit integer. 
 

Output

For each problem instance, output a single line containing the corresponding LCM. All results will lie in the range of a 32-bit integer.
 

Sample Input

2
3 5 7 15
6 4 10296 936 1287 792 1
 

Sample Output

105 10296
 

 题解 :简单的LCM,见代码。

 
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #define LL long long
 5 
 6 LL gcd(LL a, LL b)
 7 {
 8     LL r = b;
 9     while (r)
10     {
11         r = a % b;
12         a = b;
13         b = r;
14     }
15     return a;
16 }
17 
18 LL lcm(LL a, LL b)
19 {
20     return a * b / gcd(a, b);
21 }
22 
23 int main()
24 {
25 #ifdef CDZSC_OFFLINE
26     freopen("in.txt", "r", stdin);
27     freopen("out.txt", "w", stdout);
28 #endif
29     int t, n;
30     LL a, b;
31     scanf("%d", &t);
32     while (t--)
33     {
34         scanf("%d%lld", &n, &a);
35         while (--n)
36         {
37             scanf("%lld", &b);
38             a = lcm(a, b);
39         }
40         printf("%lld\n", a);
41     }
42     return 0;
43 }
View Code
原文地址:https://www.cnblogs.com/LiuACG/p/4246576.html