杭电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,r;
    if(a<b)
    {
        t=a;
        a=b;
        b=t;
    }
    r=a%b;
    while(r!=0)
    {

        a=b;
        b=r;
        r=a%b;
    }
    return b;
}

int main()
{
    int ncase;
    int n;
    int a;
    scanf("%d",&ncase);

        while(ncase--)
        {
            long s=1;
            scanf("%d",&n);
            while(n--)
            {
                scanf("%d",&a);
                s=s/gcd(s,a)*a;
            }
            printf("%ld\n",s);
        }

}

  

时间: 2024-10-21 21:01:43

杭电1019 Least Common Multiple【求最小公倍数】的相关文章

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

杭电 2028 ( Lowest Common Multiple Plus )

链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2028 题目要求:就是求最大公倍数,我百度了一下,最好实现的算法就是: 公式法 由于两个数的乘积等于这两个数的最大公约数与最小公倍数的积.即(a,b)×[a,b]=a×b.所以,求两个数的最小公倍数,就可以先求出它们的最大公约数,然后用上述公式求出它们的最小公倍数. 例如,求[18,20],即得[18,20]=18×20÷(18,20)=18×20÷2=180.求几个自然数的最小公倍数,可以先

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--)

题目1439:Least Common Multiple(求m个正数的最小公倍数lcm)

题目链接:http://ac.jobdu.com/problem.php?pid=1439 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: // // 1439 Least Common Multiple.cpp // Jobdu // // Created by PengFei_Zheng on 10/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. /

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

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 positiv

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 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 37254    Accepted Submission(s): 14023 Problem Description The least common multiple (LCM) of a set of positive integers is