Least common multiple
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 29480 accepted submission (s): 11136
Problem descriptionthe 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.
Inputinput will consist of multiple problem instances. the first line of the input will contain in 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.
Outputfor 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
23 5 7 156 4 10296 936 1287 792 1
Sample output
10510296
#include<stdio.h>int gcd(int a,int b){return b==0?a:gcd(b,a%b);}int main(){int n,a,b,i,m;scanf("%d",&m);while(m--){scanf("%d%d",&n,&a);for(i=1;i<n;i++){ scanf("%d",&b); a=a/gcd(a,b)*b;}printf("%d\n",a);}return 0;}
Hduj 1019 least common multiple