Source Code
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mp make_pair
#define pb push_back
#define lp(i,s,f) for(ll i = s; i < ll(f); i++)
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define endl '\n'
#define MOD 1000000007
#define mm(arr) memset(arr, 0, sizeof(arr))
#define F first
#define S second

int main(){
    FAST
    int a, b, l1, r1, l2, r2;
    cin >> a >> b >> l1 >> r1 >> l2 >> r2;
    int currX = l1;
    int currY = l2;
    for(int x = l1; x <= r1; x++){
        long double val = (long double)a/b;
        //x/y = a/b, x*b = a*y, y = x*b/a
        int More = x*b/a;
        //(l2, More) positive
        //(More + 1,R2) negative
        long double val2 = (long double)currX/currY;
        int choice = More;
        if(choice < l2){
            choice = l2;
        }
        else if(choice > r2){
            choice = r2;
        }
        for(int i = -3; i <= 3; i++){
            int newChoice = choice + i;
            if(newChoice > r2 || newChoice < l2)continue;
            long double val3 = (long double)x/newChoice;
            val2 = (long double)currX/currY;
            if(abs(val3 - val) < abs(val2 - val)){
                currX = x;
                currY = newChoice;
            }
        }
    }
    cout << currX << " " << currY;
    return 0;
}
Copy
Fractions Basilhijaz
GNU G++17
7 ms
816 KB
Wrong Answer