Description:
Well, here is another math class task. In mathematics, G C D GCD GCD is the greatest common divisor, and it’s an easy task to calculate the G C D GCD GCD between two positive integers.
A common divisor for two positive numbers is a number which both numbers are divisible by.
But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a a a and b b b that is in a given range from low to high (inclusive), i.e. low ≤ d d d ≤ high. It is possible that there is no common divisor in the given range.
You will be given the two integers a a a and b b b, then n queries. Each query is a range from low to high and you have to answer each query.
Input
The first line contains two integers a a a and b b b, the two integers as described above ( 1 ≤ a , b ≤ 1 0 9 1 ≤ a, b ≤ 10^9 1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries ( 1 ≤ n ≤ 1 0 4 1 ≤ n ≤ 10^4 1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high ( 1 ≤ l o w ≤ h i g h ≤ 1 0 9 1 ≤ low ≤ high ≤ 10^9 1 ≤ low ≤ high ≤ 109).
Output
Print n n n lines. The i − t h i-th i−th of them should contain the result of the i − t h i-th i−th query in the input. If there is no common divisor in the given range for any query, you should print − 1 -1 −1 as a result for this query.
Examples
Input
9 27
3
1 5
10 11
9 11
Output
3
-1
9
题意:
给出 a , b a,b a,b 问你在区间 [ l . r ] [l.r] [l.r] 中的最大公因子是多少,区间内的最大大公因子肯定是 g c d ( a , b ) gcd(a,b) gcd(a,b) 的因子,所以先把最大公因子的所有因子都预处理出来,在区间内二分查找就行了。
AC代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
using namespace std;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", (n))
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define mem(a, n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mod(x) ((x) % MOD)
#define gcd(a, b) __gcd(a, b)
#define lowbit(x) (x & -x)
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
int ret = 0, sgn = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
ret = ret * 10 + ch - '0';
ch = getchar();
}
return ret * sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
if (a > 9)
Out(a / 10);
putchar(a % 10 + '0');
}
ll gcd(ll a, ll b)
{
return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
return a * b / gcd(a, b);
}
///快速幂m^k%mod
ll qpow(ll a, ll b, ll mod)
{
if (a >= mod)
a = a % mod + mod;
ll ans = 1;
while (b)
{
if (b & 1)
{
ans = ans * a;
if (ans >= mod)
ans = ans % mod + mod;
}
a *= a;
if (a >= mod)
a = a % mod + mod;
b >>= 1;
}
return ans;
}
// 快速幂求逆元
int Fermat(int a, int p) //费马求a关于b的逆元
{
return qpow(a, p - 2, p);
}
///扩展欧几里得
ll exgcd(ll a, ll b, ll &x, ll &y)
{
if (b == 0)
{
x = 1, y = 0;
return a;
}
ll d = exgcd(b, a % b, x, y);
ll z = x;
x = y;
y = z - y * (a / b);
return d;
}
///中国剩余定理模板0
ll china(int a[], int b[], int n) //a[]为除数,b[]为余数
{
ll M = 1, y, x = 0;
for (int i = 0; i < n; ++i) //算出它们累乘的结果
M *= a[i];
for (int i = 0; i < n; ++i)
{
int w = M / a[i];
ll tx = 0;
ll t = exgcd(w, a[i], tx, y); //计算逆元
x = (x + w * (b[i] / t) * x) % M;
}
return (x + M) % M;
}
ll a, b;
ll l, r;
vector<ll> c;
int q;
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
sldd(a, b);
ll res = gcd(a, b);
for (ll i = 1; i < sqrt(res + 0.5); i++)
{
if (res % i == 0)
{
c.pb(i);
if (i * i != res)
c.pb(res / i);
}
}
sort(c.begin(), c.end(), cmp);
/*for (vector<int>::iterator it = c.begin(); it != c.end(); it++)
cout << *it << endl;*/
sd(q);
while (q--)
{
ll ans;
sldd(l, r);
if (l > res)
{
puts("-1");
continue;
}
vector<ll>::iterator it = lower_bound(c.begin(), c.end(), r, greater<int>());
if (*it < l)
ans = -1;
else
ans = *it;
pld(ans);
}
return 0;
}