Source Code
#include <bits/stdc++.h>
using namespace std;
long double mn, a, b, l1, r1, l2, r2, x, y;

void check(int X, int Y) {
    if (Y >= l2 && Y <= r2) {
        long double cur = abs(1.0 * X / Y - a / b);
        if (cur < mn)
            mn = cur, x = X, y = Y;
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    mn = 2e18;
    cin >> a >> b >> l1 >> r1 >> l2 >> r2;

    for (int i = l1; i <= r1; i++) {
        long long y = i * b / a;
        for (int j = 0; j < 10; j++) {
            check(i, y + j);
            check(i, y - j);
            check(i, l2 + j);
            check(i, r2 - j);
        }
    }
    cout << x << ' ' << y << '\n';
}
Copy
Fractions ahmad_salah
GNU G++17
9 ms
712 KB
Wrong Answer