Problem Description

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.

Input

Input 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.

Output

For 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

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
int k = a[0];
for(int i=1;i<n;i++){
int b = a[i];
if(k>b){
b=b^k;
k=b^k;
b=b^k;
}
k=su(b,k,mod(b,k));
} System.out.println(k); }
} //最小公倍数。
private static int su(int b, int k, int mod) {
return k*(b/mod);
} //求最大公约数,辗转相除法。b>k
private static int mod(int b, int k) {
while(k>0){
int m = b%k;
b=k;
k=m;
}
return b;
}
}

HDOJ 1019 Least Common Multiple(最小公倍数问题)的更多相关文章

  1. HDU 1019 Least Common Multiple【gcd+lcm+水+多个数的lcm】

    Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  2. ACM hdu 1019 Least Common Multiple

    Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...

  3. 1019 Least Common Multiple

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

  4. hdu_1019Least Common Multiple(最小公倍数)

    太简单了...题目都不想贴了 //算n个数的最小公倍数 #include<cstdio> #include<cstring> #include<algorithm> ...

  5. zoj1797 Least Common Multiple 最小公倍数

    Least Common Multiple Time Limit: 2 Seconds      Memory Limit: 65536 KB The least common multiple (L ...

  6. 杭电1019 Least Common Multiple【求最小公倍数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1019 解题思路:lcm(a,b)=a*b/gcd(a,b) 反思:最开始提交的时候WA,以为是溢出了, ...

  7. HDU 1019 Least Common Multiple 数学题解

    求一组数据的最小公倍数. 先求公约数在求公倍数.利用公倍数,连续求全部数的公倍数就能够了. #include <stdio.h> int GCD(int a, int b) { retur ...

  8. HDU 1019 Least Common Multiple GCD

    解题报告:求多个数的最小公倍数,其实还是一样,只需要一个一个求就行了,先将答案初始化为1,然后让这个数依次跟其他的每个数进行求最小公倍数,最后求出来的就是所有的数的最小公倍数.也就是多次GCD. #i ...

  9. HDU - 1019 - Least Common Multiple - 质因数分解

    http://acm.hdu.edu.cn/showproblem.php?pid=1019 LCM即各数各质因数的最大值,搞个map乱弄一下就可以了. #include<bits/stdc++ ...

随机推荐

  1. Visual C++内存泄露检测—VLD工具使用说明

    一.        VLD工具概述 Visual Leak Detector(VLD)是一款用于Visual C++的免费的内存泄露检测工具.他的特点有:可以得到内存泄漏点的调用堆栈,如果可以的话,还 ...

  2. 修改DeDe标签Pagelist分页样式

    我们在用dede仿站的时候,调用文章列表页的分页时,我们会用到: {dede:pagelist listitem=”info,index,end,pre,next,pageno” listsize=” ...

  3. 刷新的时候jquery获取checkbox是否为选中和设置选中

    $(document).ready(function(){ $('.uninstall_list_checkbox').click(function(){ if($(this).parent('.un ...

  4. (转)PHP模板smarty简单入门教程

    转之--http://blog.163.com/zf_2011@126/blog/static/166861361201062595057962/ 如何在smarty中开始我们程序设计.PHP代码:- ...

  5. Android LayoutInflater和findViewById 源码详解

    LayoutInflater大家很熟悉,简单点说就是布局文件XML解析器,setContentView函数也是调用了LayoutInflater 用法: View view = LayoutInfla ...

  6. tomcat启动项目内存溢出问题

    catalina.bat文件的第二行加下面的即可: 注意最大内存设置,和系统的内存有关系 set JAVA_OPTS=%JAVA_OPTS% -Xms512m -Xmx1024m -XX:PermSi ...

  7. 【转】 iOS 两种方法实现左右滑动出现侧边菜单栏 slide view

      原文: http://blog.csdn.net/crayondeng/article/details/9057637 --- 关于评论中,很多网友都是需要这部分的相关源码,其实在我上传的新浪微博 ...

  8. 一些iOS笔试题目

    1.什么是arc?(arc是为了解决什么问题诞生的?) 首先解释ARC: automatic reference counting自动引用计数. ARC几个要点: 在对象被创建时 retain cou ...

  9. iOS中使用UIWebView与JS进行交互

    iOS中使用UIWebView与JS进行交互 前一段忙着面试和复习,这两天终于考完试了,下学期的实习也有了着落,把最近学的东西更新一下,首先是使用UIWebView与JS进行交互 在webView中我 ...

  10. [逼死强迫症 - C&C++设计风格选择.1] : 命名规范

    1.命名规范 本系列的第一篇,命名风格本就是有关艺术审美,没有美与丑的绝对标准,本文难免带有主观选择倾向,但是会尽量保持客观的态度归纳几种主流的命名风格,仅供参考.制定规范是为了方便团队沟通和利于代码 ...