← Real problems

yac6p3

easyinteractivemathStatement on DMOJ

CP Lang

15 lineschecks
// yac6p3 (interactive): two hidden cells; a query answers the distance to the nearer one. The four
// corners give four sums and differences; one more query tells which pairing of them is real.
let wire = Wire()
let (n, m) = read<(1..=1_000_000_000, 1..=1_000_000_000)>()
fn near(x: Int, y: Int) -> 0..=2_000_000_000 = wire.ask("? {x} {y}")
let a = near(1, 1) + 2
let b = near(n, 1) - n + 1
let c = near(1, m) - m + 1
let d = n + m - near(n, m)
let y = (a + b) / 2
let x = a - y
if (a + b) % 2 == 0 and 1 <= x and x <= n and 1 <= y and y <= m and near(x, y) == 0:
    print("!", x, y, d - (d - c) / 2, (d - c) / 2)
else:
    print("!", (a + c) / 2, a - (a + c) / 2, (d - b) / 2, d - (d - b) / 2)

Accepted C++

70 lines · 0.14 s on the judge
#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
    cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t << " | ";
    dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                           \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
         << ": ";                                          \
    dbgh(__VA_ARGS__)
#else
#define cerr   \
    if (false) \
    cerr
#define dbg(...)
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

int query(int x, int y) {
    cout << "? " << x << " " << y << endl;
    int ans;
    cin >> ans;
    return ans;
}

void answer(int x1, int y1, int x2, int y2) {
    cout << "! " << x1 << " " << y1 << " " << x2 << " " << y2 << endl;
}

void solve() {
    int n, m;
    cin >> n >> m;
    auto ibs = [&](int x, int y) -> bool {
        return 1 <= x && x <= n && 1 <= y && y <= m;
    };
    int c1 = query(1, 1), c2 = query(n, 1), c3 = query(1, m), c4 = query(n, m);
    c1 += 2;
    c2 += -n + 1;
    c3 += -m + 1;
    c4 += -n - m;
    {
        int y1 = (c1 + c2) / 2, x1 = c1 - y1;
        if (ibs(x1, y1) && !query(x1, y1)) {
            int y2 = (c3 + c4) / -2, x2 = -c4 - y2;
            answer(x1, y1, x2, y2);
            return;
        }
    }
    int x1 = (c1 + c3) / 2, y1 = c1 - x1, x2 = (c2 + c4) / -2, y2 = -c4 - x2;
    answer(x1, y1, x2, y2);
}

int main() {
    cin.exceptions(ios::failbit);
    ios_base::sync_with_stdio(false);
    solve();
}