1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include <bits/stdc++.h> using namespace std; int a[100005], st[30][100005], n; void init() { int i, j, t = log2(n); for (i = 1; i <= n; i++) st[0][i] = a[i]; for (j = 1; j <= t; j++) { for (i = 1; i <= n - (1 << j) + 1; i++) st[j][i] = __gcd(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]); } } int sgcd(int l, int r) { int t = log2(r - l + 1); return __gcd(st[t][l], st[t][r - (1 << t) + 1]); } map<int, long long> sum; int ub(int beg, int l, int r, int tar) { int md; while (r - l > 1) { md = ((r - l) >> 1) + l; if (sgcd(beg, md) < tar) r = md; else l = md; } return r; } int main() { int q, g, i, j, t; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &a[i]); init(); for (i = 1; i <= n; i++) { j = i; while (j <= n) { g = sgcd(i, j); t = ub(i, j, n, g); sum[g] += t - j; j = t; if (sgcd(i, t) == g) { sum[g]++; j++; } } } scanf("%d", &q); while (q--) { scanf("%d", &t); printf("%lld\n", sum[t]); } return 0; }
|