0
点赞
收藏
分享

微信扫一扫

HDU2028 Lowest Common Multiple Plus【stein算法】【水题】


Lowest Common Multiple Plus





Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


Total Submission(s): 36800    Accepted Submission(s): 15116



Problem Description


求n个数的最小公倍数。


 


Input


输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。


 


Output


为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。


 


Sample Input


2 4 6


3 2 5 7


 


Sample Output


12


70


 


Author


lcy


思路:没有用欧几里得算法来求,用的是stein算法试一下。最大公约数、

最小公倍数的欧几里得算法和stein算法参考博文:



#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

int GCD(int a,int b)
{
if(a == 0)
return b;
if(b == 0)
return a;
if(~a & 1)
{
if(b & 1)
return GCD(a>>1, b);
else
return GCD(a>>1, b>>1)<<1;
}
if(~b & 1)
return GCD(a, b>>1);
if(a > b)
return GCD((a-b)>>1, b);
return GCD((b-a)>>1,a);
}

int LCM(int a,int b)
{
return a/GCD(a,b)*b;
}
int main()
{
int N,d;
while(cin >> N)
{
int ans = 1;
for(int i = 1; i <= N; ++i)
{
cin >> d;
ans = LCM(ans,d);
}
cout << ans << endl;
}
return 0;
}



举报

相关推荐

0 条评论