C. Modified GCD(二分加搜索约数)
来源:互联网 发布:淘宝优惠卷是真的假的 编辑:程序博客网 时间:2024/06/06 00:43
C. Modified GCD
题目连接: 传送门
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 integersa and b that is in a given range fromlow tohigh (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.
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).
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.
9 2731 510 119 11
3-19AC代码:#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;}
- C. Modified GCD(二分加搜索约数)
- CodeForces 75C Modified GCD 【二分+数论】
- codeforces 75C C. Modified GCD(二分)
- cf 75c Modified GCD
- CodeForces 75C Modified GCD
- Codeforces 75 C Modified GCD
- 文章标题 codeforces 75C:Modified GCD (二分+最大公因数)
- Modified GCD
- 互补约数(GCD)续
- 二分搜索代码(c++)
- 【CodeForces】75C - Modified GCD(快速求公约数,upper_bound函数)
- 二分搜索||c语言实现
- codeforces 460C - Present 二分加模拟
- C. Present(二分加贪心)
- Codeforces 493C 枚举加二分
- 贪心加二分搜索之疯牛问题
- HDU 5812 Distance(求约数+枚举gcd)
- GCD XOR 求约数以及推论 UVA
- Servlet学习笔记(三):Servlet基础
- 数据结构学习(一)
- [面试] - java面试题2
- 解决TCP网络传输“粘包”问题
- 山东中医药大学《数据结构》实验1
- C. Modified GCD(二分加搜索约数)
- 二维数组的输出
- ubuntu下批量修改后缀名及批量重命名
- 杭电2008
- [面试] - java面试题3
- Oralce新建数据库、新建远程登录用户全过程
- 0923
- MFC、WTL 、ATL、STL联系与区别
- Python __name__的问题