HDU 4627(最小公倍数最大问题)
2015-07-18 07:54
447 查看
HDU 4627
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
There are many unsolvable problem in the world.It could be about one or about zero.But this time it is about bigger number.
Given an integer n(2 <= n <= 10 9).We should find a pair of positive integer a, b so that a + b = n and [a, b] is as large as possible. [a, b] denote the least common multiplier of a, b.
Input
The first line contains integer T(1<= T<= 10000),denote the number of the test cases.
For each test cases,the first line contains an integer n.
Output
For each test cases, print the maximum [a,b] in a line.
Sample Input
3
2
3
4
Sample Output
1
2
3
此题的话,因为一个数n由两个正整数a+b得来,所以可以先确定a和b的范围,是从1到n/2
因为从n/2+1到n,是和前半部分重复,不用计算
然后,就用辗转相除法,a,b互质,输出最大的 LCM(a, b),即a,b的最小公倍数最大。
注意:这种算法易懂,但是很容易超时,自己做的时候就是
#include <stdio.h> int main() { int a,b,c; int max,min,t1,t2; int i,n,T; scanf("%d",&T); while(T--) { scanf("%d",&n); max=0; for(i=1; i<=n/2; i++) { a=i; b=n-i; a>b?t1=a:t1=b; t2=n-t1; while(t2) { c=t1%t2; t1=t2; t2=c; } min=a*b/t1; if(min>max) max=min; } printf("%d\n",max); } return 0; }
另外还有一种,这是一种奇偶求法,很简单巧妙,经某ACM大神指点得知
#include <stdio.h> int main() { int n,T; long long max; scanf("%d",&T); while(T--) { scanf("%d",&n); max=0; if (n==2) printf("1\n"); else { if (n%2==0) { max=n/2; if (max%2==0) max=(max+1)*(max-1); else max=(max+2)*(max-2); } else { max=n/2; max=max*(max+1); } printf("%I64d\n",max); } } return 0; }
相关文章推荐
- 核心动画
- MKNetworkKit 使用
- ACM暑期计划 —— 第三天
- 请珍惜身边的人
- 我这个站长是怎么炼成的
- Median of two Sorted Arrays
- 微软强制Win10家庭版用户自动安装所有系
- 经典排序算法分析及其Java实现
- CentOS安装Mysql5.5
- 课程设计——银行系统
- 初试zabbix基础概念
- 黑马程序员——基础学习感悟总结...
- 使用 jQuery Mobile 与 HTML5 开发 Web App —— jQuery Mobile 默认配置与事件基础
- 多线程程序设计学习(1)之多线程程序的评量标准
- 架构师速成8.2-架构师要懂产品
- 架构师速成8.2-架构师要懂产品 分类: 架构师速成 2015-07-18 07:25 232人阅读 评论(0) 收藏
- 架构师速成8.1-谈做技术人员的态度
- 架构师速成8.1-谈做技术人员的态度 分类: 架构师速成 2015-07-18 07:22 56人阅读 评论(0) 收藏
- python:任意输入3个数,判断能否组成三角形
- CentOS安装配置tengine并设置开机启动