HDU 4627(最小公倍数)

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的最大公约数,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;
}

时间: 2024-10-21 02:33:57

HDU 4627(最小公倍数)的相关文章

hdu 1019 最小公倍数

简单题 注意__int64 的使用 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 Problem : 1019 ( Least Common Multiple )     Judge Status : Accepted RunId : 10599776    Language : C++   

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

欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) 相遇周期 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2465    Accepted Submission(s): 1236 Problem Description 2007年3月26日,在中俄两国元首的见证下,中国国家航天局局长孙来燕与俄罗斯联邦航天局局长别尔米诺夫

HDU 1108: 最小公倍数

最小公倍数 #include<iostream> using namespace std; int gcd(int a, int b) { while (b) { int t = a%b; a = b; b = t; } return a; } int lcm(int a, int b) { return a / gcd(a, b)*b; } int main() { ios::sync_with_stdio(false); int a, b; while (cin >> a &g

HDU 1108: 最小公倍数(in Java)

最小公倍数 ///@author Sycamore, ZJNU ///@date 8/2/2017 import java.util.*; public class Main { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int a = scanner.nextInt(), b = scanner.nextInt();

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

Problem 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 <= 109).We should find a pair of positive integer a, b so that a + b = n and [a

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

Chinese remainder theorem again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description 我知道部分同学最近在看中国剩余定理,就这个定理本身,还是比较简单的:假设m1,m2,…,mk两两互素,则下面同余方程组:x≡a1(mod m1)x≡a2(mod m2)…x≡ak(mod mk)在0<=<m1m2…mk内有唯一

HDU 4627 The Unsolvable Problem 解题心得

原题: 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

HDU 1019 Least Common Multiple (最小公倍数)

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30285    Accepted Submission(s): 11455 Problem Description The least common multiple (LCM) of a set of positive integers is

hdu 最小公倍数

/* * hdu 最小公倍数 * date 2014/5/13 * state AC */ #include <iostream> using namespace std; int gcd(int x,int y) { while(x!=y) { if(x>y)x=x-y; else y=y-x; } return x; } int main() { //cout << "Hello world!" << endl; int a,b; whil