HDU 1019 Least Common Multiple(三个注意的点)
来源:互联网 发布:小号云源码库 编辑:程序博客网 时间:2024/05/13 15:42
1.优美的最大公约数的算法
int gcd(int a, int b) { return b?gcd(b, a%b):a;}
2.溢出问题
两个数是a,b最大公约数是p,最小公倍数是q
那么有这样的关系:ab=pq
所以q=ab/p,于是很多同学 a * b / p,这里a * b可能会溢出。只要改一下运算顺序就对了, a / p * b;
3.依次处理类的问题没必要开数组,第一次多读取一个值,然后依次往后处理即可
scanf("%d %d", &n, &temp); for (int i = 1; i < n; ++i) { scanf("%d", &x); temp = temp / gcd(temp, x) * x; }
代码如下
#include<stdio.h>int gcd(int a, int b) { return b?gcd(b, a%b):a;}int main(void){ int T, n, x, temp; scanf("%d", &T); while (T--) { scanf("%d %d", &n, &temp); for (int i = 1; i < n; ++i) { scanf("%d", &x); temp = temp / gcd(temp, x) * x; } printf("%d\n", temp); } return 0;}
阅读全文
0 0
- 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
- HDU 1019 Least Common Multiple
- HDU 1019 Least Common Multiple
- HDU 1019 Least Common Multiple
- hdu 1019Least Common Multiple
- hdu 1019 Least Common Multiple
- hdu 1019 Least Common Multiple
- Hdu 1019 Least Common Multiple
- hdu 1019 Least Common Multiple
- HDU 1019Least Common Multiple
- hdu 1019 Least Common Multiple
- hdu 1019 Least Common Multiple
- hdu 1019 Least Common Multiple
- HDU 1019 Least Common Multiple
- CMake交叉编译配置
- bzoj3376 [Usaco2004 Open]Cube Stacking 方块游戏
- 透彻理解匿名函数
- Qt使用Windows Api模拟鼠标单击双击滚轮事件
- 建造者模式
- HDU 1019 Least Common Multiple(三个注意的点)
- html中判断phone与pc的ce
- myeclipse下src/main/resources/META-INF下的资源文件不会编译到classpath下
- 【演讲实录】金融级数据库技术与实践
- 一个数组中只有两个数字是出现一次,其他所有数字都出现了两次,找出这两个数字
- 机器学习之线性回归:OLS 无偏估计及相关性python分析
- 简单说 chrome浏览器 必知必会的小技巧
- php批量生成产品编号:xxx.000001,并依次递增
- 机器学习:说说贝叶斯分类