Maximum Multiple

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3313    Accepted Submission(s): 1382

Problem Description
Given an integer n, Chiaki would like to find three positive integers x, y and z such that: n=x+y+z, x∣n, y∣n, z∣n and xyz is maximum.
 
Input
There are multiple test cases. The first line of input contains an integer T (1≤T≤106), indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤106).
 
Output
For each test case, output an integer denoting the maximum xyz. If there no such integers, output −1 instead.
 
Sample Input
3
1
2
3
 
Sample Output
-1
-1
1
 

题目大意:

给你一个正整数n,要你找到三个整数x,y,z满足:n=x+y+z, x∣n, y∣n, z∣n,并使xyz最大。求最大的xyz,若不存在,则输出-1。

简单数论题。

1可以分解成这样几个形式:

1=1/3+1/3+1/3  1=1/2+1/4+1/4  1=1/2+1/3+1/6

而他们对应的乘积分别是1/27 > 1/32 > 1/36。

所以应当优先考虑n被3整除的情况,然后是被2整除,最后是被6整除。由于被3整除包含被6整除,故只需考虑前两种情况即可。注意输出用long long。

#include<cstdio>

using namespace std;

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long n;
scanf("%lld",&n);
if(n%==)
{
printf("%lld\n",(n/)*(n/)*(n/));
}
else if(n%==)
{
printf("%lld\n",(n/)*(n/)*(n/));
}
else
{
printf("-1\n");
}
}
return ;
}
05-28 21:45