Source Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void lets_hope_its_right(){
    double a,b;
    int l1,r1,l2,r2;
    cin>>a>>b>>l1>>r1>>l2>>r2;
    pair<int,int>ans;
    double cur=DBL_MAX;
    for(double i=l1;i<=r1;i++){
        int l=l2,r=r2;
        while(l<=r){
            double x=i;
            int y=l+(r-l)/2;
            if(x/y==a/b){
                cout<<x<<' '<<y;
                return;
            }
            else if(x/y<a/b){
                if(abs(x/y-a/b)<cur){
                    cur=abs(x/y-a/b);
                    ans.first=x;
                    ans.second=y;
                }
                r=y-1;
            }
            else{
                if(abs(x/y-a/b)<cur){
                    cur=abs(x/y-a/b);
                    ans.first=x;
                    ans.second=y;
                }
                l=y+1;
            }
        }
    }
    cout<<ans.first<<' '<<ans.second;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int tt=1; //cin>>tt;
    while(tt--) lets_hope_its_right();
}
Copy
Fractions YazanIstatiyeh
GNU G++17
39 ms
880 KB
Accepted