#include <iostream>
#include <cstdio>
using namespace std;
int gcd(int a,int b)
{
int t;
if(a<b)
{
t=a;a=b;b=t;
}
while(t=a%b)
{
a=b;
b=t;
}
return b;
}
int main()
{
int a,b,x,y,n,c,d;
scanf("%d%d",&x,&y);
scanf("%d",&n);
int tmp=gcd(x,y);
bool flag;
int ans;
while(n--)
{
flag=false;
scanf("%d%d",&a,&b);
c=tmp/a;d=(tmp-1)/b+1;
if(b-a<d-c)
{
for(int i=b;i>=a;i--)
{
if(tmp%i==0)
{
ans=i;
flag=true;
break;
}
}
}
else
{
for(int i=d;i<=c;i++)
{
if(tmp%i==0)
{
ans=tmp/i;
flag=true;
break;
}
}
}
if(flag)
printf("%d\n",ans);
else
printf("-1\n");
}
}cf 75c Modified GCD
最新推荐文章于 2019-06-01 12:06:00 发布
本文介绍了一个基于最大公约数(GCD)的应用算法,通过计算两个整数的最大公约数来解决一系列数学问题。该算法使用C++实现,并通过具体实例展示了如何在一组特定条件下寻找符合条件的整数。
198

被折叠的 条评论
为什么被折叠?



