题目1439:Least Common Multiple(求m个正数的最小公倍数lcm)
Posted 伊甸一点
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了题目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. // #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <cmath> using namespace std; //to avoid overflow thus use long long to store data long long gcd(long long a, long long b){ return b==0 ? a : gcd(b,a%b); } long long lcm(long long a, long long b){ return (a/gcd(a,b))*b;// not a*b/gcd(a,b) beacuse it may cause overflow } int main(){ int kase; scanf("%d",&kase);//kase number while(kase--){//a small tips int m; scanf("%d",&m); long long ans; scanf("%lld",&ans);// input the first number for(int i = 1 ; i < m ; i++){ long long tmp;//to store the next number scanf("%lld",&tmp); ans = lcm(ans,tmp);//calculate each two adjacent elements‘s lcm } printf("%lld\n",ans);//print the answer } return 0; }
以上是关于题目1439:Least Common Multiple(求m个正数的最小公倍数lcm)的主要内容,如果未能解决你的问题,请参考以下文章
HDU - 3092 Least common multiple (数论+背包)