Monday, December 21, 2015

UVa 11428 - Cubes

// UVa 11428 - Cubes

#include <iostream>
#include <math.h>
#include <limits.h>
using namespace std;

int main() {
	int n;
	while ((cin >> n) && n) {
		int stop = n; //(int) (sqrt(n));
		int sx = 0, sy = INT_MAX;
		for (int d1 = 1; d1 <= stop; d1++)
			if (n % d1 == 0) {
				int d2 = n / d1;
				int a = 3;
				int b = -3 * d1;
				int c = d1 * d1 - d2;
				int d = b * b - 4 * a * c;
				if (d >= 0) {
					int x1 = (-b + sqrt(d)) / (2 * a);
					int x2 = (-b - sqrt(d)) / (2 * a);
					if (x1 > 0) {
						int y1 = x1 - d1;
						if (y1 > 0 && y1 < sy && x1 * x1 * x1 - y1 * y1 * y1 == n) {
							sy = y1;
							sx = x1;
						}
					}
					if (x2 > 0) {
						int y2 = x2 - d1;
						if (y2 > 0 && y2 < sy && x2 * x2 * x2 - y2 * y2 * y2 == n) {
							sy = y2;
							sx = x2;
						}
					}
				}
			}
		if (sx == 0)
			cout << "No solution" << endl;
		else
			cout << sx << " " << sy << endl;
	}
	return 0;
}

No comments:

Post a Comment