[暑假集训--数论]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
Sample Output
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
hdu 1019
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
相关文章推荐
- HDU 1019 Least Common Multiple(数论)
- hdu 1019 Least Common Multiple(数论:求最小公倍数)
- HDU 1019 Least Common Multiple-数论(最小公倍数)
- hdu 1019 Least Common Multiple
- Least Common Multiple HDU 1019
- Least Common Multiple HDU - 1019
- HDU-1019-Least Common Multiple( 最大公约数 && 最小公倍数 && GCD )
- HDU1019 Least Common Multiple(求多个数的最小公倍数)
- HDU 1019 Least Common Multiple
- HDU - 1019 Least Common Multiple
- hdu 3092 Least common multiple(完全背包+数论)
- Least Common Multiple(hdu 1019)
- hdu 1019 Least Common Multiple
- HDU - 1019 - Least Common Multiple (lcm)
- hdu1019 Least Common Multiple
- hdu1019 Least Common Multiple
- Hdu 1019 Least Common Multiple
- HDU oj Least Common Multiple 1019
- hdu 1019 Least Common Multiple
- HDU 1019 Least Common Multiple 数学题解