1. 程式人生 > >HDU_6298 Maximum Multiple 【找規律】

HDU_6298 Maximum Multiple 【找規律】

一、題目

Given an integer $n$, Chiaki would like to find three positive integers $x$, $y$ and $z$ such that: $n=x+y+z$, $x\mid n$, $y \mid n$, $z \mid n$ and $xyz$ is maximum.  InputThere are multiple test cases. The first line of input contains an integer $T$ ($1 \le T \le 10^6$), indicating the number of test cases. For each test case: 
The first line contains an integer $n$ ($1 \le n \le 10^{6}$). 
OutputFor 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

二、分析

需要先按照題意分析(20項即可),然後發現能輸出答案的都是3或者4的倍數,那麼x,y,z的結構就是1+1+1和1+1+2的模式,如果出現3和4的公倍數,為了能保證xyz的更大值,優先考慮1+1+1的結構。

三、程式碼

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;

LL Max(const LL a, const LL b)
{
    return a>b?a:b;
}
/***    找規律程式碼   ***/
void solve(int N)
{
    LL ans = -1;
    for(int i = 1; i < N; i++)
    {
        for(int j = 1; j < N; j++)
        {
            for(int k = 1; k < N; k++)
            {
                if(N%i == 0 && N%j == 0 && N%k == 0 && N == i+j+k)
                {
                    ans = Max(i*j*k, ans);
                }
            }
        }
    }
    cout << ans <<endl;

}

int main()
{
    int T, N;
    LL ans;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &N);
        if(N%3==0)
        {
           ans = (LL)(N/3)*(N/3)*(N/3);
           printf("%I64d\n", ans);
        }
        else if(N%4==0)
        {
            ans = (LL)(N/4)*(N/4)*(N/2);
            printf("%I64d\n", ans);
        }
        else
            printf("-1\n");
    }
    return 0;
}