-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcd.cpp
More file actions
37 lines (33 loc) · 718 Bytes
/
gcd.cpp
File metadata and controls
37 lines (33 loc) · 718 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
freopen("input1.txt", "r", stdin);
freopen("output1.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
//brute force
int t;
cin >> t;
while (t--) {
int n , q;
cin >> n >> q;
int ar[n + 10];
for (int i = 1; i <= n; i++) {
cin >> ar[i];
}
while (q--) {
int l, r;
cin >> l >> r;
int gc = 0;
for (int i = 1 ; i <= l - 1; i++) {
gc = __gcd(gc, ar[i]);
}
for (int i = r + 1 ; i <= n; i++) {
gc = __gcd(gc, ar[i]);
}
cout << gc << endl;
}
}
}