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

#SP0000110. Đếm điểm nguyên nguyên tố trên chu vi (Chef and Polygons)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đếm điểm nguyên nguyên tố trên chu vi (Chef and Polygons)

Chef and Polygons

Source: CodeChef

Version: Phuoc Hung OJ Extended

Problem Statement

A simple polygon is formed by connecting the nn integer-coordinate vertices (x1,y1),(x2,y2),…,(xn,yn)(x_1,y_1),(x_2,y_2),\ldots,(x_n,y_n) in the stated order and closing the last edge to the first vertex. Edges do not cross. An integer lattice point has both coordinates integral. Count all lattice points (x,y)(x,y) on the polygon boundary for which x+yx+y is prime. Each point must be counted once, including vertices shared by adjacent edges.

Input

The first line contains nn, the number of vertices.

The second line contains nn integers x1,x2,…,xnx_1,x_2,\ldots,x_n.

The third line contains nn integers y1,y2,…,yny_1,y_2,\ldots,y_n. The ii-th values on the latter two lines form vertex ii.

Output

Print one integer: the number of boundary lattice points with prime coordinate sum.

Subtasks

Let PP denote the Euclidean perimeter of the polygon.

  • Subtask 1 (20 points): 3≤n≤43\le n\le 4; 0≤xi,yi≤10000\le x_i,y_i\le 1000 for every vertex; 0<P≤20000<P\le 2000; vertices in the given order form a simple polygon.
  • Subtask 2 (30 points): 3≤n≤123\le n\le 12; 0≤xi,yi≤10000000000\le x_i,y_i\le 1000000000 for every vertex; 0<P≤2000000<P\le 200000; vertices in the given order form a simple polygon.
  • Subtask 3 (50 points): 3≤n≤1003\le n\le 100; 0≤xi,yi≤1000000000000000\le x_i,y_i\le 100000000000000 for every vertex; 0<P≤100000000<P\le 10000000; vertices in the given order form a simple polygon.

Examples

Input

4
1 5 5 1
1 1 4 4

Output

7

Explanation

The vertices are (1,1),(5,1),(5,4),(1,4)(1,1),(5,1),(5,4),(1,4). The three qualifying points along the bottom are (1,1)(1,1), (2,1)(2,1), and (4,1)(4,1). One additional point on the right is (5,2)(5,2); two on the top are (3,4)(3,4) and (1,4)(1,4); one on the left is (1,2)(1,2). Their coordinate sums are all prime. Every corner is counted once, giving 77.