ÀÂÒ
Language:

Remote Training on Programming

Problems On-line status Contests FAQ
For authors:
Register  ||  Login
 
Hello, Guest! Login or register.

108. Common greatest Divisors

Time Limit: 3 seconds
Memory Limit:800KB
Points:10
View Problem Statistics Submit Problem added Administrator

Consider a sequence of N interegs xi. In order to run a number-theoretic statistical experiment, a random set of queries to this sequence is generated.

Each query is a pair of integers <r, q>. The result of a query will be the Greatest Common Divisor of the members (GCD) in the subsequence starting with xr and ending with xq.

For example, if sequence (1, 2, 4, 6, 12) is given then for query <1, 5> the result equals to GCD(1, 2, 4, 6, 12) = 1, for query <2, 4> the result will be GCD(2, 4, 6) = 2, and for query <4, 5> the result will be GCD(6, 12) = 6.

Your task is to write a program, which will construct a set of results, given a number sequence (xi) and a set of queries.

Note that the queries may be repeated.

Input

The first line of the input file contains two integers (N, M) separated by one or several spaces. Number N is the number of elements in the sequence of numbers. (1<N<=10000). Number M is the number of queries (0<M<=100000).

The second line contains N integers xi that form the sequence. The numbers are delimited by one or more spaces (0<xi<=109).

Then M lines follow, containing number pairs (ri, qi) each. The numbers in a pair are delimited by one or more spaces. A pair is a query for GCD computation of the members xri ... xqi (0<ri,qi<=N, ri<qi).

Output

The first line of the output file must contain the number M. The following M lines must contain the answers. The i-th line of those M lines must contain the answer to the query <ri, qi>.

Samples

InputOutput
5 3
1 2 4 6 12
1 5
2 4
4 5
3
1
2
6
3 3
8 12 15
1 2
2 3
1 3
3
4
3
1

View Problem Statistics Submit Author/source:
Problems from Contests / ACM Contests / Rybinsk-2005 /
107. F - Telegraph 108. 109. H - Random Number Generator 110. I - Circles
We can all benefit by doing occasional "toy" programs, when artificial restrictions are set up, so that we are forced to push our abilities to the limit. The art of tackling miniproblems with all our energy will sharpen our talents for the real problems. Donald E. Knuth.
time generating 0.499 sec.
© Copyright VSTU, AVT, Nosov D.A.