1019 Least Common Multiple

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

 

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

思路:求多个数的最小公倍数

 1 #include<iostream>
 2 using namespace std;
 3 long long  gcd(long long  c,long long  b)
 4 {
 5     return b? gcd(b,c%b):c;
 6 }
 7 int main()
 8 {
 9     int t;
10     cin>>t;
11     while(t--)
12     {
13         int n;
14         long long a[10000];
15         cin>>n;
16         for(int i=0;i<n;i++)
17         {
18             cin>>a[i];
19         }
20         for(int i=1;i<n;i++)
21         {
22             a[i]=a[i]*a[i-1]/gcd(a[i],a[i-1]);
23         }
24         cout<<a[n-1]<<endl;
25     }
26     return 0;
27  } 

求最大公约数代码:

long long  gcd(long long  c,long long  b)
{
    return b? gcd(b,c%b):c;
} 
时间: 2024-10-10 00:33:22

1019 Least Common Multiple的相关文章

HDU 1019 Least Common Multiple (最小公倍数)

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30285    Accepted Submission(s): 11455 Problem Description The least common multiple (LCM) of a set of positive integers is

HDU 1019 Least Common Multiple 数学题解

求一组数据的最小公倍数. 先求公约数在求公倍数,利用公倍数,连续求所有数的公倍数就可以了. #include <stdio.h> int GCD(int a, int b) { return b? GCD(b, a%b) : a; } inline int LCM(int a, int b) { return a / GCD(a, b) * b; } int main() { int T, m, a, b; scanf("%d", &T); while (T--)

hdu 1019 Least Common Multiple

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 54584    Accepted Submission(s): 20824 Problem Description The least common multiple (LCM) of a set of positive integers is

HDUJ 1019 Least Common Multiple

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 Description The least common multiple (LCM) of a set of positive integers is

HDU 1019 Least Common Multiple 数学题

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 prob

HDU 1019 Least Common Multiple (最小公倍数_水题)

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 prob

杭电1019 Least Common Multiple【求最小公倍数】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1019 解题思路:lcm(a,b)=a*b/gcd(a,b) 反思:最开始提交的时候WA,以为是溢出了,于是改成了long long,还是WA,于是就不明白了,于是就去看了discuss,发现应该这样来写 lcm(a,b)=a*gcd(a,b)*b;是为了以防a乘以b太大溢出,注意啊!!!!所以就先除再乘. #include<stdio.h> int gcd(int a,int b) { int t

HDU - 1019 - Least Common Multiple - 质因数分解

http://acm.hdu.edu.cn/showproblem.php?pid=1019 LCM即各数各质因数的最大值,搞个map乱弄一下就可以了. #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ui; map<ui,ui> M; ll _pow(ui f,ui s){ ll res=1; while(s){ res*=f; s--; } retur

ACM-简单题之Least Common Multiple——hdu1019

***************************************转载请注明出处:http://blog.csdn.net/lttree*************************************** Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 28975