Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
2.3 kB
4
Indexable
Never
#include "bits/stdc++.h"
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x...)
#endif

using db = long double;

constexpr db EPS = 1e-8;

bool equal(db a, db b) {
    return fabs(a - b) < EPS;
}

bool is_int(db n) {
    int r = round(n);
    if (n - EPS <= r && n + EPS >= r) {
        return true;
    } else {
        return false;
    }
}

void solve() {
    int n, m, k, xO, yO, xA, yA;
    cin >> n >> m >> k >> xO >> yO >> xA >> yA;
    int ans = INT_MAX;
    for (int i = -200; i <= 200; i++) {
        for (int j = -200; j <= 200; j++) {
            int curxA = xA, curyA = yA;
            if (i & 1) {
                curxA = n - xA;
            }
            if (j & 1) {
                curyA = m - yA;
            }
            curxA += i * n;
            curyA += j * m;
            int dx = curxA - xO, dy = curyA - yO;
            if (i >= 0) {
                for (int k1 = i; k1 <= 200; k1++) {
                    db t = 1.0 * (k1 * n - curxA) / dx;
                    //if (t < 0) continue;
                    db k2 = 1.0 * (curyA + dy * t) / m;
                    if (j >= 0) {
                        if (k2 < j) continue;
                    } else {
                        if (k2 > j) continue;
                    }
                    if (is_int(k2)) {
                        ans = min(ans, abs(k1) + abs((int)round(k2)));
                    }
                }
            } else {
                for (int k1 = -200; k1 <= i; k1++) {
                    db t = 1.0 * (k1 * n - curxA) / dx;
                    //if (t < 0) continue;
                    db k2 = 1.0 * (curyA + dy * t) / m;
                    if (j >= 0) {
                        if (k2 < j) continue;
                    } else {
                        if (k2 > j) continue;
                    }
                    if (is_int(k2)) {
                        ans = min(ans, abs(k1) + abs((int)round(k2)));
                    }
                }
            }
        }
    }
    cout << (ans <= k ? ans : -1);
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int tests = 1;
    // cin >> tests;
    while (tests--) {
        solve();
    }
    return 0;
}