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

#GL0000291. Điểm nguyên trên đoạn thẳng (Lattice Point or Not)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Điểm nguyên trên đoạn thẳng (Lattice Point or Not)

Lattice Point or Not

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

In the Cartesian plane, a lattice point has both coordinates integral. Two points A=(x1,y1)A=(x_1,y_1) and B=(x2,y2)B=(x_2,y_2) are given, each coordinate having exactly one decimal digit. Count all lattice points on the closed line segment ABAB, including endpoints if they are lattice points. The endpoints may coincide.

Input

One line containing four decimal numbers x1,y1,x2,y2x_1,y_1,x_2,y_2 in that order.

Output

Print one integer: the number of lattice points on the given segment.

Subtasks

  • Subtask 1 (20 points): 0<∣x1∣,∣y1∣,∣x2∣,∣y2∣≤200<|x_1|,|y_1|,|x_2|,|y_2|\le20; each coordinate has exactly one digit after the decimal point.
  • Subtask 2 (30 points): 0<∣x1∣,∣y1∣,∣x2∣,∣y2∣≤20000<|x_1|,|y_1|,|x_2|,|y_2|\le2000; each coordinate has exactly one digit after the decimal point.
  • Subtask 3 (50 points): 0<∣x1∣,∣y1∣,∣x2∣,∣y2∣≤2000000<|x_1|,|y_1|,|x_2|,|y_2|\le200000; each coordinate has exactly one digit after the decimal point.

Examples

Example 1

Input

10.1 10.1 11.2 11.2

Output

1

Explanation

The segment lies on y=xy=x, with both coordinates between 10.110.1 and 11.211.2. The only integer in that interval is 1111, so the unique lattice point is (11,11)(11,11). Neither endpoint is a lattice point.

Example 2

Input

10.2 100.3 300.3 11.1

Output

0

Explanation

The line through the endpoints satisfies 892x+2901y=300068.7892x+2901y=300068.7. If xx and yy were integers, the left side would be an integer, contradicting the nonintegral right side. Thus the segment contains no lattice point.

Example 3

Input

1.0 1.0 2.0 2.0

Output

2

Explanation

Both endpoints, (1,1)(1,1) and (2,2)(2,2), are lattice points and are included. There is no integer strictly between 11 and 22, so there are no other lattice points on this segment.