HDU 多校对抗赛 A Maximum Multiple

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

 

posted @ 2018-07-23 23:04  buerdepepeqi  阅读(126)  评论(0编辑  收藏  举报