The Unsolvable Problem

The Unsolvable Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


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] 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
 
 
题意:求满足a+b=n,a与b没有公约数(互质),a*b尽可能地大的值
 
思路:
分情况讨论:
如果n是奇数
那么你通过n/=2求得它的中间数,那么a和b就应该为n与n+1
如果n是偶数
那么先求中间数n/=2,再判断此时的这个中间数是奇数还是偶数,如果是奇数,那与它相邻的两个数肯定不互质,可以排除,所以a,b
应该为n-2和n+2;如果是偶数,那么就选n-1和n+1即可
 
代码:
 

#include"iostream"
#include"stdio.h"
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
long long n;                     //max和n都必须是long long型
long long max;
cin>>n;
if(n==2) cout<<1<<endl;
else{
if(n%2==0)
{
n/=2;
if(n%2==0)
max=(n-1)*(n+1);
else
max=(n-2)*(n+2);
}
else
{
n/=2;
max=n*(n+1);
}
cout<<max<<endl;
}
}
return 0;
}

 

你可能感兴趣的:(ab)