***************************************转载请注明出处:http://blog.csdn.net/lttree***************************************

Least Common Multiple

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

Total Submission(s): 28975    Accepted Submission(s): 10905

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
 
Source
 

这道题就是求一堆数的最小公倍数。

要注意,每一行的第一个数表示后面跟着有几个数。

然后,我的方法是将输入的数存到数组,然后进行从大到小排序。

从头開始推断,假设当前的mul(之前全部数的最小公倍数)是当前数的倍数,就不须要计算这个数了,直接跳过。

最后再注意一点,用__int64来解决,scanf是用:%I64d

/****************************************
*****************************************
* Author:Tree *
*From : blog.csdn.net/lttree *
* Title : Least Commn Multiple *
*Source: hdu 1019 *
* Hint : *
*****************************************
****************************************/ #include <stdio.h>
#include <algorithm>
using namespace std; __int64 num[10001]; // 求a和b的最小公倍数
__int64 lcm( __int64 a,__int64 b)
{
__int64 x=a,y=b,k;
while( x%y!=0 )
{
k=x%y;
x=y;
y=k;
}
return a*b/k;
}
// 从大到小排序 sort用
bool cmp(__int64 a,__int64 b)
{
return a>b;
}
int main()
{
int t,n,i;
__int64 mul; scanf("%d",&t);
while( t-- )
{
scanf("%d",&n);
for( i=0;i<n;++i )
scanf("%I64d",&num[i]);
sort(num,num+n,cmp);
mul=num[0];
// 假设当前的最小公倍数是该数的倍数就不须要计算了。
for( i=1;i<n;++i )
if( mul%num[i]!=0 )
mul=lcm(mul,num[i]);
printf("%I64d\n",mul);
}
return 0;
}

ACM-简单题之Least Common Multiple——hdu1019的更多相关文章

  1. 2道acm简单题(2010):1.猜数字游戏;2.字符串提取数字并求和;

    //第一题是猜数字的游戏.//题目:随即产生一个3位的正整数,让你进行猜数字,//如果猜小了,输出:"猜小了,请继续".//如果猜大了,输出:"猜大了,请继续" ...

  2. 2道acm简单题(2013):1.(时分秒)时间相减;2.主持人和N-1个人玩游戏,每个人说出自己认识的人数,判断其中是否有人说谎。

    /*1.题目:输入一个数,代表要检测的例子的个数,每个例子中:输入两个时间(格式HH:MM : SS),前面时间减去后面时间,输出在时钟上显示的时间,格式一样,如果是以为数字的前面补零.*//**思路 ...

  3. 3道acm简单题(2011):1.判断是否能组成三角形;2.判断打鱼还是晒网;3.判断丑数。

    //1.输入三个正整数A.B.C,判断这三个数能不能构成一个三角形.//思路:最小的两边之和是否是大于第三边#include<iostream>#include<algorithm& ...

  4. HDU1019 Least Common Multiple(多个数的最小公倍数)

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  5. Least Common Multiple (HDU - 1019) 【简单数论】【LCM】【欧几里得辗转相除法】

    Least Common Multiple (HDU - 1019) [简单数论][LCM][欧几里得辗转相除法] 标签: 入门讲座题解 数论 题目描述 The least common multip ...

  6. HDU-1019 Least Common Multiple

    http://acm.hdu.edu.cn/showproblem.php?pid=1019 Least Common Multiple Time Limit: 2000/1000 MS (Java/ ...

  7. ACM hdu 1019 Least Common Multiple

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

  8. acm.njupt 1001-1026 简单题

    点击可展开上面目录 Acm.njupt 1001-1026简单题 第一页许多是简单题,每题拿出来说说,没有必要,也说不了什么. 直接贴上AC的代码.初学者一题题做,看看别人的AC代码,寻找自己的问题. ...

  9. ACM学习历程—HDU 3092 Least common multiple(数论 && 动态规划 && 大数)

    Description Partychen like to do mathematical problems. One day, when he was doing on a least common ...

随机推荐

  1. Kendo UI开发教程(22): Kendo MVVM 数据绑定(十一) Value

    Value绑定可以把ViewModel的某个属性绑定到DOM元素或某个UI组件的Value属性.当用户修改DOM元素或UI组件的值时,绑定的ViewModel的值也随之发生改名.同样,如果ViewMo ...

  2. select的种种取值

    今天别人问我一个问题 <body> <select id="tests" onchange="test()"> <option & ...

  3. StackOverFlow的2016统计

    http://stackoverflow.com/research/developer-survey-2016

  4. 51cto大数据培训路线

    Java Java IO/NIO JVM原理与配置.调优 Socket 网络套接字技术 Java Collection java Reflection 多线程与并发编程 设计模式 Collection ...

  5. C++建立动态二维数组

    C++建立动态二维数组主要有两种方法: 1.使用数组指针,分配一个指针数组,将其首地址保存在b中,然后再为指针数组的每个元素分配一个数组                           int * ...

  6. ExtJs4 笔记(11) Ext.ListView、Ext.view.View 数据视图

    本篇介绍两个用来展示数据的容器控件,分别是Ext.ListView和Ext.view.View.Ext.ListView就是大名鼎鼎的Ext GridPanel的前身,不过现在的Ext4已经将它整合到 ...

  7. Swift - 图像控件(UIImageView)的用法

    1,使用图像控件显示图片 1 2 3 var imageView=UIImageView(image:UIImage(named:"icon")) imageView.frame= ...

  8. hadoop namanodejava

    最近突然想看下hadoop源码,有利于处理一些突发问题.先从name启动开始, NameNode.java public static void main(String argv[]) throws ...

  9. QT5.6,5.7,5.8的新特征以及展望

    https://wiki.qt.io/New_Features_in_Qt_5.6 (跨平台High-DPI,改进WebEngine到45,支持WIN 10,Canvas3D,3D) https:// ...

  10. html中的rowspan和colspan

    摘自w3school(http://www.w3school.com.cn/tags/att_td_colspan.asp)colspan 属性规定单元格可横跨的列数.<table border ...