2018 Multi-University Training Contest 1 AMaximum Multiple

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

思路:这个题我刚开始想麻烦了,本来想通过公式变换凑出来找答案,其实是想麻烦了,就是分情况讨论被3和被4整除的情况分类讨论一下。

首先这个数

1,n/3,n/3,n/3可以

2,n/2,n/4,n/4可以

3,n/2,n/3,n/6不可以,可被1代替变得更优

....

扫描二维码关注公众号,回复: 2488227 查看本文章

讨论到最后可以发现如果能满足3及后面的,那么前两条必定更优。

int main()
{
    ll int n, t;
    while(scanf("%lld",&t)!=EOF)
    {
        while(t--)
        {
            scanf("%lld",&n);
            if(n%3==0)
            {
               n = n/3;
               printf("%lld\n",n*n*n);
            }
            else if(n%4==0) printf("%lld\n",n*n*n/32);
            else printf("-1\n");
        }
    }
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/snayf/article/details/81192970