PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GL0000145. Truy vấn GCD sau khi xóa đoạn (GCD Queries)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Truy vấn GCD sau khi xóa đoạn (GCD Queries)
GCD Queries
Source: CodeChef
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a sequence of positive integers indexed from to . For a query , imagine removing every element from index through , inclusive.
Find the GCD of all remaining elements. Every query is independent and uses the original sequence. The input guarantees that at least one element remains after the removal.
Input
The first line contains , followed by values . Each of the next lines contains such that and .
Output
Print GCD values, one per query.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Input
3 3
2 6 9
1 1
2 2
2 3
Output
3
1
2
Explanation
Removing index leaves , with GCD . Removing index leaves , with GCD . Removing leaves the single number , so the GCD is .