#GL0000144. Ước chung lớn nhất trên hình chữ nhật (Rectangle GCD)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Ước chung lớn nhất trên hình chữ nhật (Rectangle GCD)
Rectangle GCD
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given two sequences of positive integers and . Consider an grid. Its cell at row , column contains . Both rows and columns are numbered from .
Process independent queries. Each specifies a rectangle with top-left cell and bottom-right cell , including its boundary. For each rectangle, find the greatest common divisor of all the values in it. A rectangle may consist of a single cell, a single row, or a single column. No query modifies the grid.
Input
The first line contains . The second line lists and the third lists . Each of the next lines contains , where and .
Output
Print lines, each containing the GCD of all entries in the respective queried rectangle.
Subtasks
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; .
- Subtask 3 (50 points): ; .
Examples
Input
3 5
3 5 2
8 1 3
1 2 2 3
1 3 1 3
1 1 1 1
2 2 2 2
3 3 1 1
Output
2
1
11
6
10
Explanation
The first rectangle contains , whose GCD is . The second covers the whole grid and has GCD . The last three queries each contain one cell: , , and .