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

#CT00000051. Chuông hẹn giờ (Alarm Bells)

Bài tập này chưa được xếp vào lộ trình.

Chuông hẹn giờ (Alarm Bells)

Alarm Bells

Version: Phuoc Hung OJ Extended

Problem Statement

Two bells at an amusement park ring together when the park opens, which is defined as minute 00. From then on, each bell rings at a fixed interval:

  • The first bell rings at minutes 0,A,2A,3A,…0,A,2A,3A,\ldots.
  • The second bell rings at minutes 0,B,2B,3B,…0,B,2B,3B,\ldots.

Time is measured in integer minutes since opening. The management considers only the interval from minute LL to minute RR, including both endpoints.

Count the distinct minutes in [L,R][L,R] when both bells ring simultaneously. Minute 00 counts if it belongs to the interval.

Input

One line contains four integers A,B,L,RA,B,L,R, in this order:

  • AA: the first bell's ringing interval, in minutes.
  • BB: the second bell's ringing interval, in minutes.
  • L,RL,R: the endpoints of the time interval to consider.

Output

Print one integer: the number of minutes in [L,R][L,R] at which both bells ring.

Subtasks

The following general constraints apply to all subtasks: 1≤A,B≤1091\le A,B\le 10^9; 0≤L≤R≤10180\le L\le R\le 10^{18}.

  • Subtask 1 (30%): L=0L=0 and R≤105R\le 10^5.
  • Subtask 2 (30%): gcd⁡(A,B)=1\gcd(A,B)=1.
  • Subtask 3 (40%): No additional restrictions beyond the general constraints.

Examples

Example 1

Input

4 6 0 30

Output

3

Explanation

The bells ring together at minute 00 and again at minutes 12,24,36,…12,24,36,\ldots. Within [0,30][0,30], the qualifying minutes are 00, 1212, and 2424. Minute 3636 is greater than R=30R=30, so the answer is 33.

Example 2

Input

5 7 1 70

Output

2

Explanation

For A=5A=5 and B=7B=7, the simultaneous ringing minutes are 00, 3535, 7070, 105105, and so on. Minute 00 is outside [1,70][1,70], whereas 3535 and 7070 are inside it. The answer is 22.

Example 3

Input

8 12 25 47

Output

0

Explanation

The simultaneous ringing minutes are 0,24,48,…0,24,48,\ldots. Minute 2424 is before L=25L=25, and minute 4848 is after R=47R=47. Thus there are no qualifying minutes in [25,47][25,47], and the answer is 00.