CodeForces 75C Modified GCD 【二分+数论】
来源:互联网 发布:免费摄像头监控软件 编辑:程序博客网 时间:2024/06/05 16:52
题目链接
先求出a和b的最大公约数,找出其所有的因数——sqrt(n)的复杂度,涨姿势了。
然后就是判断所有的因数有没有落在low,high区间里面了——二分即可(upper_bound)
C++版本:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>#include <vector>using namespace std;typedef long long ll;vector<int> x;int low, high, a, b, n, m, ans;int main() { scanf("%d%d", &a, &b); a = __gcd(a, b); b = sqrt(a); x.clear(); for (int i=1; i<=b; i++) if (a % i == 0) { x.push_back(i); x.push_back(a/i); } sort(x.begin(), x.end()); scanf("%d", &n); for (int i=0; i<n; i++) { scanf("%d%d", &low, &high); m = upper_bound(x.begin(), x.end(), high) - x.begin() - 1; ans = x[m]; if (low > ans) puts("-1"); else printf("%d\n", ans); } return 0;}
Python版本:
from fractions import gcdfrom bisect import bisect_right as br g = gcd(*map(int, raw_input().split()))i = 1r = []while i*i <= g: if g % i == 0: r.append(i) r.append(g/i) i += 1r = sorted(r)for i in xrange(input()): l, h = map(int, raw_input().split()) m = r[br(r, h)-1] print -1 if m < l else m
- CodeForces 75C Modified GCD 【二分+数论】
- codeforces 75C C. Modified GCD(二分)
- CodeForces 75C Modified GCD
- Codeforces 75 C Modified GCD
- 文章标题 codeforces 75C:Modified GCD (二分+最大公因数)
- C. Modified GCD(二分加搜索约数)
- cf 75c Modified GCD
- 【CodeForces】75C - Modified GCD(快速求公约数,upper_bound函数)
- Codeforces #207 div2 C. Prime Number 数论 gcd
- Codeforces #323 div2 C. GCD Table 数论 构造
- Codeforces-798C-Mike and gcd problem(贪心+数论)
- Codeforces 798C:Mike and gcd problem【数论+贪心】
- Modified GCD
- hdu 5042 GCD pair(数论 + 二分)
- CodeForces 7C 【数论】
- Codeforces 624D Array Gcd(数论+dp)
- Codeforces Round #426 (Div. 2) C. The Meaningless Game(gcd,数论)
- Codeforces 75C (二分+数学)
- jsgraphics 图形库
- Linux下的内存调试工具 ---valgrind
- AssetBundle测试
- C# Listview控件用法
- 并查集------无线网络
- CodeForces 75C Modified GCD 【二分+数论】
- 【windows8进入安全模式3种方法分享】
- asp.net数据绑定表达式
- Javascript 数组 中的 sort方法
- mysql性能优化-慢查询分析、优化索引和配置
- eclipse如何生成exe程序
- Java 简单的Socket通讯的服务器端实现
- eclispse中c&c++环境配置
- C语言qsort函数详解