hdoj6298:Maximum Multiple(找規律,總結)
目錄
Maximum Multiple
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3469 Accepted Submission(s): 1429
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
題目解釋:
三個正整數x,y,z,使得x+y+z=n,且n/x,n/y,n/z都能整出,輸出xyz的最大乘積
解題思路:
舉一些例子,可以發現
3=1+1+1;xyz=1;
4=1+1+2;xyz=2;
如果n能整除3,xyz=(n/3)*(n/3)*(n/3);
如果n能整除4,xyz=(n/4)*(n/4)*(n/2);
其他情況xyz都是-1;
又一個可能出錯的點:比如12,即可以整除3也可以整除4,但是4*4*4>3*3*6,所以優先判斷是否可以整除3
ac程式碼:
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#define ll long long int
int main()
{
ll t,n;
scanf("%lld",&t);
while(t--)
{
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/4)*(n/4)*(n/2));
else
puts("-1");
}
return 0;
}