#P5444. [APIO2019] 奇怪装置

[APIO2019] 奇怪装置

Description

Archaeologists have discovered a strange device left behind by an ancient civilization. The device has two screens, which display two integers xx and yy.

After research, scientists reached the following conclusion: the device is a special clock. It starts counting the number of moments tt that have passed since some point in the past, but the creator displays tt in a strange way. If the number of moments that have passed since the device started measuring is tt, then it displays two integers: x=((t+tB)modA)x = ((t + \lfloor \frac {t}{B} \rfloor) \bmod A), and y=(tmodB)y = (t \bmod B). Here x\lfloor x \rfloor is the floor function, which means the greatest integer less than or equal to xx.

Archaeologists further found that the screens cannot work all the time. In fact, the screens work normally only during nn continuous time intervals. The ii-th interval is from moment lil_i to moment rir_i. Now scientists want to know how many different pairs x,yx,y can be displayed while the device is working.

Two pairs (x1,y1)(x_1,y_1) and (x2,y2)(x_2,y_2) are different if and only if x1x2x_1 \neq x_2 or y1y2y_1 \neq y_2.

Input Format

The first line contains three integers nn, AA, and BB. The next nn lines each contain two integers lil_i and rir_i, representing the ii-th time interval during which the device can work.

Output Format

Output one line containing one integer, the answer to the problem.

3 3 3
4 4
7 9
17 18
4
3 5 10
1 20
50 68
89 98
31
2 16 13
2 5
18 18
5

Hint

For the first sample, the device screens will display the following pairs.

t=4:(2,1)t=4:(2,1)

t=7:(0,1)t=7:(0,1)

t=8:(1,2)t=8:(1,2)

t=9:(0,0)t=9:(0,0)

t=17:(1,2)t=17:(1,2)

t=18:(0,0)t=18:(0,0)

There are four different pairs in total: (0,0),(0,1),(1,2),(2,1)(0,0),(0,1),(1,2),(2,1).

For all data, $1 \leq n \leq 10^6,1 \leq A,B \leq 10^{18},0 \leq l_i \leq r_i \leq 10^{18}$.

Let S=i=1n(rili+1)S=\sum_{i=1}^n (r_i-l_i+1) and L=maxi=1n(rili+1)L=\max_{i=1}^n (r_i-l_i+1).

The detailed additional constraints and scores for subtasks are shown in the table below:

Subtask Additional Constraints Score
1 S106S\leq 10^6 10
2 n=1n=1 5
3 A×B106A\times B \leq 10^6
4 B=1B=1
5 B3B\leq 3
6 B106B\leq 10^6 20
7 LBL\leq B
8 No additional constraints. 30

Translated by ChatGPT 5