Codeforces Beta Round #67 (Div. 2)C. Modified GCD
2 seconds
256 megabytes
standard input
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.
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).
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.
- 9 27
3
1 5
10 11
9 11
- 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的更多相关文章
- Codeforces Beta Round #67 (Div. 2)
Codeforces Beta Round #67 (Div. 2) http://codeforces.com/contest/75 A #include<bits/stdc++.h> ...
- 【计算几何】 Codeforces Beta Round #67 (Div. 2) E. Ship's Shortest Path
读懂题意其实是模板题.就是细节略多. #include<cstdio> #include<cmath> #include<algorithm> using name ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #74 (Div. 2 Only)
Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...
随机推荐
- mfc按钮悬停显示文字
.h CToolTipCtrl m_toopTip; .cpp oninitdialog void CDlgDwgLibMan::InitTooltips(){ EnableToolTips(); m ...
- vue中websoket的使用
首先安装npm install --save websocket-heartbeat-js@^1.0.7 在main.js中 引入并挂载全局方法 import WebsocketHeartbeat ...
- JAVA基础——Date和Calendar类
1计算某一月份的最大天数 Calendar time=Calendar.getInstance(); time.clear(); time.set(Calendar.YEAR,year); //yea ...
- vue 使用vue-i18n做全局中英文切换
1.vue-i18n安装 npm install vue-i18n --save-dev 2.在main.js文件中引入 import VueI18n from 'vue-i18n'; Vue.use ...
- 怎么提交小程序给微信?微信小程序的提交审核流程
开发者开发好一款微信小程序后,如何将其提交给微信审核呢?今天正好有空,就整理了一下小程序的提交流程,以供大家参考.如果要发布小程序,那么你需要申请真正的小程序账号,拿到appId,才能在手机预览.及提 ...
- Python基础(五)集合与函数
一.Set集合 set和dict类似,也是一组key的集合,但不存储value.由于key不能重复,所以,在set中,没有重复的key.下面一起看一下set的定义和使用方法: (一),set定义 1 ...
- 手写DAO框架(三)-数据库连接
-------前篇:手写DAO框架(二)-开发前的最后准备--------- 前言 上一篇主要是温习了一下基础知识,然后将整个项目按照模块进行了划分.因为是个人项目,一个人开发,本人采用了自底向上的开 ...
- StringBuilder的构造方法
/* * String和StringBuilder的区别: * String的内容是固定的 * StringBuilder的内容是可变的 * 构造方法: * StringBuilder() * 成员方 ...
- MongoDB 数据文件备份与恢复
备份与恢复数据对于管理任何数据存储系统来说都是非常重要的. 1.冷备份与恢复——创建数据文件的副本(前提是要停止MongoDB服务器),也就是直接copy MongoDB将所有数据都存储在数据目录下, ...
- [luoguP1026] 统计单词个数(DP)
传送门 题解 #include <cstdio> #include <cstring> #define max(x, y) ((x) > (y) ? (x) : (y)) ...