HDU 多校对抗赛 A Maximum Multiple

本文针对给定整数n,探讨如何找出三个正整数x、y和z,使它们的和等于n且各自能整除n,同时使得x*y*z的乘积达到最大值。文章提供了详细的解题思路与C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Maximum Multiple

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 494    Accepted Submission(s): 215


Problem Description
Given an integer n, Chiaki would like to find three positive integers xy and z such that: n=x+y+zxnynzn and xyz is maximum.
 

 

Input
There are multiple test cases. The first line of input contains an integer T (1T106), indicating the number of test cases. For each test case:
The first line contains an integer n (1n106).
 

 

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,使得:n = x + y + z,x /n,y / n,z / n和xyz是最大的。


 题解:

x+y+z=n

设r=n/x,s=n/y,t=n/z

所以 1/r+1/s+1/t=1;

设 r<=s<=t;

所以r<=3

r=2     1/s+1/t=1/2;   s=t=4  | s=3 ,t=6

r=3      s=t=3; 

如果3/n的话 就分成 n/3,n/3,n/3;

n/2,n/4,n/4;

n/2,n/3,n/6; //这种的乘积小于其余的,舍弃

n可以被3整除的话就分成n/3,n/3,n/3的情况
n可以被4整除的情况就分成n/2,n/4,n/4的情况
 

代码如下

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
int main(){
  int T;
  scanf("%d",&T);
  while(T--){
     ll n;
     scanf("%lld",&n);
     if(n%3==0){
       ll x=n/3;
       ll ans=x*x*x;
       printf("%lld\n",ans);
     }
     else if (n%4==0){
       ll x=n/2;
       ll ans=x*n/4*n/4;
       printf("%lld\n",ans);
     }else{
       printf("-1\n");
     }
  }
  return 0;
}
View Code

 

转载于:https://www.cnblogs.com/buerdepepeqi/p/9357476.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值