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

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


int ansx , ansy;

long long cur , cur2 , curt , curt2;


vector< long long > v , v2;

bool Comp(vector< long long > &v,vector< long long > &v2){
	bool mi1 = false , mi2 = false;
	for(int i = 0 ;i < (int)v.size();i++){
		if(v[i] < 0)
			mi1 = !mi1;
		v[i] = abs(v[i]);
	}
	for(int i = 0 ;i < (int)v2.size();i++){
		if(v2[i] < 0)
			mi2 = !mi2;
		v2[i] = abs(v2[i]);

	}
	if(mi1 && !mi2)
		return false;
	if(!mi1 && mi2)
		return true;
	vector< long long > a , b , tmp , tmp2;
	a.push_back(1);
	b.push_back(1);
	long long val;
	for(int i = 0 ;i < (int)v.size();i++){
		val = v[i];
		tmp.clear();
		while(val > 0){
			tmp.push_back(val % 10);
			val /= 10;
		}
		tmp2.clear();
		for(int i = 0 ;i < (int)tmp.size();i++){
			for(int j = 0 ;j < (int)a.size();j++){
				while((int)tmp2.size() <= i + j) tmp2.push_back(0);
				tmp2[i + j] += tmp[i] * a[j];
			}
		}
		for(int i = 0; i < (int)tmp2.size();i++){
			if(tmp2[i] >= 10){
				if(i == (int)tmp2.size() - 1) tmp2.push_back(0);
				tmp2[i + 1] += tmp2[i] / 10;
				tmp2[i] %= 10;
			}
		}
		a = tmp2;
	}
	for(int i = 0 ;i < (int)v2.size();i++){
		val = v2[i];
		tmp.clear();
		while(val > 0){
			tmp.push_back(val % 10);
			val /= 10;
		}
		tmp2.clear();
		for(int i = 0 ;i < (int)tmp.size();i++){
			for(int j = 0 ;j < (int)b.size();j++){
				while((int)tmp2.size() <= i + j) tmp2.push_back(0);
				tmp2[i + j] += tmp[i] * b[j];
			}
		}
		for(int i = 0; i < (int)tmp2.size();i++){
			if(tmp2[i] >= 10){
				if(i == (int)tmp2.size() - 1) tmp2.push_back(0);
				tmp2[i + 1] += tmp2[i] / 10;
				tmp2[i] %= 10;
			}
		}
		b = tmp2;
	}
	if((int)a.size() != (int)b.size())
		return (((int)a.size() > (int)b.size()) ^ mi1);
	for(int i = (int)a.size() - 1;i >= 0 ;i--){
		if(a[i] > b[i]) return (mi1 ^ 1);
		if(a[i] < b[i]) return mi1;
	}
	return mi1;
}


inline void check(int x,int y){
     cur = (long long)x * b - (long long)a * y;
     cur = abs(cur);
     curt = (long long)y * b;
     cur2 = (long long)ansx * b - (long long)ansy * a;
     cur2 = abs(cur2);
     curt2 = (long long)ansy * b;
     v[0] = cur;
     v[1] = curt2;
     v2[0] = cur2;
     v2[1] = curt;
    if(!Comp(v , v2)){
        //cout << x << " " << y << " better than " << ansx << " " << ansy << endl;
        ansx = x;
        ansy = y;
    }
}

int main(){
    v.resize(2);
    v2.resize(2);
    cin >> a >> b >> l1 >> r1 >> l2 >> r2;
    ansx = l1;
    ansy = l2;
    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
44 ms
712 KB
Wrong Answer