UVA 11768 - Lattice Point or Not(数论)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA 11768 - Lattice Point or Not(数论)相关的知识,希望对你有一定的参考价值。

UVA 11768 - Lattice Point or Not

php?

option=com_onlinejudge&Itemid=8&page=show_problem&category=516&problem=2868&mosmsg=Submission+received+with+ID+13823461" target="_blank" style="">题目链接

题意:给定两个点,构成一条线段。这些点都是十分位形式的,求落在这个直线上的正数点。

思路:先把直线表达成a x + b y = c的形式,a,b, c都化为整数表示。然后利用扩展gcd求出x和y的通解,然后已知min(x1, x2) <= x <= max(x1, x2), min(y1, y2) <= y <= max(y1, y2)。这样一来就能够求出通解中t的范围,t能取的整数就是整数解。就得到答案。

值得注意的是。直线为平行坐标系的情况。要特殊推断一下

代码:

#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;
}


以上是关于UVA 11768 - Lattice Point or Not(数论)的主要内容,如果未能解决你的问题,请参考以下文章

UVa 11768 格点判定(扩展欧几里得求线段整点)

UVA 1602 Lattice Animals

UVA1602 Lattice Animals 搜索+剪枝

UVA 1602 Lattice Animals解题思路(打表+set)

例题 7-14 UVA-1602Lattice Animals

UVA - 1602 Lattice Animals (暴力+同构判定)