UVA 11768-lattice Point or not
option=com_onlinejudge&itemid=8&page=show_problem&category=516&problem=2868&mosmsg= submission+received+with+id+13823461 "target=" _blank "style=" "> Topic Links
Test instructions: Given two points, form a line segment. These points are in a very bit form, and they are the positive points that fall on this line.
Idea: First, the line is expressed as a x + b y = C form, A, B, C are all represented as integers. Then use the extended gcd to find the general solution of X and Y, then known as min (x1, x2) <= x <= max (x1, x2), min (y1, y2) <= y <= max (y1, y2). In this way, the range of T in the general solution can be obtained, and the integer t can be the integer solution. Will get the answer.
It's worth noting that. The case where the line is a parallel coordinate system. Make a special inference.
Code:
#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm>using namespace Std;const Long Long INF = 0x3f3f3f3f3f3f3f;int T;long long xx1, yy1, xx2, Yy2;long long A, B, C;long Long read () {double T; scanf ("%lf", &t); return (long Long) (Ten * (T + 0.05));} Long Long gcd (long long A, long long B) {if (!b) return A;return gcd (b, a% b);} Long Long EXGCD (long long A, long long B, long long &x, long long &y) {if (!b) {x = 1; y = 0; return A;} Long Long d = EXGCD (b, a% B, y, x); y-= A/b * X;return D;} void Build () {a = (yy2-yy1) * 10;b = (xx1-xx2) * 10;c = (yy2-yy1) * xx1 + (xx1-xx2) * Yy1;long Long T = gcd (GCD (A, b), c); a/= t; b/= t; C/= t;} Long long Solve () {long long ans = 0;long long x, y;long long d = EXGCD (A, B, X, y); long long up = INF, down =-inf;if (xx 1 > xx2) Swap (xx1, xx2), if (Yy1 > Yy2) Swap (yy1, yy2), if (c% d) return ans;if (b/d > 0) {down = max (lon G long) Ceil ((xx1 * d * 1.0/10-x * c * 1.0)/b)); up = Min (up, (Long Long) floor ((XX2 * d * 1.0/10-x * c * 1.0)/b)); } else if (b/d < 0) {up = min (up, long Long) floor ((XX1 * d * 1.0/10-x * c * 1.0)/b));d own = max (long l ONG) Ceil ((XX2 * d * 1.0/10-x * c * 1.0)/b)); } else if (xx1%) return ans; if (A/d > 0) {down = max (down, (Long Long) ceil ((Y * c * 1.0-d * yy2 * 1.0/10)/a)); up = min (up, (Long Long) floor ((Y * c * 1.0-d * yy1 * 1.0/10)/a)); } else if (A/d < 0) {up = min (up, (Long Long) floor ((Y * c * 1.0-d * yy2 * 1.0/10)/a)); down = Max (down, (Long Long) ceil ((Y * c * 1.0-d * yy1 * 1.0/10)/a)); } else if (yy1%) return ans; if (down <= up) ans + = Up-down + 1;return ans;} int main () {scanf ("%d", &t), while (t--) {xx1 = read (); yy1 = read (); xx2 = read (); yy2 = read (); build ();p rintf ("%lld\n ", Solve ());} return 0;}
UVA 11768-lattice point or not (number theory)