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
思路
给你一个数n,让你寻找一个满足条件的正整数:
- 都是n的约数
- 使得 的值最大,求最大值
首先把要求的数看成 份,那么组成 的所有可能情况是:
.
那么要把一个数分成3份,就必须按照这样的比例来分,因为是正整数,就必须可以整除这些分子,分三种情况:
- 分成 的组合,那么就要满足最小公倍数是 .
- 分成 的组合,那么就要满足最小公倍数是 .
- 分成 的组合,那么就要满足最小公倍数是 .
因为题目中求得是乘积最大,所以:
按照贪心的策略,从大到小选,如果可以整除3,那么答案就是
,
然后再判断是否可以整除4,答案是
,剩下的一个6,因为可以整除6的一定可以整除3,所以就不用算了,对于其他情况,直接输出-1
代码
#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int N = 5e5 + 10;
const int inf = 0x3f3f3f3f;
int main()
{
ll t, n;
scanf("%lld", &t);
while (t--)
{
scanf("%lld", &n);
if (n % 3 == 0)
printf("%lld\n", n * n * n / 27);
else if (n % 4 == 0)
printf("%lld\n", n * n * n / 32);
else
puts("-1");
}
return 0;
}