# 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 (二分+最大公因数）

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 题目...

codefoces 题解 博弈

## 【codeforces 803C】Maximal GCD

【题目链接】:http://codeforces.com/contest/803/problem/C【题意】 给你一个数字n;一个数字k; 让你找一个长度为k的序列; 要求这个长度为k的序列...

## Codeforces 803C Maximal GCD【思维】

C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input st...

## Codeforces 798C Mike and gcd problem (贪心)

Codeforces 798C Mike and gcd problem (贪心)

## Codeforces 798C Mike and gcd problem【思维+贪心】好题！

C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input s...

## 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
• 111

## Codeforces 583C GCD Table 模拟 map

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)