HDU - 2018 Multi-University Training Contest 1 - 1001: Maximum Multiple

Maximum Multiple

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

 

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:1 1 1
4:1 1 2
如果有解,一定满足 n%3==0 || n%4==0,否则输出 -1。
若是:n%3==0,n/3 n/3 n/3;
若是:n%4==0,n/4 n/4 n/2;
而且 n%3 的情况要比 n%4 大。

 

AC 代码

#include
#include

#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f

using namespace std;

typedef long long ll;

int main()
{
    int T; scanf("%d",&T);
    ll n;
    while(T-- && ~scanf("%lld",&n))
    {
        ll rs;
        if(n%3==0) rs=n/3*n/3*n/3;
        else if(n%4==0) rs=n/2*n/4*n/4;
        else rs=-1;
        printf("%lld\n",rs);
    }

    return 0;
}

你可能感兴趣的:(#,ACM,#,HDU,#,数论)