АВТ
Язык:

Дистанционный практикум по программированию

Задачи Online статус Соревнования
Новости Справка СДО
 
Здравствуйте, Гость! Войдите с паролем или зарегистрируйтесь.

108. Common greatest Divisors

Ограничение времени: 3 сек.
Ограничение памяти:65536 КБайт
Баллы:100
Статистика Послать на проверку Задачу добавил 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.

Исходные данные

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).

Результат

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>.

Примеры

Исходные данныеРезультат
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

Статистика Послать на проверку Обсуждение задачи Автор/источник:
Задачи с соревнований и сборов / Чемпионат мира по программированию (ICPC) / Рыбинск-2005 /
107. F - Telegraph 108. 109. H - Random Number Generator 110. I - Circles
 
время генерации 0.344 сек.
© Copyright ВоГУ, АВТ, Носов Д.А., Андрианов И.А.