Problem DescriptionThe Least common multiple (LCM) of a set of positive integers are the smallest positive integer which is Divisible by all the numbers in the set. For example, the LCM of 5, 7 and 105.
Inputinput would consist of multiple problem instances. The first line of the input would contain a single integer indicating the number of problem instances. Each instance would 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 is the integers. All integers would be positive and lie within the range of a 32-bit integer.
Outputfor each problem instance, output a single line containing the corresponding LCM. All results would lie in the range of a 32-bit integer.
Sample Input
23 5 7 156 4 10296 936 1287 792 1
Sample Output
10510296
int gcd (int a,int b)//greatest common divisor template
{
if (a<b) return gcd (B,a);
int R;
while (b) {r=a%b;a=b;b=r;}
return A;
}
int LCM (int a,int b) {return a/gcd (b) *b;} Least common multiple templates
This is titled Template title ...
#include <iostream> #include <cmath> #include <cstdio>using namespace std;int gcd (int a,int b) {if (a <B) return gcd (b,a); int r; while (b) {r=a%b;a=b;b=r;} return A;} int LCM (int a,int b) {return a/gcd (b) *b;} int main () {int t,n,i,j,num,sum;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&sum); for (i=2;i<=n;i++) { scanf ("%d", &num); if (sum%num) {SUM=LCM (sum,num);}} printf ("%d\n", sum);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1019 Least Common Multiple (least common multiple _ water problem)