-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModifiedGCD.java
47 lines (40 loc) · 955 Bytes
/
ModifiedGCD.java
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
/** https://codeforces.com/problemset/problem/75/C #binary-search #number-theory */
import java.util.Scanner;
public class ModifiedGCD {
public static int getGCD(int a, int b) {
int tmp;
while (b != 0) {
tmp = b;
b = a % b;
a = tmp;
}
return a;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int n = sc.nextInt();
int GDC = getGCD(a, b);
int low, high, val;
for (int i = 0; i < n; i++) {
low = sc.nextInt();
high = sc.nextInt();
val = -1;
if (low <= GDC) {
if (high >= GDC) {
val = GDC;
} else {
while (high >= low) {
if (a % high == 0 && b % high == 0) {
val = high;
break;
}
high = a / ((a / high) + 1);
}
}
}
System.out.println(val);
}
}
}