• [暑假集训--数论]hdu1019 Least Common Multiple


    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. 

    InputInput 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. 
    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

    2
    3 5 7 15
    6 4 10296 936 1287 792 1

    Sample Output

    105
    10296

    a*b=gcd(a,b)*lcm(a,b)

    lcm(a,b)=a*b/gcd(a,b)

    求n个数的lcm,每加一个数进去就ans=ans/__gcd(ans,x)*x

    除法放中间是为了防爆int

     1 #include<cstdio>
     2 #include<algorithm>
     3 #define LL long long
     4 using namespace std;
     5 inline LL read()
     6 {
     7     LL x=0,f=1;char ch=getchar();
     8     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
     9     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    10     return x*f;
    11 }
    12 int T;
    13 int main()
    14 {
    15     T=read();
    16     for (int i=1;i<=T;i++)
    17     {
    18         int x=read(),now=read(),y;
    19         for (int i=2;i<=x;i++)
    20         {
    21             y=read();
    22             now=now/__gcd(now,y)*y;
    23         }
    24         printf("%d
    ",now);
    25     }
    26 }
    hdu 1019
  • 相关阅读:
    c++中的extern
    DOS性能监视器
    谈谈.NET中的内存管理(转帖)
    static_cast和dynamic_cast
    关于对EventHandler 和e的理解(转帖)
    使用Windows Mobile 6模拟器上网的步骤(转帖)
    接口抽象类类
    当前不会命中断点 尚未加载指定的模块 windows mobile
    C# 编码的双重检验锁定
    Loadrunner 监控Unix系统性能指标的解释
  • 原文地址:https://www.cnblogs.com/zhber/p/7285535.html
Copyright © 2020-2023  润新知