Strange Machine
Time limit4sMemory limit512 MB
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 and .
After examining the machine, the archaeologists concluded that it is a special clock that outputs information about a time , starting from some point in the past. At time , the first output part prints the integer , and the second output part prints the integer . ( denotes the largest integer not greater than .)
Analysis showed that the machine does not always work; it works only on consecutive intervals . For further research, the archaeologists ask you to write a program that determines how many distinct ordered pairs the machine outputs.
Two ordered pairs and are different if or .
Input
The first line gives three integers , , . (; )
Each of the next lines gives two integers and , the start and end times of an interval on which the machine works. (, )
Output
Print the number of distinct ordered pairs the machine outputs while it works.
Hint
In the first test, the machine outputs at time 4, at time 7, at time 8, at time 9, at time 17, and at time 18. Thus it outputs four distinct ordered pairs: .