Maximum Multiple

Maximum Multiple

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,Chiaki想找到三个正整数x,y,z,这样:n = x + y + z,x∣n,y∣n z∣n和xyz是最大的。

输入

有多个测试用例。输入的第一行包含一个整数T(1≤T≤106),显示测试用例的数量。为每个测试用例:

第一行包含一个整数n(1≤n≤106)。

输出

对于每个测试用例,输出一个表示最大xyz的整数。如果没有这样的整数,输出−1代替。

样例输入

3

1

2

3

样例输出

-1

-1

1

思路

按照题目意思我们可以设三个数 i,j,h,使这三个数满足条件,那么可知道
n=i+j+h
h=n-i-j
n/i为整数
n/j为整数
n/h为整数
这样我们可以用两个for循环镶套

for(i=1;i<n;i++)
{
        .
        .
        .
        for(j=1;j<n-i;j++)
        {
            .
            .
            .
            h=n-i-j;
        }
        .
        .
        .

}

完整代码如下(这个代码会超时)

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

int main()
{
    int n,s,i,j,k;
    long long int mmax;
    cin >> n;
    while(n--)
    {
        mmax=0;       //用于保留最大值
        cin >> s;
        for(i=1;i<s;i++)   //求i值
        {
            if(s%i==0)     //判断是否满足n/i为整数
            {
                for(j=1;j<s-i;j++)//求j值
                {
                    if(s%j==0)   //判断是否满足n/j为整数
                    {
                        k=s-i-j; // k值为s-i-j
                        if(s%k==0&&k!=0) //判断是否满足n/j为整数
                        {
                            if(mmax<i*j*k)  //如满足判断i*j*k是否为最大值
                                mmax=i*j*k;
                        }
                    }
                }
            }
        }
        if(mmax==0)     //没有最大值说明不存在解
        cout << "-1" <<endl;
        else
            cout << mmax << endl;
    }
    return 0;
}

但会发现 这样提交会超时 但是我们可以通过这个程序来做测试
得到以下数据
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

由以上数据我们可以得到规律

每12个数形成一个循环,在每个循环里 第1,2,5,7,10,11个数会是-1
每个循环中答案不是-1的数 都是3或者4的倍数
凡是3的倍数的数x 答案为(x/3)*(x/3)*(x/3)
凡是4的倍数的数x答案为(x/4)*(x/4)*(x/4)*2
优先判断是否为三的倍数

因此我们可以得到优化后的代码(不能用cin/cout否则也会超时)

正确代码

#include<stdio.h>
int main()
{
    long long int n, s;
    long long int mmax;
    scanf("%d", &n);
    while (n--)
    {
        mmax = 0;
        scanf("%lld", &s);
        if (s % 12 == 1 || s % 12 == 2 || s % 12 == 5 || s % 12 == 7 || s % 12 == 10 || s % 12 == 11)
            printf("-1\n");
        else
        {
            if (s % 3 == 0)
                mmax = (s / 3)*(s / 3)*(s / 3);
            else
                mmax = (s / 4)*(s / 4)*(s / 4) * 2;
            printf("%lld\n", mmax);
        }
    }
    return 0;
}

wa了多遍 才得到的 希望各位亲能看明白 欢迎留言提问

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值