C. Modified GCD(二分加搜索约数)
2014-09-23 20:36
176 查看
C. Modified GCD
time limit per test 2 seconds
memory limit per test 256 megabytes
题目连接:
传送门
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 divisord between two integers
a and b that is in a given range fromlow 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 tohigh 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 integern,
the number of queries (1 ≤ n ≤ 104). Thenn lines follow, each line contains one query consisting of two integers,low
and high (1 ≤ low ≤ high ≤ 109).
Output
Print n lines. Thei-th of them should contain the result of thei-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.
Sample test(s)
Input
Output
time limit per test 2 seconds
memory limit per test 256 megabytes
题目连接:
传送门
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 divisord between two integers
a and b that is in a given range fromlow 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 tohigh 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 integern,
the number of queries (1 ≤ n ≤ 104). Thenn lines follow, each line contains one query consisting of two integers,low
and high (1 ≤ low ≤ high ≤ 109).
Output
Print n lines. Thei-th of them should contain the result of thei-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.
Sample test(s)
Input
9 27 3 1 5 10 11 9 11
Output
3 -1 9 AC代码:#include <algorithm> #include <iostream> #include <cstdio> using namespace std; const int M = 1e6; int ma[M]; int Gcd(int a, int b) { return b == 0 ? a : Gcd(b, a % b); } int main() { int a,b,n,ua,ub; while(~scanf("%d %d",&a,&b)) { scanf("%d",&n); int ans = Gcd(a,b), to = 0; for(int i = 1; i * i <= ans; i++) //运用求素数的方法sqrt时间复杂度找出所有约数; { if(ans % i == 0) { ma[to++] = i; if(i * i != ans) ma[to++] = ans / i; } } sort(ma,ma + to); while(n--) { scanf("%d %d",&ua,&ub); int tp = lower_bound(ma, ma + to, ub) - ma; if(ma[tp] > ub || tp == to) tp--; if(ma[tp] < ua || ma[tp] > ub) puts("-1"); else printf("%d\n",ma[tp]); } } return 0; }
相关文章推荐
- 数据结构_ST算法+二分搜索_GCD问题
- 文章标题 codeforces 75C:Modified GCD (二分+最大公因数)
- codeforces 75C C. Modified GCD(二分)
- 350. Intersection of Two Arrays II -- 双指针、哈希表、排序、二分搜索
- 2016 ACM/ICPC Asia Regional Qingdao Online 1001 I Count Two Three(打表+二分搜索)
- 九度OJ月赛---题目1534:数组中第K小的数字 (二分搜索)
- 挖掘算法中的数据结构(六):二分查找 和 二分搜索树(插入、查找、深度优先遍历)
- HDU 5726 GCD(rmq+二分)
- 二分搜索
- codeforces 778D. String Game (二分搜索)
- 最大化平均值(二分搜索)
- codeforces 732D Exams 二分搜索 贪心
- 二分搜索(两次)-poj3579
- 无处不在的二分搜索
- poj 3258 River Hopscotch 二分搜索
- (Relax 数论1.23)POJ 3122 Pie(二分搜索)
- Codeforces Round #214 (Div. 2) D. Dima and Trap Graph (枚举+二分+搜索)
- UVALive5170 UVA231 POJ1887 Testing the CATCHER【最长下降子序列+DP+二分搜索】
- 二分搜索 POJ 2456 Aggressive cows
- 二分搜索