1. 程式人生 > >hdu 6298 Maximum Multiple (簡單數論)

hdu 6298 Maximum Multiple (簡單數論)

sam -c limit miss hide hid 不存在 review print

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, xn, yn, zn and xyz is maximum.

Input There are multiple test cases. The first line of input contains an integer T (1T106), indicating the number of test cases. For each test case:
The first line contains an integer n (1n106).

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, xn, yn, zn,並使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%3==0)
        {
            printf("%lld\n",(n/3)*(n/3)*(n/3));
        }
        else if(n%4==0)
        {
            printf("%lld\n",(n/2)*(n/4)*(n/4));
        }
        else
        {
            printf("-1\n");
        }
    }
    return 0;
}
View Code

hdu 6298 Maximum Multiple (簡單數論)