***************************************转载请注明出处: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. 基于TCP/IP协议的C++网络编程(API函数版)

    源代码:http://download.csdn.net/detail/nuptboyzhb/4169959 基于TCP/IP协议的网络编程 定义变量——获得WINSOCK版本——加载WINSOCK库 ...

  2. BZOJ 3110([Zjoi2013]K大数查询-区间第k大[段修改,在线]-树状数组套函数式线段树)

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec   Memory Limit: 512 MB Submit: 418   Solved: 235 [ Submit][ ...

  3. BSGS_Baby steps giant steps算法

    BSGS这个主要是用来解决这个题: A^x=B(mod C)(C是质数),都是整数,已知A.B.C求x. 在具体的题目中,C一般是所有可能事件的总数. 解: 设m = ceil(sqrt(C))(ce ...

  4. Linux教程:如何在Linux下进行C++开发?

    Linux是一类Unix计算机操作系统的统称,Linux操作系统的内核的名字也是“Linux”, 在Linux下进行C++开发,需要注意许多问题,比如:减少不必要的编辑动作,减少编辑的时间. Wind ...

  5. Swift - 列表项尾部附件点击响应(感叹号,箭头等)

    列表单元格尾部可以添加各种样式的附件,如感叹号,三角箭头等.而且点击内容区域与点击附件的这两个响应事件是不同的,这样可以方便我们实现不同的功能(比如点击内容则查看详情,点击感叹号则编辑) 1 2 3 ...

  6. Nginx设置alias实现虚拟目录 alias与root的用法区别

    Nginx 貌似没有虚拟目录的说法,因为它本来就是完完全全根据目录来设计并工作的.如果非要给nginx安上一个虚拟目录的说法,那就只有alias标签比较"像",干脆来说说alias ...

  7. 全局忽略编译警告(设置QMAKE_CXXFLAGS )

    msvc编译器从2010 sp1开始就已经支持UTF-8的源码文件了,然后到vs2012又不支持了,官方表示是BUG.到目前最新的vs2013就解决了这个问题... 但是在编译时仍然会出现4819的警 ...

  8. 与众不同 windows phone (25) - Input(输入)之捕获 UIElement 之外的触控操作, Silverlight 方式捕获手势操作, XNA 方式捕获手势操作, 多点触控

    原文:与众不同 windows phone (25) - Input(输入)之捕获 UIElement 之外的触控操作, Silverlight 方式捕获手势操作, XNA 方式捕获手势操作, 多点触 ...

  9. 当Scheduler拿不到url的 时候,不能立即退出

    在webmagic的多线程抓取中有一个比较麻烦的问题:当Scheduler拿不到url的 时候,不能立即退出,需要等到没抓完的线程都运行完毕,没有新url产生时,才能退出.之前使用Thread.sle ...

  10. 【矩阵乘】【NOI 2012】【cogs963】随机数生成器

    963. [NOI2012] 随机数生成器 ★★ 输入文件:randoma.in 输出文件:randoma.out 简单对照 时间限制:1 s 内存限制:128 MB **[问题描写叙述] 栋栋近期迷 ...