The Unsolvable Problem

题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=45783

题目大意:

多组案例T(1<=T<=10000),每组案例输入一个数n(2 <= n <= 10 9),有a和b两个数满足a+b=n,又有[a,b]为a和b的最小公倍数,问:[a,b]最大为多少
     案例:

Sample Input

3 2 3 4

Sample Output

1 2 3

题目分析:

要注意的是,n数据过于庞大,如果采用枚举方式易超时!对于一个数n,满足条件的a、b有可能有多个,但越接近n/2,a*b越大,所以从n/2处入手,a、b不仅要满足其和为n,同时也需要两者间没有公约数。分情况讨论:

1.特殊情况n=2,输出结果1;

2.n为偶数,则又分两种情况:n/2为偶数,输出(n/2-1)*(n/2+1),

n/2为奇数,输出(n/2-2)*(n/2+2);

3.n为奇数,则输出n/2*(n/2+1)。

注:Microsoft Visual C++6.0不支持long long型,定义n时,编译采用long型,提交采用long long型!

源代码:

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int T;
 6     cin>>T;//案例数
 7     while(T--)
 8     {   long long n;
 9         cin>>n;
10         if(n==2) cout<<"1"<<endl;
11         else if(n%2==0) //判断n的奇偶性,n为偶数
12         {   if(n/2%2==0)
13                cout<<(n/2-1)*(n/2+1)<<endl;
14             else cout<<(n/2-2)*(n/2+2)<<endl;
15         }
16         else //n为奇数
17            cout<<n/2*(n/2+1)<<endl;
18     }
19     return 0;
20 }
时间: 2024-10-26 11:25:13

The Unsolvable Problem的相关文章

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 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 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

the least common multiplier

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

多校+CF简单题

A - The Unsolvable Problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description There are many unsolvable problem in the world.It could be about one or about zero.But this time it is about bigger numb

The 3n + 1 problem

The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 50648   Accepted: 16059 Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In th

poj-1207 THE 3n+1 problem

Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all po

POJ1207 The 3n + 1 problem

这题不是很难,模拟一下即可,但有一些细节需要注意 输入的数不一定升序,且要按原顺序输出,还有就是读完数据的问题 The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53006   Accepted: 16841 Description Problems in Computer Science are often classified as belonging to a certain cl

The 3n + 1 problem(杭电1032)(暴力求解)

The 3n + 1 problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23163    Accepted Submission(s): 8653 Problem Description Problems in Computer Science are often classified as belonging to a