This page is still under construction.

Parts of this page are still being built. What you see may change.

Strange Machine

Time limit4sMemory limit512 MB

Summary
Count the distinct pairs (x, y) produced by the map t -> (((t + floor(t/B)) mod A), t mod B) over n disjoint time intervals.
Level

Hard8 of 10

Topics
Math, Number theory, Implementation, Intervals
Solved
No attempts yet

Problem

Archaeologists have found a strange machine left behind by an ancient civilization. This machine has two parts that output two integers xx and yy.

After examining the machine, the archaeologists concluded that it is a special clock that outputs information about a time tt, starting from some point in the past. At time tt, the first output part prints the integer x=((t+⌊t/B⌋) mod A)x = ((t + \lfloor t/B \rfloor) \bmod A), and the second output part prints the integer y=(t mod B)y = (t \bmod B). (⌊x⌋\lfloor x \rfloor denotes the largest integer not greater than xx.)

Analysis showed that the machine does not always work; it works only on nn consecutive intervals [li,ri][l_i, r_i]. For further research, the archaeologists ask you to write a program that determines how many distinct ordered pairs (x,y)(x, y) the machine outputs.

Two ordered pairs (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) are different if x1≠x2x_1 \ne x_2 or y1≠y2y_1 \ne y_2.

Input

The first line gives three integers nn, AA, BB. (1≤n≤1061 \le n \le 10^6; 1≤A,B≤10181 \le A, B \le 10^{18})

Each of the next nn lines gives two integers lil_i and rir_i, the start and end times of an interval [li,ri][l_i, r_i] on which the machine works. (0≤li≤ri≤10180 \le l_i \le r_i \le 10^{18}, ri<li+1r_i < l_{i+1})

Output

Print the number of distinct ordered pairs (x,y)(x, y) the machine outputs while it works.

Hint

In the first test, the machine outputs (2,1)(2, 1) at time 4, (0,1)(0, 1) at time 7, (1,2)(1, 2) at time 8, (0,0)(0, 0) at time 9, (1,2)(1, 2) at time 17, and (0,0)(0, 0) at time 18. Thus it outputs four distinct ordered pairs: (0,0),(0,1),(1,2),(2,1)(0, 0), (0, 1), (1, 2), (2, 1).

Examples3

  1. Example 1

    Input
    3 3 3
    4 4
    7 9
    17 18
    
    Expected output
    4
    
  2. Example 2

    Input
    3 5 10
    1 20
    50 68
    89 98
    
    Expected output
    31
    
  3. Example 3

    Input
    2 16 13
    2 5
    18 18
    
    Expected output
    5