-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path75-C.cpp
38 lines (33 loc) · 826 Bytes
/
75-C.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
/*
math > number theory > greatest common divisor (GCD)
difficulty: medium
date: 12/Jun/2020
problem: find the gcd(a, b) that is in a query range [lo .. hi]
hint: note that all divisors of gcd(a, b) are also divisors of a and b
by: @brpapa
*/
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
return b == 0? a : gcd(b, a%b);
}
int main() {
int a, b; cin >> a >> b;
int n = gcd(a, b);
vector<int> divs;
for (int i = 1; i*i <= n; i++) {
if (n%i) continue;
divs.push_back(i);
if (n != i*i) divs.push_back(n/i);
}
int Q; cin >> Q;
while (Q--) {
int lo, hi; cin >> lo >> hi;
int ans = -1;
for (int div : divs)
if (div >= lo && div <= hi)
ans = max(ans, div);
cout << ans << endl;
}
return 0;
}