DMOPC '19 Contest 7 P4 - Bob and Continued Fractions

View as PDF

Submit solution


Points: 20 (partial)
Time limit: 2.0s
Memory limit: 128M

Author:
Problem type

A continued fraction is an expression of the form a1+1a2+1+1an where a1,a2,,an are positive integers.

Bob has managed to obtain an array A of N positive integers. He now wants to compute Q continued fractions, the i-th of which uses the elements from indices li to ri (inclusive) in the array, in order. For example, if A=[1,4,5,2], li=2, and ri=4, the answer would be: 4+15+12=4+211=4611 Please help Bob with this task!

Constraints

1N,Q300000
1Ai109
1liriN

Subtask 1 [10%]

1N,Q1000

Subtask 2 [90%]

No additional constraints.

Input Specification

The first line of input will contain two space-separated integers, N and Q.
The second line will contain N space-separated integers, A1 through AN.
The next Q lines will each contain two space-separated integers, li and ri.

Output Specification

Q lines, each containing two space-separated integers: the numerator and denominator of the i-th continued fraction, in lowest terms. Since these numbers may be very large, you may output them mod 109+7. However, note that the fraction must be in lowest terms before modding; reducing the fraction after modding may not yield the same result!

Sample Input

Copy
4 3
1 4 5 2
2 4
1 2
3 3

Sample Output

Copy
46 11
5 4
5 1

Comments

There are no comments at the moment.