From 276bf447e808737a001106e534d0c13d8db429cb Mon Sep 17 00:00:00 2001 From: freakin23 Date: Sun, 27 Oct 2024 12:43:00 +0530 Subject: [PATCH 1/5] py sol for Polygon Lattice Points --- content/5_Plat/Geo_Pri.mdx | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/content/5_Plat/Geo_Pri.mdx b/content/5_Plat/Geo_Pri.mdx index a5e1404c0f..6e19cfbdaf 100644 --- a/content/5_Plat/Geo_Pri.mdx +++ b/content/5_Plat/Geo_Pri.mdx @@ -466,6 +466,46 @@ int main() { ``` + + +```py +from math import gcd + +class Point: + def __init__(self, x: int = 0, y: int = 0): + self.x = x + self.y = y + + def __sub__(self, other): + return Point(self.x - other.x, self.y - other.y) + +n = int(input()) +points = [] + +for _ in range(n): + x, y = map(int, input().split()) + points.append(Point(x, y)) + +points.append(points[0]) + +area = 0 +for i in range(n): + area += points[i].x * points[i + 1].y - points[i].y * points[i + 1].x + +area = abs(area) +boundary_points = 0 + +for i in range(n): + diff = points[i + 1] - points[i] + g = gcd(abs(diff.x), abs(diff.y)) + boundary_points += g + +interior_points = (area - boundary_points) // 2 + 1 + +print(interior_points, boundary_points) +``` + + From 866e3f216d79d0404c3b069ab8a72035794332cb Mon Sep 17 00:00:00 2001 From: freakin23 Date: Sun, 27 Oct 2024 12:44:19 +0530 Subject: [PATCH 2/5] add TC --- content/5_Plat/Geo_Pri.mdx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/content/5_Plat/Geo_Pri.mdx b/content/5_Plat/Geo_Pri.mdx index 6e19cfbdaf..5413ab2b5c 100644 --- a/content/5_Plat/Geo_Pri.mdx +++ b/content/5_Plat/Geo_Pri.mdx @@ -421,6 +421,8 @@ $A$ can be computed using cross-product. ## Implementation +**Time Complexity:** $\mathcal{O}(N)$ + From 8a975b28a3ace183ec3caf89028927159e7aa31f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 27 Oct 2024 07:17:27 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- content/5_Plat/Geo_Pri.mdx | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/content/5_Plat/Geo_Pri.mdx b/content/5_Plat/Geo_Pri.mdx index 5413ab2b5c..b7f5b3836c 100644 --- a/content/5_Plat/Geo_Pri.mdx +++ b/content/5_Plat/Geo_Pri.mdx @@ -473,34 +473,36 @@ int main() { ```py from math import gcd + class Point: - def __init__(self, x: int = 0, y: int = 0): - self.x = x - self.y = y + def __init__(self, x: int = 0, y: int = 0): + self.x = x + self.y = y + + def __sub__(self, other): + return Point(self.x - other.x, self.y - other.y) + - def __sub__(self, other): - return Point(self.x - other.x, self.y - other.y) - n = int(input()) points = [] for _ in range(n): - x, y = map(int, input().split()) - points.append(Point(x, y)) + x, y = map(int, input().split()) + points.append(Point(x, y)) points.append(points[0]) area = 0 for i in range(n): - area += points[i].x * points[i + 1].y - points[i].y * points[i + 1].x + area += points[i].x * points[i + 1].y - points[i].y * points[i + 1].x area = abs(area) boundary_points = 0 for i in range(n): - diff = points[i + 1] - points[i] - g = gcd(abs(diff.x), abs(diff.y)) - boundary_points += g + diff = points[i + 1] - points[i] + g = gcd(abs(diff.x), abs(diff.y)) + boundary_points += g interior_points = (area - boundary_points) // 2 + 1 From 9a886483f3ef21a4390b6974fb452598e2441bd6 Mon Sep 17 00:00:00 2001 From: Rameez Parwez <79394137+freakin23@users.noreply.github.com> Date: Sun, 27 Oct 2024 20:26:29 +0530 Subject: [PATCH 4/5] Update TC --- content/5_Plat/Geo_Pri.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/5_Plat/Geo_Pri.mdx b/content/5_Plat/Geo_Pri.mdx index b7f5b3836c..6d8e3c58ee 100644 --- a/content/5_Plat/Geo_Pri.mdx +++ b/content/5_Plat/Geo_Pri.mdx @@ -421,7 +421,7 @@ $A$ can be computed using cross-product. ## Implementation -**Time Complexity:** $\mathcal{O}(N)$ +**Time Complexity:** $\mathcal{O}(N \log P)$, where `P` is the maximum difference between coordinates of points. From 17b167463544822e7b346c0598c048799ce02a27 Mon Sep 17 00:00:00 2001 From: Rameez Parwez <79394137+freakin23@users.noreply.github.com> Date: Sun, 27 Oct 2024 20:28:40 +0530 Subject: [PATCH 5/5] Update Geo_Pri.mdx --- content/5_Plat/Geo_Pri.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/5_Plat/Geo_Pri.mdx b/content/5_Plat/Geo_Pri.mdx index 6d8e3c58ee..6924fe01e5 100644 --- a/content/5_Plat/Geo_Pri.mdx +++ b/content/5_Plat/Geo_Pri.mdx @@ -421,7 +421,7 @@ $A$ can be computed using cross-product. ## Implementation -**Time Complexity:** $\mathcal{O}(N \log P)$, where `P` is the maximum difference between coordinates of points. +**Time Complexity:** $\mathcal{O}(N \log P)$, where $P$ is the maximum difference between coordinates of points.