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;

扫描二维码关注公众号,回复: 2330510 查看本文章

所以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

猜你喜欢

转载自www.cnblogs.com/buerdepepeqi/p/9357476.html