#arc159e. [arc159_e]Difference Sum Query
[arc159_e]Difference Sum Query
Problem Statement
You are given a positive integer , and pairs of positive integers: (note that and start with index ).
We have the following sequence of non-negative integers .
- is determined as follows.
- Let , , and .
- Let $m=\\left \\lfloor \\dfrac{a_{t \\bmod M} \\times l + b_{t \\bmod M} \\times r}{a_{t \\bmod M} +b_{t \\bmod M}} \\right \\rfloor$ ( is the greatest integer not exceeding ). If , let and terminate.
- If , let ; otherwise, let . Increment by and return to step 2.
Find for .
It can be proved that the answers are at most under the constraints of this problem.
Constraints
- $\\max \\left (\\dfrac{a_i}{b_i},\\dfrac{b_i}{a_i}\\right ) \\leq 2$
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print lines. The -th line should contain the answer to the question for .
Sample Input 1
5 1
1 1
3
1 2
2 4
3 5
Sample Output 1
1
3
2
We have . For example, is determined as follows.
- Let , , and .
- Let $m=3(=\\left \\lfloor \\dfrac{1 \\times 1 + 1 \\times 5}{1+1} \\right \\rfloor)$.
- Since , let . Increment by to .
- Let $m=1(= \\left \\lfloor \\dfrac{1 \\times 1 + 1 \\times 2}{1+1} \\right \\rfloor )$. Since , let and terminate.
The answer to the question for , for example, is $\\sum_{j=c_i}^{d_i-1} |x_j - x_{j+1}| = |x_1-x_2| = 1$.
Sample Input 2
1000000000000000 2
15 9
9 15
3
100 10000
5000 385723875
150 17095708
Sample Output 2
19792
771437738
34191100