Ultraviolet A 11768

Source: Internet
Author: User

Ultraviolet A 11768-Lattice Point or Not

Question Link

Given two vertices to form a line segment, these points are in the form of a very bit, and the positive number points fall on this line.

Train of Thought: first form a x + B y = c for the linear table, and Form a, B, and c as integers, then use the extended gcd to find the general solutions of x and y, then we know min (x1, x2) <= x <= max (x1, x2), min (y1, y2) <= y <= max (y1, y2 ), in this way, we can find the t range in the general solution, and the integer that t can take is the integer solution. Then we get the answer.

It is worth noting that when a straight line is in a parallel coordinate system, you must make a special judgment.

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)(10 * (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 (xx1 > xx2) swap(xx1, xx2);if (yy1 > yy2) swap(yy1, yy2);if (c % d) return ans;if (b / d > 0) {down = max(down, (long 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));down = max(down, (long long)ceil((xx2 * d * 1.0 / 10 - x * c * 1.0) / b));  }  else if (xx1 % 10) 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 % 10) 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();printf("%lld\n", solve()); }return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.