# Codeforces 75 C Modified GCD

http://codeforces.com/problemset/problem/75/C

C. Modified GCD
time limit per test2 seconds
memory limit per test256 megabytes

Problem description
Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it’s an easy task to calculate the 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 and b that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range.

You will be given the two integers a and 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 and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).

Output
Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.

Examples
input
9 27
3
1 5
10 11
9 11
output
3
-1
9

1.预处理两个数所有的公约数。
2.对数组进行二分，比较和y的大小，找出比y小的公约数中的最大的那一个，把它和左区间进行比较，如果比左区间大，该值就是答案，如果比左区间小，那么就是没有答案。

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;

int m[1000005];

int gcd(int a,int b);
int search(int y,int t);

int main()
{
int a,b;
while(scanf("%d%d",&a,&b)!=EOF)
{
int i,t=0,x;
x=gcd(a,b);
for(i=1;i<=sqrt(x);i++)   //不能为i<=x，遍历会超时
{
if(x%i==0)
{
m[t++]=i;
m[t++]=x/i;
}
}
sort(m,m+t);     //排序

int n,z,y,c;
scanf("%d",&n);
while(n--)
{
int tag=0;
scanf("%d%d",&z,&y);
if(y<m[0]||z>m[t-1])    //特判
tag=1;
else
{
c=search(y,t);

if(z>m[c])
tag=1;
}
if(tag)
printf("-1\n");
else
printf("%d\n",m[c]);

}

}
return 0;
}

int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}

int search(int y,int t)
{
int left=0,right=t,mid;
while(left+1<right)
{
mid=(left+right)/2;
if(y>=m[mid])
left=mid;
else
right=mid;
}
return left;
}

• 本文已收录于以下专栏：

## 【CodeForces】75C - Modified GCD（快速求公约数，upper_bound函数）

C. Modified GCD time limit per test 2 seconds memory limit per test 256 megabytes i...
• wyg1997
• 2016年05月24日 18:57
• 410

## 文章标题 codeforces 75C:Modified GCD (二分+最大公因数）

Modified GCDDescriptionWell, here is another math class task. In mathematics, GCD is the greatest co...

## C. Modified GCD(二分加搜索约数)

C. Modified GCD time limit per test  2 seconds memory limit per test   256 megabytes 题目...

## CodeForces - 803C Maximal GCD

Maximal GCDYou are given positive integer number n. You should create such strictly increasing seque...
• WwyY9
• 2017年08月01日 18:46
• 173

## Codeforces Round #323 (Div. 2)C. GCD Table

C. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
• ZZ_AC
• 2015年10月06日 00:26
• 314

## Mike and gcd problem CodeForces - 798C

Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ...,...

## Codeforces 583C/582A 暴力+gcd

A. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard inp...

## Codeforces #323 C. GCD Table （脑洞题、Induction）

• lwt36
• 2015年10月04日 15:50
• 367

## 【Codeforces 798 C】+ gcd

C. Mike and gcd problem time limit per test2 seconds memory limit per test256 megabytes inputstan...

举报原因： 您举报文章：Codeforces 75 C Modified GCD 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)