HDU1019 Least Common Multiple(求多个数的最小公倍数)
2014-09-01 11:02
471 查看
快速gcd
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define N 100005 using namespace std; int num ; bool cmp(int a,int b) { return a>b; } int kgcd(int a, int b) { if (a == 0) return b; if (b == 0) return a; if (!(a & 1) && !(b & 1)) return kgcd(a>>1, b>>1) << 1; else if (!(b & 1)) return kgcd(a, b>>1); else if (!(a & 1)) return kgcd(a>>1, b); else return kgcd(abs(a - b), min(a, b)); } int main() { int n,T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%d",&num[i]); } sort(num,num+n,cmp); int ans; ans=num[0]; for(int i=1;i<n;i++) { if(ans%num[i]==0) continue; ans=ans/kgcd(ans,num[i])*num[i];//注意不能写成(ans*num[i])/kgcd(ans,num[i]),int可能会溢出 } cout<<ans<<endl; } return 0; }
相关文章推荐
- HDU 1019 Least Common Multiple(求最小公倍数)
- HDU 1019 Least Common Multiple 最小公倍数 水题
- hdu 1019 Least Common Multiple(数论:求最小公倍数)
- HDU1019 Least Common Multiple 就是求最小公倍数
- hdu 1019 Least Common Multiple(几个数的最小公倍数)
- HDU-1019-Least Common Multiple( 最大公约数 && 最小公倍数 && GCD )
- HDU——1019Least Common Multiple(多个数的最小公倍数)
- 算法模板之欧几里得算法(HDU1019 Least Common Multiple)
- HDU1019:Least Common Multiple
- HDU 1019 Least Common Multiple
- hdu 1019Least Common Multiple(最大公约数)
- hdu 1019 Least Common Multiple
- HDOJ1019 Least Common Multiple(求多个数的最小公倍数)
- hdu 1019 Least Common Multiple_水题
- hdu 1019 Least Common Multiple 简单数学
- HDU 1019 Least Common Multiple(求多个数字的最小公倍数)
- Least Common Multiple (HDU 1019)
- hdu_1019 Least Common Multiple(最小公倍数)
- HDU1019 Least Common Multiple
- hdu 1019 Least Common Multiple