ÀÂÒ
Language: Russian
English

Remote Training on Programming

Problems Online status Contests
News FAQ E-learning
For authors:
Register  ||  Login
 
Hello, Guest! Please login or register.

108. Common greatest Divisors

Time Limit: 3 seconds
Memory Limit:65536KB
Points:100
View Problem Statistics Submit Problem added debug

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 Problem discussion Author/source:
Problems from Contests and Camps / World Championship in Programming (ICPC) / Rybinsk-2005 /
107. F - Telegraph 108. 109. H - Random Number Generator 110. I - Circles
time generating 0.406 sec.
© Copyright VSU, AVT, Nosov D.A., Andrianov I.A.