[C++] 纯文本查看 复制代码 #include <bits/stdc++.h>
using namespace std;
#define int long long
using i128 = __int128_t;
struct Node { int x1, y1, x2, y2; };
bool is_pow2(int x) { return x && !(x & (x - 1)); }
void solve() {
int a, b, x, y;
if (!(cin >> a >> b >> x >> y)) return;
if ((a == 0 && x != 0) || (b == 0 && y != 0)) {
cout << -1 << '\n';
return;
}
int k = 0;
bool ok = true;
auto upd = [&](int A, int X) {
if (A == 0) return;
int g = std::gcd(A, X);
int need = A / g;
if (!is_pow2(need)) { ok = false; return; }
k = max<int>(k, __builtin_ctzll(need));
};
upd(a, x);
upd(b, y);
if (!ok) { cout << -1 << '\n'; return; }
i128 ix = a ? ((i128)x << k) / a : 0;
i128 iy = b ? ((i128)y << k) / b : 0;
int lim = 1LL << k;
if (ix < 0 || ix > lim || iy < 0 || iy > lim) {
cout << -1 << '\n';
return;
}
bool lock_x = (a && (x == 0 || x == a));
bool lock_y = (b && (y == 0 || y == b));
vector<Node> ans;
int tx = x, ty = y;
for (int i = k - 1; i >= 0; --i) {
int qx, qy, px, py;
if (lock_x || a == 0) {
qx = (a == 0 ? 0 : (x == a ? a : 0));
px = tx;
} else {
int bx = (ix >> i) & 1;
qx = bx ? a : 0;
px = 2 * tx - qx;
}
if (lock_y || b == 0) {
qy = (b == 0 ? 0 : (y == b ? b : 0));
py = ty;
} else {
int by = (iy >> i) & 1;
qy = by ? b : 0;
py = 2 * ty - qy;
}
ans.push_back({qx, qy, px, py});
tx = px; ty = py;
}
reverse(ans.begin(), ans.end());
cout << ans.size() << '\n';
for (auto [x1, y1, x2, y2] : ans)
cout << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
|