CodeForces 75C Modified GCD 【二分+数论】
2013-06-24 11:07
417 查看
题目链接
先求出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 gcd
from bisect import bisect_right as br
g = gcd(*map(int, raw_input().split()))
i = 1
r = []
while i*i <= g:
if g % i == 0:
r.append(i)
r.append(g/i)
i += 1
r = 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
先求出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 gcd
from bisect import bisect_right as br
g = gcd(*map(int, raw_input().split()))
i = 1
r = []
while i*i <= g:
if g % i == 0:
r.append(i)
r.append(g/i)
i += 1
r = 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 689C Mike and Chocolate Thieves (二分+数论)
- codeforces 300E Empire Strikes Back 数论+二分查找
- Codeforces 483B Friends and Presents(二分+数论)
- Codeforces 483B Friends and Presents【二分+数论】涨姿势~~~
- CodeForces 359D (数论+二分+ST算法)
- Codeforces 75C (二分+数学)
- codeforces 225B B. Well-known Numbers(数论+二分+贪心+构造)
- Codeforces Round #425 (Div. 2) Problem C Strange Radiation (Codeforces 832C) - 二分答案 - 数论
- CodeForces 633 B.A Trivial Problem(二分+数论)
- Codeforces 456 div2 A B ***D(思维题) ***E(二分/DFS/数论)
- CodeForces 75 C.Modified GCD(数论)
- codeforces 75C C. Modified GCD(二分)
- [BZOJ2876] [NOI2012] 骑行川藏 - 数论 - 拉格朗日定理(拉格朗日乘子法) + 二分
- Magic Powder - 2 CodeForces - 670D2(二分)
- CodeForces - 670D2 Magic Powder - 2 (二分&模拟)
- CodeForces 3107 ODDDIV - Odd Numbers of Divisors(数论)
- codeforces 700A As Fast As Possible 二分求和?我觉得直接解更好
- CodeForces - 689C Mike and Chocolate Thieves (二分)
- CodeForces - 567D One-Dimensional Battle Ships (二分)