Source Code
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;

int a , b , l1 , r1 , l2 , r2;

double ans = 1e18;

int ansx , ansy;

double val2;

inline void check(int x,int y){
    double val = (double)x / y;
    if(abs(val - val2) < ans){
        ans = abs(val - val2);
        ansx = x;
        ansy = y;
    }
}

int main(){
    cin >> a >> b >> l1 >> r1 >> l2 >> r2;
    val2 = (double)a / b;
    long long tmpVal;
    for(int y = l2;y <= r2;y++){
        check(l1 , y);
        check(r1 , y);
        tmpVal = (long long)a * y / b;
        for(long long j = tmpVal - 3;j <= tmpVal + 3;j++){
            if(tmpVal <= (long long)r1 && tmpVal >= (long long)l1)
                check(tmpVal , y);
        }
    }
    cout << ansx << " " << ansy << endl;

}
Copy
Fractions Kilani
GNU G++17
5 ms
664 KB
Wrong Answer