-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodifiedgdc.java
More file actions
30 lines (29 loc) · 939 Bytes
/
modifiedgdc.java
File metadata and controls
30 lines (29 loc) · 939 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
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class modifiedgdc {
static int mod = (int) 1e9 + 7;
static int dp[][];
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt(),b=sc.nextInt();
TreeSet<Integer> treeset=new TreeSet<>();
int c=Math.min(a,b);
for(int i=1;i*i<=c;i++){
if(a%i==0 && b%i==0)treeset.add(i);
if(a%(c/i)==0 && b%(c/i)==0)treeset.add(c/i);
}
int q=sc.nextInt();
StringBuilder sb=new StringBuilder();
Integer temp;
while(q-->0){
int l=sc.nextInt(),r=sc.nextInt();
temp=treeset.floor(r);
if(temp!=null && temp>=l)sb.append(temp+"\n");
else sb.append("-1\n");
}
System.out.println(sb);
}
}