Least common multiple
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 11716 accepted submission (s): 4278
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 input2
3 5 7 15
6 4 10296 936 1287 792 1
Sample output105
10296
Sourceeast central North America 2003, practice
Recommendjgshining
# Include < Stdio. h >
Int Gcd ( Int A, Int B)
{
If (B = 0 ) Return A;
Return Gcd (B, % B );
}
Int Main ()
{
Int T;
Int N, A, B, I;
Int CNT;
Scanf ( " % U " , & T );
While (T -- )
{
Scanf ( " % D " , & N );
CNT = A = 1 ;
For (I = 1 ; I <= N; I ++ )
{
Scanf ( " % D " , & B );
CNT = A / Gcd (A, B) * B; // Next, remove and multiply to avoid data overflow.
A = CNT;
}
Printf ( " % D \ n " , CNT );
}
Return 0 ;< BR >}