#include <stdio.h> #include <math.h> #include <algorithm> using namespace std; long long gcd(long long x, long long y) { long long t; while (x%y) t = x, x = y, y = t%y; return y; } long long exgcd(long long x, long long y, long long &a, long long &b) { int flag = 0; long long t, la = 1, lb = 0, ra = 0, rb = 1; while(x%y) { if(flag == 0) la -= x/y*ra, lb -= x/y*rb; else ra -= x/y*la, rb -= x/y*lb; t = x, x = y, y = t%y; flag = 1 - flag; } if(flag == 0) a = ra, b = rb; else a = la, b = lb; return y; } long long countSolution(long long n1, long long n2, long long n, long long lx, long long rx, long long ly, long long ry) { if (lx > rx || ly > ry) return 0; if (n1 == 0) return rx - lx + 1; if (n2 == 0) return ry - ly + 1; long long a, b, g; g = exgcd(n1, n2, a, b); if(n%g) return 0; long long k = n/g, k1, k2; a *= k, b *= k; k1 = n1*n2/g/n1, k2 = n1*n2/g/n2; long long x1, x2, x3, x4, y1, y2, y3, y4; k = (a - lx)/k1; a -= k*k1, b += k*k2; while (a < lx) { if (k1 < 0) a -= k1, b += k2; else a += k1, b -= k2; } x1 = a, y1 = b; k = (b - ly)/k2; a += k*k1, b -= k*k2; while (b < ly) { if (k2 < 0) a += k1, b -= k2; else a -= k1, b += k2; } x3 = a, y3 = b; k = (a - rx)/k1; a -= k*k1, b += k*k2; while (a > rx) { if (k1 < 0) a += k1, b -= k2; else a -= k1, b += k2; } x2 = a, y2 = b; k = (b - ry)/k2; a += k*k1, b -= k*k2; while (b > ry) { if (k2 < 0) a -= k1, b += k2; else a += k1, b -= k2; } x4 = a, y4 = b; long long l1 = 0, r1 = (x2 - x1)/ k1; long long l2 = (x3 - x1)/k1, r2 = (x4 - x1)/k1; if (l2 > r2) swap(l2, r2); if (x1 > x2 || y3 > y4) return 0; l1 = max(l1, l2), r1 = min(r1, r2); if (l1 <= r1) return r1 - l1 + 1; return 0; } long long read1Float() { long long x, y; scanf("%lld.%lld", &x, &y); return x * 10 + y; } int main() { int testcase, cases = 0; long long sx, sy, ex, ey; scanf("%d", &testcase); while (testcase--) { sx = read1Float(); sy = read1Float(); ex = read1Float(); ey = read1Float(); long long minx, miny, maxx, maxy; minx = ceil(min(sx, ex)/10.0); maxx = floor(max(sx, ex)/10.0); miny = ceil(min(sy, ey)/10.0); maxy = floor(max(sy, ey)/10.0); long long a, b, c; a = (ey - sy), b = (sx - ex), c = (a * sx + b * sy); a = a * 10, b = b * 10; printf("%lld\n", countSolution(a, b, c, minx, maxx, miny, maxy)); } return 0; }
|