Greatest of the Greatest Common Divisors
Time limit1sMemory limit2048 MB
Given an integer sequence and range queries, find the largest gcd over all pairs of positions inside each query range.
- Level
Medium6 of 10
- Topics
- Number theory, Sorting, Array
- Solved
- No attempts yet
Problem
You are given a sequence of integers and a number of intervals in the sequence. The intervals are specified by their leftmost and rightmost positions. An interval consisting of integers has pairs of integers at different positions, and each pair has a greatest common divisor. For each given interval, find the greatest of these greatest common divisors.
For example, when the sequence is and the whole sequence is the interval, the following 15 pairs of integers at different positions and are considered.
The greatest of the greatest common divisors of these 15 pairs is in this case.
Input
The input consists of a single test case. The first line contains an integer (), the number of integers in the sequence. The second line contains positive integers . None of them exceeds . The third line contains a positive integer (), the number of intervals. The next lines each contain two integers and (), specifying the interval through .
Output
Output lines. The -th line contains the greatest of the greatest common divisors of all pairs in the interval specified by and .