Source Code
#include <iostream>
#include <cmath>

using namespace std;

int a, b, l1, r1, l2, r2, ansX, ansY;
long double aOverb, xOvery, ansXOverY = 10000000;

int main() {
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);

	cin >> a >> b >> l1 >> r1 >> l2 >> r2;

	aOverb = (long double) a / b;

	for (int x = l1; x <= r1; ++x) {
		int l = l2, r = r2, y;

		while (l <= r) {
			y = (l + r) / 2;

			xOvery = (long double) x / y;

			if (xOvery == aOverb) {
				cout << x << " " << y << endl;
				return 0;
			}
			else if (xOvery < aOverb) {
				r = y - 1;
			}
			else {
				l = y + 1;
			}

			if (abs(xOvery - aOverb) < abs(ansXOverY - aOverb)) {
				ansX = x;
				ansY = y;
				ansXOverY = (long double) x / y;
			}
		}

		if (abs(xOvery - aOverb) < abs(ansXOverY - aOverb)) {
			ansX = x;
			ansY = y;
			ansXOverY = (long double) x / y;
		}
	}

	cout << ansX << " " << ansY << endl;
}
Copy
Fractions omarthejuiceboi
GNU G++17
94 ms
748 KB
Accepted