Score : 500 points
问题描述
你将获得一个正整数 N 以及两个各有 N 个正整数的序列:A=(A1,A2,…,AN) 和 B=(B1,B2,…,BN)。
我们有一个 N×N 的网格。从上到下第 i 行、从左到右第 j 列的方格被称为方格 (i,j)。对于每一对整数 (i,j),满足 1≤i,j≤N,方格 (i,j) 上写有整数 Ai+Bj。处理 Q 个以下形式的查询请求。
- 给定一组四个整数 h1,h2,w1,w2,满足 1≤h1≤h2≤N,1≤w1≤w2≤N。找出以 (h1,w1) 为左上角、(h2,w2) 为右下角的矩形区域内的所有整数的最大公约数。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
You are given a positive integer N and sequences of N positive integers each: A=(A1,A2,…,AN) and B=(B1,B2,…,BN).
We have an N×N grid. The square at the i-th row from the top and the j-th column from the left is called the square (i,j). For each pair of integers (i,j) such that 1≤i,j≤N, the square (i,j) has the integer Ai+Bj written on it. Process Q queries of the following form.
- You are given a quadruple of integers h1,h2,w1,w2 such that 1≤h1≤h2≤N,1≤w1≤w2≤N. Find the greatest common divisor of the integers contained in the rectangle region whose top-left and bottom-right corners are (h1,w1) and (h2,w2), respectively.
Constraints
- 1≤N,Q≤2×105
- 1≤Ai,Bi≤109
- 1≤h1≤h2≤N
- 1≤w1≤w2≤N
- All values in input are integers.
Input is given from Standard Input in the following format:
N Q
A1 A2 … AN
B1 B2 … BN
query1
query2
⋮
queryQ
Each query is in the following format:
h1 h2 w1 w2
Output
Print Q lines. The i-th line should contain the answer to queryi.
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
Sample Output 1
2
1
11
6
10
Let Ci,j denote the integer on the square (i,j).
For the 1-st query, we have C1,2=4,C1,3=6,C2,2=6,C2,3=8, so the answer is their greatest common divisor, which is 2.
1 1
9
100
1 1 1 1
Sample Output 2
109
update @ 2024/3/10 10:50:11