题目链接:Maximum Multiple
题意
给定一个整数 n n ,找到三个整数 ,要求满足 x+y+z=n x + y + z = n 且 x,y,z x , y , z 都能整除 n n ,要使得 的值最大,求 xyz x y z 的最大值。
输入
第一行为一个整数 T (1≤T≤106) T ( 1 ≤ T ≤ 10 6 ) ,表示数据组数,接下去 T T 行每行一个整数 。
输出
如果存在满足条件的 x,y,z x , y , z 的值,输出其中 xyz x y z 的最大值,如果不存在满足条件的值,则输出 −1 − 1 。
样例
输入 |
---|
3 1 2 3 |
输出 |
-1 -1 1 |
题解
暴力打表找规律,可以发现如果 n n 是 的倍数,那么 x,y,z x , y , z 的值一定都是 n3 n 3 ,如果 n n 是 的倍数,那么 x,y,z x , y , z 其中一个数是 n2 n 2 ,另外两个数是 n4 n 4 ,其他情况都输出 −1 − 1 。
过题代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
#define LL long long
int T;
LL n, a, b, c, ans;
int main() {
#ifdef Dmaxiya
freopen("test.txt", "r", stdin);
#endif // Dmaxiya
ios::sync_with_stdio(false);
scanf("%d", &T);
while(T--) {
scanf("%I64d", &n);
a = b = c = -1;
if(n % 3 == 0) {
a = b = c = n / 3;
} else if(n % 4 == 0) {
a = b = n / 4;
c = a * 2;
}
if(a == -1) {
printf("-1\n");
} else {
printf("%I64d\n", a * b * c);
}
}
return 0;
}