HDU 4627

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

 

Description

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=;
for(i=; i<=n/; 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 ;
}

另外还有一种,这是一种奇偶求法,很简单巧妙,经某ACM大神指点得知

#include <stdio.h>

int main()
{
int n,T;
long long max;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
max=;
if (n==) printf("1\n");
else
{
if (n%==)
{
max=n/;
if (max%==) max=(max+)*(max-);
else max=(max+)*(max-);
}
else
{
max=n/;
max=max*(max+);
}
printf("%I64d\n",max);
}
}
return ;
}

HDU 4627(最小公倍数最大问题)的更多相关文章

  1. HDU 4627 The Unsolvable Problem(简单题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4627 题目大意:给定一个整数n(2 <= n <= 109),满足a+b=n并且[a,b] ...

  2. hdu 4627 The Unsolvable Problem【hdu2013多校3签到】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4627 The Unsolvable Problem Time Limit: 2000/1000 MS ( ...

  3. HDU 1713 最小公倍数与最大公约数的问题 相遇周期

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) 相遇周期 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/ ...

  4. hdu 4627 The Unsolvable Problem

    http://acm.hdu.edu.cn/showproblem.php?pid=4627 分类讨论一下就可以 代码: #include<iostream> #include<cs ...

  5. 2013多校联合3 G The Unsolvable Problem(hdu 4627)

    2013-07-30 20:35 388人阅读 评论(0) 收藏 举报 http://acm.hdu.edu.cn/showproblem.php?pid=4627 The Unsolvable Pr ...

  6. hdu 4627 水数学题

    最小公倍数最大,也就是尽量让2个数互质,所以把n除以2 从中间向两边找就够了,自己写几组数据就能发现规律. 注意longlong存 #include<cstdio> #include< ...

  7. hdu 4627 The Unsolvable Problem(暴力的搜索)

    Problem Description There are many unsolvable problem in the world.It could be about one or about ze ...

  8. HDU 4627 The Unsolvable Problem 杭电多校联赛第三场1009 数学题

    题意描述:给出一个n,要求在所有满足n = a+b的a和b里面求a和b的最小公倍数最大的两个数的最小公倍数. 解题报告:比赛的时候看到这个题的第一反应就是寻找这两个数一定是在a和b比较接近的地方找,这 ...

  9. hdu 1788 最小公倍数(这题面。。。)

    Chinese remainder theorem again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

随机推荐

  1. openSession和getCurrentSession的比较

    在比较openSession和getCurrentSession这两个方法之前,我们先认识一下这两个方法. 在进行配置信息管理时,我们一般进行一下简单步骤: Configuration cfg = n ...

  2. c++转换构造函数和类型转换函数

    看stl源码时,有一段代码感觉很奇怪 iterator begin() { return (link_type)((*node).next); } iterator和link_type是两种不同类型, ...

  3. Java中的 修饰符

    java中的修饰符分为类修饰符,字段修饰符,方法修饰符. 根据功能的不同,主要分为以下几种. 1.权限访问修饰符  访问权限的控制常被称为具体实现的隐藏 把数据和方法包进类中,以及具体实现的隐藏,常共 ...

  4. 赵雅智_ListView_BaseAdapter

    Android界面中有时候须要显示略微复杂的界面时,就须要我们自己定义一个adapter,而此adapter就要继承BaseAdapter,又一次当中的方法. Android中Adapter类事实上就 ...

  5. js事件防止冒泡

    原文连接:http://www.cnblogs.com/jams742003/archive/2009/08/29/1556187.html 1. 事件目标 如今.事件处理程序中的变量event保存着 ...

  6. 改写URL的查询字符串QUERY_STRING(转)

    查询字符串是指URL请求中“问号”后面的部分.比如,http://www.nowamagic.net/?foo=bar中粗体部分就是查询字符串,其中变量名是foo,值是bar. 1. 利用QSA转换查 ...

  7. GridView禁止上下滚动的方法

    通常情况下,我们使用GridView来完成类似表格的布局,这种布局,我们只需要设置列数,会自动根据适配器的数据进行适配,非常灵活. GridView其实就是一个容器.允许向其内部添加控件,通常情况下, ...

  8. linux命令行计算器 <转>

    转自 http://blog.chinaunix.net/uid-26959241-id-3207711.html 详细文档请 man bc 在windows下,大家都知道直接运行calc,(c:\w ...

  9. Activity之间的数据传递(Arraylist)

    1.使用Serialiable方法 实现序列化 2.使用Parcelable方法(这是android自己封装的类) Parcel类是封装数据的容器,封装后的数据通过Intent和IPC传递     实 ...

  10. 深入理解UITableView

    基本介绍 UITableView有两种风格:UITableViewStylePlain和UITableViewStyleGrouped.这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照 ...