HDU——1019Least Common Multiple(多个数的最小公倍数)
来源:互联网 发布:那些年听过的网络歌曲 编辑:程序博客网 时间:2024/05/21 10:22
Least Common Multiple
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 42735 Accepted Submission(s): 16055
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
23 5 7 156 4 10296 936 1287 792 1
Sample Output
10510296
这题百度了下有出现n=1的情况,按我之前先取两个数得到第一个公倍数的做法会超时,n=1根本没无法输出。因此要重新写,顺便复习下gcd公式
代码:
#include<cstdio>#include<iostream>#include<algorithm>using namespace std;long long gcd(long long a,long long b){ return b?gcd(b,a%b):a;//还是记这个吧,简单易用} int main(){ int t; cin>>t; while (t--) { int n;long long a,tlcm,beg=1;//让beg初始化为1不影响结果并成为第0个数,这样一开始也就可以一个一个地求gcd scanf("%d",&n); for (int i=0; i<n; i++) { scanf("%lld",&a); beg=(beg*a)/gcd(a,beg); } cout<<beg<<endl; } return 0;}
0 0
- HDU——1019Least Common Multiple(多个数的最小公倍数)
- Least Common Multiple hdu 1019 求多个数最小公倍数
- HDOJ——1019:Least Common Multiple(求n个数的最小公倍数)
- HDOJ1019 Least Common Multiple(求多个数的最小公倍数)
- hdu 1019Least Common Multiple(最小公倍数)
- HDU1019 Least Common Multiple(求多个数的最小公倍数)
- 给你N个数求其最小公倍数(hdu 1019 Least Common Multiple)
- hdu 1019 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-数论(最小公倍数)
- HDU 1019 Least Common Multiple (最小公倍数_水题)
- HDU.1019Least Common Multiple【深搜求最小公倍数】(3.13)
- HDU 1019 Least Common Multiple(求最小公倍数)
- hdoj Least Common Multiple 1019 && 2028 (n个数的最小公倍数)
- HDU 1019.Least Common Multiple【多个数的最小公倍数】【自己的算法】【8月18】【8月19更新】
- openSUSE中文输入的安装和设置
- 多态
- java中形参长度可变的方法
- CURL模拟POST请求实例
- Yaf安装及配置
- HDU——1019Least Common Multiple(多个数的最小公倍数)
- 开源爬虫
- LightOJ 1014 - Ifter Party (**求因子)
- 了解套接字Socket
- html需要掌握的几个全局属性
- JavaScript的this分别代表什么
- 用户爬虫例子
- 爬虫基本知识
- 电脑如何连接万能钥匙解开的WiFi