题目链接:
题目大意:
给出两个数,查询[l,r]内这两个数的gcd
题目分析:
对这两个数的cd进行打表,然后二分查找即可。
AC代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
int a,b,n,l,r;
vector<int> v;
int bsearch ( int x , int y )
{
int l = 0 , r = v.size()-1,mid;
//cout << "YES : " << l << " " <<r << endl;
while ( l != r )
{
mid = (l+r+1)>>1;
//cout << v[mid] << " " << l << " " << r <<" " <<y << endl;
if ( v[mid] <= y ) l = mid;
else r = mid-1;
}
if ( v[l] >= x ) return v[l];
return -1;
}
int main ( )
{
while (~scanf ("%d%d" , &a , &b ) )
{
for ( int i = 1 ; i*i <= a ; i++ )
{
if ( a%i ) continue;
if ( b%i == 0 ) v.push_back ( i );
int x = a/i;
if ( i == x ) continue;
if ( b%x == 0 ) v.push_back ( x );
}
sort ( v.begin() , v.end());
scanf ("%d" , &n );
while ( n-- )
{
scanf ( "%d%d" , &l , &r );
printf ( "%d\n" , bsearch ( l , r ));
}
}
}