C. Modified GCD
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

题目大意:找出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 << ;
const double eps=1e-;
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=;i<=b;i++){
if(a%i==){
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=;i<=n;i++){
cin>>x>>y;
int pos=upper_bound(v.begin(),v.end(),y)-v.begin()-;
if(x>v[pos])puts("-1");
else cout<<v[pos]<<endl;
}
return ;
}

Codeforces Beta Round #67 (Div. 2)C. Modified GCD的更多相关文章

  1. Codeforces Beta Round #67 (Div. 2)

    Codeforces Beta Round #67 (Div. 2) http://codeforces.com/contest/75 A #include<bits/stdc++.h> ...

  2. 【计算几何】 Codeforces Beta Round #67 (Div. 2) E. Ship's Shortest Path

    读懂题意其实是模板题.就是细节略多. #include<cstdio> #include<cmath> #include<algorithm> using name ...

  3. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  4. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  5. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  6. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  7. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  8. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  9. Codeforces Beta Round #74 (Div. 2 Only)

    Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...

随机推荐

  1. CMU Database Systems - Two-phase Locking

    首先锁是用来做互斥的,解决并发执行时的数据不一致问题 如图会导致,不可重复读 如果这里用lock就可以解决,数据库里面有个LockManager来作为master,负责锁的记录和授权 数据库里面的基本 ...

  2. vue-router + axios token登录状态认证

    vue项目中登录状态判断往往基于jwt认证,我们可以采用判断本地是否存在token,及token是否过期或token值错误 1.利用vue-router 钩子函数判断本地是否存在token impor ...

  3. SAS,SATA普及文档

    目前所能见到的硬盘接口类型主要有IDE.SATA.SCSI.SAS.FC等等. IDE是俗称的并口,SATA是俗称的串口,这两种硬盘是个人电脑和低端服务器常见的硬盘.SCSI是"小型计算机系 ...

  4. QT使用插件QAxWidget来展示web页面

    要求:用qt版开发一个桌面程序,该程序有一个界面,用来显示一个采用silverlight开发的web页面. 分析:在qt中实现web显示,根据qt的版本和对应编译器的版本,有如下选择: (1)5.6以 ...

  5. Qt中实现无边框的窗体

    1 自定义窗体类继承自QWidget 2 在构造函数中设置无边框效果 setWindowFlags(Qt::FramelessWindowHint);//无边框 setAttribute(Qt::WA ...

  6. typora_test

    加粗标题 加下标线 <!--aba--> #Include ![](C:\Users\123\Pictures\Saved Pictures\1.jpg) ![](http://gyz.g ...

  7. Linux 安装 Tomcat 详解

    说明:安装的 tomcat 为解压版(即免安装版):apache-tomcat-8.5.15.tar.gz (1)使用 root 用户登录虚拟机,在根目录下的 opt 文件夹新建一个 software ...

  8. mysql的密码管理、mysql初始密码查找、密码修改、mysql登录

    1.查询mysql的初始密码: 初始密码密码是随机产生的,每台机器产生的都不一样的 grep 'temporary password' /var/log/mysqld.log 或者 cat /var/ ...

  9. page对象的使用及常见方法

    page对象的使用及常见方法 制作人:全心全意 page对象代表JSP本身,只有在JSP页面内才是合法的.page对象本质上是包含当前Servlet接口引用的变量,可以看作是this关键字的别名. p ...

  10. IDLE in Python (Ubuntu)

    To lauch IDLE in the Current Woking Directory >>> usr/bin/idle3 Alt + n  # next command Alt ...