1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<long long, long long> pll; const int N=3e5+10; const int M=6e5+10; const int INF=2e9; const int p=10;
ll exgcd(ll a, ll b, ll &x, ll &y){ if(!b){ x = 1; y = 0; return a; } ll g = exgcd(b, a % b, x, y); ll t = x; x = y; y = t - a / b * y; return g; }
ll calc(pll p1, pll p2, pll p3){
ll res = (p1.first - p3.first) * (p2.second - p3.second) - (p2.first - p3.first) * (p1.second - p3.second); if(res == 0) res = 1e18; return abs(res); }
pll ans{}; ll sum = 1e18;
void run1(int x1, int y1, int x2, int y2){ ll a = abs(y1 - y2), b = abs(x1 - x2), x, y; ll g = exgcd(a, b, x, y); x %= b / g; if(x <= 0) x += b / g;
pll pi[2] = {{x1, y1}, {x2, y2}}; if(y1 < y2){ ll tm = (a * x); if(tm % b == 0) tm = tm / b - 1; else tm = tm / b; pll tp = {x1 + x, y1 + tm}; ll tmp = calc(pi[0], pi[1], tp); if(tmp < sum){ ans = tp; } }else{ ll tm = (a * x); if(tm % b == 0) tm = tm / b - 1; else tm = tm / b; pll tp = {x2 - x, y2 + tm}; ll tmp = calc(pi[0], pi[1], tp); if(tmp < sum){ ans = tp; } } }
void run2(int x1, int y1, int x2, int y2){ ll a = abs(x1 - x2), b = abs(y1 - y2), x, y; ll g = exgcd(a, b, x, y); x %= b / g; if(x <= 0) x += b / g;
pll pi[2] = {{x1, y1}, {x2, y2}}; if(y1 < y2){ ll tm = (a * x); if(tm % b == 0) tm = tm / b - 1; else tm = tm / b; pll tp = {x2 - tm, y2 - x}; ll tmp = calc(pi[0], pi[1], tp); if(tmp < sum){ ans = tp; } }else{ ll tm = (a * x); if(tm % b == 0) tm = tm / b - 1; else tm = tm / b; pll tp = {x1 + tm, y1 - x}; ll tmp = calc(pi[0], pi[1], tp); if(tmp < sum){ ans = tp; } } }
void solve(){ int x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if(x1 == x2){ printf("%d %d\n", x1 - 1, 114514); return; } if(y1 == y2){ printf("%d %d\n", 114514, y1 - 1); return; } if(x1 > x2){ swap(x1, x2); swap(y1, y2); } run1(x1, y1, x2, y2); run2(x1, y1, x2, y2); printf("%lld %lld\n", ans.first, ans.second); }
int main() { int T=1; scanf("%d", &T); for(int i=1;i<=T;i++) { solve(); } }
|