题目大意:找出a b的公因子 然后给几个查询,查询在x y之间的最大公因子 输出。
先把因子找出来,然后二分找一下最大的.
其实找因子 一共有两种方法(就我所知) 对于10^18这种级别的 用质因子那种方法找, 对于10^9这种级别的 用sqrt(n)的方法枚举因子的方法去找.....
/* *********************************************** Author :guanjun Created Time :2016/10/30 10:11:46 File Name :cf67c.cpp ************************************************ */ #include <bits/stdc++.h> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 0x3f3f3f3f #define maxn 10010 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; priority_queue<int,vector<int>,greater<int> >pq; struct Node{ int x,y; }; struct cmp{ bool operator()(Node a,Node b){ if(a.x==b.x) return a.y> b.y; return a.x>b.x; } }; bool cmp(int a,int b){ return a>b; } vector<int>v; int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); int a,b,x,y,n; cin>>a>>b; a=__gcd(a,b); b=sqrt(a); v.clear(); for(int i=1;i<=b;i++){ if(a%i==0){ if(i*i==a)v.push_back(i); else v.push_back(i),v.push_back(a/i); } } sort(v.begin(),v.end()); cin>>n; for(int i=1;i<=n;i++){ cin>>x>>y; int pos=upper_bound(v.begin(),v.end(),y)-v.begin()-1; if(x>v[pos])puts("-1"); else cout<<v[pos]<<endl; } return 0; }