给定两个点p (x1, y1) 和q (x2, y2),计算位于连接它们的线上的积分点数。
例子:如果点是 (0, 2) 和 (4, 0),那么位于其上的积分点数只有一个,即 (2, 1)。
同理,如果点是 (1, 9) 和 (8, 16),则其上的积分点是 6,它们是 (2, 10), (3, 11), (4, 12), (5, 13) )、(6, 14) 和 (7, 15)。
我们强烈建议您在继续解决方案之前单击此处进行练习。
简单的方法
从任何给定点开始,使用循环到达另一个终点。对于循环内的每个点,检查它是否位于连接给定两点的线上。如果是,则将计数增加 1。此方法的时间复杂度为 O(min(x2-x1, y2-y1))。最优方法
1. If the edge formed by joining p and q is parallel
to the X-axis, then the number of integral points
between the vertices is :
abs(p.y - q.y)-1
2. Similarly if edge is parallel to the Y-axis, then
the number of integral points in between is :
abs(p.x - q.x)-1
3. Else, we can find the integral points between the
vertices using below formula:
GCD(abs(p.x - q.x), abs(p.y - q.y)) - 1
GCD 公式如何运作?
这个想法是以最简单的形式找到直线的方程,即在方程 ax + by +c 中,系数 a、b 和 c 成为互质。我们可以通过计算 a、b 和 c 的 GCD(最大公约数)并以最简单的形式转换 a、b 和 c 来做到这一点。
那么,答案将是(y 坐标的差)除以 (a) – 1。这是因为在计算 ax + by + c = 0 之后,对于不同的 y 值,x 将是 y 值的数量,这些值可以被精确整除一种。
下面是上述想法的实现。
C++
// C++ code to find the number of integral points
// lying on the line joining the two given points
#include
#include
using namespace std;
// Class to represent an Integral point on XY plane.
class Point
{
public:
int x, y;
Point(int a=0, int b=0):x(a),y(b) {}
};
// Utility function to find GCD of two numbers
// GCD of a and b
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a%b);
}
// Finds the no. of Integral points between
// two given points.
int getCount(Point p, Point q)
{
// If line joining p and q is parallel to
// x axis, then count is difference of y
// values
if (p.x==q.x)
return abs(p.y - q.y) - 1;
// If line joining p and q is parallel to
// y axis, then count is difference of x
// values
if (p.y == q.y)
return abs(p.x-q.x) - 1;
return gcd(abs(p.x-q.x), abs(p.y-q.y))-1;
}
// Driver program to test above
int main()
{
Point p(1, 9);
Point q(8, 16);
cout << "The number of integral points between "
<< "(" << p.x << ", " << p.y << ") and ("
<< q.x << ", " << q.y << ") is "
<< getCount(p, q);
return 0;
}
Java
// Java code to find the number of integral points
// lying on the line joining the two given points
class GFG
{
// Class to represent an Integral point on XY plane.
static class Point
{
int x, y;
Point(int a, int b)
{
this.x = a;
this.y = b;
}
};
// Utility function to find GCD of two numbers
// GCD of a and b
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Finds the no. of Integral points between
// two given points.
static int getCount(Point p, Point q)
{
// If line joining p and q is parallel to
// x axis, then count is difference of y
// values
if (p.x == q.x)
return Math.abs(p.y - q.y) - 1;
// If line joining p and q is parallel to
// y axis, then count is difference of x
// values
if (p.y == q.y)
return Math.abs(p.x - q.x) - 1;
return gcd(Math.abs(p.x - q.x), Math.abs(p.y - q.y)) - 1;
}
// Driver program to test above
public static void main(String[] args)
{
Point p = new Point(1, 9);
Point q = new Point(8, 16);
System.out.println("The number of integral points between "
+ "(" + p.x + ", " + p.y + ") and ("
+ q.x + ", " + q.y + ") is "
+ getCount(p, q));
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 code to find the number of
# integral points lying on the line
# joining the two given points
# Class to represent an Integral point
# on XY plane.
class Point:
def __init__(self, a, b):
self.x = a
self.y = b
# Utility function to find GCD
# of two numbers GCD of a and b
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
# Finds the no. of Integral points
# between two given points.
def getCount(p, q):
# If line joining p and q is parallel
# to x axis, then count is difference
# of y values
if p.x == q.x:
return abs(p.y - q.y) - 1
# If line joining p and q is parallel
# to y axis, then count is difference
# of x values
if p.y == q.y:
return abs(p.x - q.x) - 1
return gcd(abs(p.x - q.x),
abs(p.y - q.y)) - 1
# Driver Code
if __name__ == "__main__":
p = Point(1, 9)
q = Point(8, 16)
print("The number of integral points",
"between ({}, {}) and ({}, {}) is {}" .
format(p.x, p.y, q.x, q.y, getCount(p, q)))
# This code is contributed by Rituraj Jain
C#
// C# code to find the number of integral points
// lying on the line joining the two given points
using System;
class GFG
{
// Class to represent an Integral point on XY plane.
public class Point
{
public int x, y;
public Point(int a, int b)
{
this.x = a;
this.y = b;
}
};
// Utility function to find GCD of two numbers
// GCD of a and b
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Finds the no. of Integral points between
// two given points.
static int getCount(Point p, Point q)
{
// If line joining p and q is parallel to
// x axis, then count is difference of y
// values
if (p.x == q.x)
return Math.Abs(p.y - q.y) - 1;
// If line joining p and q is parallel to
// y axis, then count is difference of x
// values
if (p.y == q.y)
return Math.Abs(p.x - q.x) - 1;
return gcd(Math.Abs(p.x - q.x), Math.Abs(p.y - q.y)) - 1;
}
// Driver code
public static void Main(String[] args)
{
Point p = new Point(1, 9);
Point q = new Point(8, 16);
Console.WriteLine("The number of integral points between "
+ "(" + p.x + ", " + p.y + ") and ("
+ q.x + ", " + q.y + ") is "
+ getCount(p, q));
}
}
/* This code contributed by PrinciRaj1992 */
Javascript
输出:
The number of integral points between (1, 9) and (8, 16) is 6
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。