PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GL0000198. Ước chung lớn nhất trong khoảng (Modified GCD)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Ước chung lớn nhất trong khoảng (Modified GCD)
Ước chung lớn nhất trong khoảng (Modified GCD)
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Given fixed positive integers , each query provides and asks for the largest common divisor of within that inclusive interval. Print -1 if none exists.
Input
First line: . Second line: . Next lines: .
Output
Print one integer per query.
Subtasks
-
Subtask 1 (20 points): ; ; .
-
Subtask 2 (30 points): ; ; .
-
Subtask 3 (50 points): ; ; .
Examples
Example 1
Input
9 27
3
1 5
10 11
9 11
Output
3
-1
9
Explanation
The GCD is , whose positive divisors are . The largest in is , there is none in , and is in .
Example 2
Input
12 18
2
1 6
4 5
Output
6
-1
Explanation
The common divisors of and are ; only the first interval contains , and the second contains no such divisor.
Example 3
Input
1 1
2
1 1
2 5
Output
1
-1
Explanation
When , the only common positive divisor is .