2018HDU多校训练一 A - Maximum Multiple
Given an integer nn, Chiaki would like to find three positive integers xx, yy and zzsuch that: n=x+y+zn=x+y+z, x∣nx∣n, y∣ny∣n, z∣nz∣n and xyzxyz is maximum.
Input
There are multiple test cases. The first line of input contains an integer TT (1≤T≤1061≤T≤106), indicating the number of test cases. For each test case:
The first line contains an integer nn (1≤n≤1061≤n≤106).
Output
For each test case, output an integer denoting the maximum xyzxyz. If there no such integers, output −1−1 instead.
Sample Input
3
1
2
3
Sample Output
-1
-1
1
题意:给你一个整数n,3个整数 x,y,z.让你求满足 n=x+y+z,x*y*z的最大值;
令 a=n/x ,b=n/y, c=n/z, 则1/a+1/b+1/c=1;且a,b,c为正整数,则a,b,c可取 3 3 3,2 4 4; 2 3 6;
2 3 6的话没有取没有3 3 3大,故这个可以省去,就剩下 3 3 3; 2 4 4;先考虑是否可以被3整除,再考虑是否可以被4整除,以为3 个数和一定能话,相等时乘积最大,都不满足输出-1,都满足输出 3 3 3一组;
参考代码为:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
LL T,n,x,y,z,sum;
cin>>T;
while(T--)
{
cin>>n;
if((n%3)==0)
{
x=y=z=n/3;
sum=x*y*z;
if(x+y+z==n) cout<<sum<<endl;
else cout<<-1<<endl;
}
else if((n%4)==0)
{
x=y=n/4,z=n/2;
sum=x*y*z;
if(x+y+z==n) cout<<sum<<endl;
else cout<<-1<<endl;
}
else cout<<-1<<endl;
}
return 0;
}
最新文章
- Ninject使用介绍
- ffmpeg-20160701-git-bin.7z
- [原]零基础学习在Android进行SDL开发系列文章
- 算法分析 Analysis of Algorithms -------GeekforGeeker 翻译
- wget的下载与安装使用
- BZOJ3122 随机数生成器
- 关于css样式的看法
- C#删除数组元素代码
- apply 与arguments的用法
- 《python基础教程》笔记之 条件语句和循环语句
- 一天一个类--ArrayList之二
- [HDU1210] Eddy&#39;s 洗牌问题
- NDK开发小记录 C++读取java层对象内容
- Angular使用总结 --- 模版驱动表单
- Spark学习之路 (一)Spark初识
- js:基于原生js的上啦下啦刷新功能
- 你真的懂redis的数据结构了吗?redis内部数据结构和外部数据结构揭秘
- 姿势摆好,一招学会android的布局优化!
- java impl
- OOAD和UML