Touring Show
Time limit1sMemory limit512 MB
For each range [l, r], find the fewest triangles k such that 1 + k*A_i*A_j is a multiple of a for some pair i<j in the range, or -1.
- Level
Medium6 of 10
- Topics
- Number theory, Array, Prefix sum
- Solved
- No attempts yet
Problem
In 2033, Siu became a master of drawing two different shapes at the same time with both hands, after ten years of training. To celebrate the tenth anniversary of his training, Siu plans a tour with venues. To make each show different, he takes a sequence of length , and for each show he picks two indices and with from the given interval . The show goes as follows.
- The left hand repeatedly draws a circle that takes seconds to complete.
- The right hand first waves for 1 second, then repeatedly draws a triangle that takes seconds to complete.
- There is no pause between completing a shape and starting the next one. The show ends at the moment both shapes complete at exactly the same time.
Drawing triangles is very tiring, so Siu wants to draw as few triangles as possible. However, it is impossible to finish the show without drawing any triangle.
Input
The first line contains integers , , and in that order. (, )
The second line contains the integers of sequence , separated by spaces, in order. ()
From the third line, each of the next lines contains two integers and , separated by a space. ()
Output
For each show, print on its own line the minimum number of triangles drawn. If the show cannot be finished in finite time by any method, print .