-
Notifications
You must be signed in to change notification settings - Fork 2
/
GCDQ.cpp
86 lines (64 loc) · 2.05 KB
/
GCDQ.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
You are given an array A of integers of size N. You will be given Q queries where each query is represented by two integers L, R. You have to find the gcd(Greatest Common Divisor) of the array after excluding the part from range L to R inclusive (1 Based indexing). You are guaranteed that after excluding the part of the array remaining array is non empty.
Input
First line of input contains an integer T denoting number of test cases.
For each test case, first line will contain two space separated integers N, Q.
Next line contains N space separated integers denoting array A.
For next Q lines, each line will contain a query denoted by two space separated integers L, R.
Output
For each query, print a single integer representing the answer of that query.
Constraints
Subtask #1: 40 points
2 = T, N = 100, 1 = Q = N, 1 = A[i] = 105
1 = L, R = N and L = R
Subtask #2: 60 points
2 = T, N = 105, 1 = Q = N, 1 = A[i] = 105
1 = L, R = N and L = R
Sum of N over all the test cases will be less than or equal to 106.
Example
Input:
1
3 3
2 6 9
1 1
2 2
2 3
Output:
3
1
2
Explanation
For first query, the remaining part of array will be (6, 9), so answer is 3. For second query, the remaining part of array will be (2, 9), so answer is 1. For third query, the remaining part of array will be (2), so answer is 2.
*/
#include<bits/stdc++.h>
using namespace std;
int gcdd (int a, int b) {
if (a == 0) return b;
return gcdd(b % a, a);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, q;
cin >> n >> q;
vector <int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
vector <int> pre(n), suf(n);
pre[0] = v[0];
for (int i = 1; i < n; i++) pre[i] = gcdd(pre[i - 1], v[i]);
suf[n - 1] = v[n - 1];
for (int i = n - 2; i >= 0; i--) suf[i] = gcdd(suf[i + 1], v[i]);
while (q--) {
int l, r;
cin >> l >> r;
l -= 2;
if (l >=0 && r <= n - 1) cout << gcdd(pre[l], suf[r]) << endl;
else if (l < 0) cout << suf[r] << endl;
else cout << pre[l] << endl;
}
}
return 0;
}