杭电ACM 1019 Least Common Multiple - neuxxm's Blog - 本为贵公子,平生实爱才。感时思报国,拔剑起蒿莱。

杭电ACM 1019 Least Common Multiple

neuxxm posted @ 2011年5月20日 00:51 in 杭电ACM 解题报告 with tags 最大公约数 , 2110 阅读

Least Common Multiple

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10956    Accepted Submission(s): 3953



 

Problem 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

 

#include <stdio.h>
int gcd(int x, int y){
    if (y == 0){
        return x;
    }
    int i = x&1, j = y&1;
    return i ? j ? (x>y ? gcd(y, x-y) : gcd(x, y-x)) : gcd(x, y>>1): j ? gcd(x>>1, y) : gcd(x>>1, y>>1)<<1;
}
int lcm(int x, int y){
    return x/gcd(x, y)*y;
}
int main(){
    int z, n, t, r;
    scanf("%d", &z);
    while (z-- != 0){
        scanf("%d", &n);
        scanf("%d", &r);
        while (--n != 0){
            scanf("%d", &t);
            r = lcm(r, t);
        }
        printf("%d\n", r);
    }
    return 0;
}
  • 无匹配
  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee