Least Common Multiple HDU 1019(一列数的最小公倍数)
来源:互联网 发布:知世公主 编辑:程序博客网 时间:2024/04/30 12:59
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.
23 5 7 156 4 10296 936 1287 792 1
10510296
题意: 就是求一列数的最小公倍数;
思路:就是求前两个数的最大公倍数a,再让a与第三个数求最大公倍数再赋予a,这是时候的a就是,前三个数的最大公倍数,依次类推;
在求a,b的最大公倍数时,有一个小技巧,避免溢出 a/gcd(a,b)*b 先除再乘;避免数的溢出;
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int gcd(int a,int b){return b==0?a:gcd(b,a%b);}int main(){int i,j,t,n;scanf("%d",&t);while(t--){scanf("%d",&n);int a=1,b;for(i=0;i<n;i++){scanf("%d",&b);a = a/(gcd(a,b))*b; // 先除再乘,避免数的溢出; }printf("%d\n",a);}return 0;}
阅读全文
0 0
- Least Common Multiple HDU 1019(一列数的最小公倍数)
- hdu 1019 Least Common Multiple(几个数的最小公倍数)
- hdu 1019 Least Common Multiple(最小公倍数)
- hdu 1019Least Common Multiple(最小公倍数)
- HDU 1019 Least Common Multiple 最小公倍数 水题
- hdu 1019 Least Common Multiple 最小公倍数
- 三个数的最小公倍数LCM (least common multiple)
- hdoj1019 Least Common Multiple(一组数的最小公倍数)
- Least Common Multiple hdu 1019 求多个数最小公倍数
- 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( 最大公约数 && 最小公倍数 && GCD )
- HDU.1019Least Common Multiple【深搜求最小公倍数】(3.13)
- HDU 1019 Least Common Multiple(求最小公倍数)
- HDU——1019Least Common Multiple(多个数的最小公倍数)
- HDOJ 1019 Least Common Multiple(最小公倍数问题)
- 【NodeJS】浅析 exports 和 module.exports
- psd里面如何切图
- Spark
- Android Studio导入Eclipse工程 encoding报错
- (二)java框架篇笔记库(21)
- Least Common Multiple HDU 1019(一列数的最小公倍数)
- BZOJ4873: [Shoi2017]寿司餐厅
- mybatis官网参考文档-XML配置_properties(版本: 3.4.6-SNAPSHOT)
- 所谓的原创内容对于seo的意义在何方?
- svn bad interpreter: No such file or directory
- 关于JVM的简单介绍
- 关于MySQL异常捕捉那些事儿
- 译文 | 与TensorFlow的第一次接触 第五章:多层神经网络
- String类能否被继承?