CCCPaste

Untitled

#include 
#include 
#include 
using namespace std;
typedef long long ll;

ll distance(ll x, ll y, ll x2, ll y2) {
    return llabs(x - x2) + llabs(y - y2);
}
int main() {
    ll w, h, c, r;
    
    cin >> w >> h >> c >> r;
    
    ll a = h / r;
    ll b = w / c;
    ll res = 1e18;
    for(ll i = a - 1; i <= a + 1; i++) {
        res = min(res, distance(w, h, i * c, i * r));
    }
    for(ll i = b - 1; i <= b + 1; i++) {
        res = min(res, distance(w, h, i * c, i * r));
    }
    cout << res << endl;
    
    
    return 0;
}