HDU6298 Maximum Multiple(2018HDU多校联赛第一场,思路)

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,让你寻找一个满足条件的正整数:

  1. x , y , z 都是n的约数
  2. x + y + z = n
  3. 使得 x + y + z 的值最大,求最大值

首先把要求的数看成 1 份,那么组成 1 的所有可能情况是:

1 = 1 2 + 1 3 + 1 6 = 1 3 + 1 3 + 1 3 = 1 2 + 1 4 + 1 4 .

那么要把一个数分成3份,就必须按照这样的比例来分,因为是正整数,就必须可以整除这些分子,分三种情况:

  1. 分成 2 , 3 , 6 的组合,那么就要满足最小公倍数是 6 .
  2. 分成 3 , 3 , 3 的组合,那么就要满足最小公倍数是 3 .
  3. 分成 2 , 4 , 4 的组合,那么就要满足最小公倍数是 4 .

因为题目中求得是乘积最大,所以:

  • 1 2 1 3 1 6 = 1 36
  • 1 3 1 3 1 3 = 1 27
  • 1 2 1 4 1 4 = 1 32

按照贪心的策略,从大到小选,如果可以整除3,那么答案就是 n 1 3 n 1 3 n 1 3 = n n n 27 ,
然后再判断是否可以整除4,答案是 n n n 32 ,剩下的一个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;
}

猜你喜欢

转载自blog.csdn.net/riba2534/article/details/81177096
今日推荐