PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000360. Điểm nguyên trong đa giác (Polygon Lattice Points)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Điểm nguyên trong đa giác (Polygon Lattice Points)

Polygon Lattice Points

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a simple polygon with nn vertices whose coordinates are all integers. Vertices are listed in boundary order and the last vertex is connected to the first. The polygon does not self-intersect and has positive area; it may be concave or convex and either orientation is allowed.

A lattice point has integer xx and yy coordinates. Count:

  1. The number II of lattice points strictly inside the polygon, excluding its boundary.
  2. The number BB of lattice points on the polygon boundary, including vertices.

Input

  • First line: number of vertices nn.
  • Next nn lines: integer coordinates xi,yix_i,y_i in boundary order.

Output

Print I B (interior points first, boundary points second).

Subtasks

  • Subtask 1 (20 points): 3<=n<=8;−20<=xi,yi<=203 <= n <= 8; -20 <= x_i,y_i <= 20.
  • Subtask 2 (30 points): 3<=n<=1000;−105<=xi,yi<=1053 <= n <= 1000; -10^5 <= x_i,y_i <= 10^5.
  • Subtask 3 (50 points): 3<=n<=100000;−109<=xi,yi<=1093 <= n <= 100000; -10^9 <= x_i,y_i <= 10^9.

The vertices form a simple polygon with positive area.

Examples

Example 1

Input

4
1 1
5 3
3 5
1 4

Output

6 8

Explanation

The shoelace formula gives 2*area=18. The sum of edge gcd values is B=8. By Pick, I=(18-8+2)/2=6.

Example 2

Input

4
-1 -1
2 -1
2 1
-1 1

Output

2 10

Explanation

The 3-by-2 rectangle has (3-1)(2-1)=2 interior lattice points and 2(3+2)=10 boundary points.

Example 3

Input

3
0 0
4 0
0 3

Output

3 8

Explanation

Its area is 6, hence 2*area=12. The edge gcd contributions are 4,1,3, so B=8 and I=(12-8+2)/2=3.