您的位置:首页 > 其它

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

2017-08-04 15:45 375 查看
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\n",now);
25     }
26 }


hdu 1019
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: