2013西安交大ACM热身赛D题

Description

Beer Lovers Club makes regular parties. They hate warm beer, but club’s refrigerator is too small to store enough beer for the whole company. So they decided to order a special super-big beer refrigerator. The new refrigerator should be a parallelepiped a × b × c and store exactly n cubical 1 × 1 × 1 beer boxes (the club has n members). To decrease losses of cold, the total area of the surface of the refrigerator must be as small as possible.

For example, if the capacity of the refrigerator must be 12, the possible variants are:

Dimensions Surface Area
3 × 2 × 2 32
4 × 3 × 1 38
6 × 2 × 1 40
12 × 1 × 1 50

The best variant in this case is 3 × 2 × 2.

Help the beer lovers to find the optimal dimensions of their new refrigerator.

Input

The input file contains single integer number n (1 ≤ n ≤ 10
6) — the capacity of the refrigerator. Help the beer lovers to find the optimal dimensions of their new refrigerator.

Output

Output three integer numbers: a, b and c — the optimal dimensions of the refrigerator. If there are several solutions, output any of them.

Sample Input

12

Sample Output

3 2 2

Hint

题目是英文的。大家自己谷歌下就好。 说说算法,实际上是穷举得到的,三围必有一围大于体积的三次根号,必有一围小于体积的三次根号。但事实上并不能保证体积是质数,或者说完全可能1 1 n这种情况。所以从0到体积的三次根号和体积的三次根号到体积之间穷举边的长度比较靠谱。

#include<stdio.h>
#include<math.h>
int main(){
	int i,j;
	long n,a,b,c,d,e,f,l[3];
	scanf("%ld",&n);
	f=1+2*n;
	d=(long)(pow(n*1.0,1.0/3.0));
	for(a=1;a<=d;a++){
		if((n%a)==0){
			e=(long)(n/a);
			for(c=d;c<=n;c++){
				if((e%c)==0){
					b=(long)(e/c);
					if(f>=(a*b+b*c+a*c)){
						f=a*b+b*c+a*c;
						l[0]=a;
						l[1]=b;
						l[2]=c;
					}
				}
			}
		}
	}
	for(i=0;i<2;i++)
		for(j=0;j<2-i;j++){
			if(l[j]<l[j+1]){
				f=l[j];
				l[j]=l[j+1];
				l[j+1]=f;
			}
		}
	printf("%ld %ld %ld\n",l[0],l[1],l[2]);
	return 0;
}
点赞