-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcdq.cpp
60 lines (50 loc) · 1.08 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
//Codechef- gcdq
//#include<iostream>
//using namespace std;
#include<stdio.h>
int gcd(int a, int b)
{
if(a%b==0)
return b;
else
return gcd(b, a%b);
}
int main()
{
int tc, n, q, a[100001], l, r, temp;
//cin>>tc;
scanf("%d", &tc);
while(tc--)
{
//cin>>n>>q;
scanf("%d%d", &n, &q);
for(int i=1; i<=n; i++)
//cin>>a[i];
scanf("%d", &a[i]);
while(q--)
{
//cin>>l>>r;
scanf("%d%d", &l, &r);
temp = -1;
for(int j=1; j<=n; j++)
{
if((j>=l) && (j<=r))
{
j = r;
continue;
}
else
{
if(temp == -1)
temp = a[j];
if((a[j]%temp == 0) && a[j] > temp)
continue;
temp = gcd(a[j],temp);
}
}
//cout<<temp<<endl;
printf("%d \n", temp);
}
}
return 0;
}