Bayan 2015 Contest Warm Up D. CGCDSSQ 暴力

链接:

http://codeforces.com/contest/475/problem/D

题意:

给你一个a数组,每次询问一个x,问有多少区间的gcd等于x

题解:

直接暴力打表,因为gcd的值不会有太多不同的,所以不会超时

代码:

31 ll n;
32 ll a[MAXN];
33 map<ll, ll> ans;
34 map<ll, ll> last;
35 map<ll, ll> now;
36 
37 ll gcd(ll a, ll b) {
38     return b == 0 ? a : gcd(b, a%b);
39 }
40 
41 int main() {
42     ios::sync_with_stdio(false), cin.tie(0);
43     cin >> n;
44     rep(i, 0, n) cin >> a[i];
45     rep(i, 0, n) {
46         now[a[i]]++;
47         for (auto it = last.begin(); it != last.end(); ++it)
48             now[gcd((*it).first, a[i])] += (*it).second;
49         for (auto it = now.begin(); it != now.end(); ++it)
50             ans[(*it).first] += (*it).second;
51         last = now;
52         now.clear();
53     }
54     int q;
55     cin >> q;
56     while (q--) {
57         int x;
58         cin >> x;
59         cout << ans[x] << endl;
60     }
61     return 0;
62 }
原文地址:https://www.cnblogs.com/baocong/p/7381766.html