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 65 66 67 68 69 70 71 72 73
| #include <bits/stdc++.h> using namespace std;
namespace io {...} using namespace io;
const int maxn = 1e5 + 5;
int n, t, q; int a[maxn][18], log_2[maxn]; map<int, long long> ans;
int gcd(int a, int b) { while (a ^= b ^= a ^= b %= a); return b; }
int ask(int l, int r) { int k = log_2[r - l + 1]; return gcd(a[l][k], a[r - (1 << k) + 1][k]); }
int bs(int i, int x) { int l = i, r = n, mid; while (l < r) { mid = (l + r + 1) >> 1; if (ask(i, mid) < x) r = mid - 1; else l = mid; } return l; }
const string tttttmp = "Case #";
int main() { read(t); for (int tid = 1; tid <= t; tid++) { ans.clear(); read(n); for (int i = 1; i <= n; i++) { read(a[i][0]); if (i != 1) log_2[i] = log_2[i >> 1] + 1; } for (int j = 1; j < 18; j++) { for (int i = 1; i + (1 << j) - 1 <= n; i++) { a[i][j] = gcd(a[i][j - 1], a[i + (1 << (j - 1))][j - 1]); } } for (int i = 1; i <= n; i++) { int j = bs(i, a[i][0]); ans[a[i][0]] += j - i + 1; while (j < n) { int g = ask(i, j + 1); int nj = bs(i, g); ans[g] += nj - j; j = nj; } } for (char x : tttttmp) putchar(x); write(tid, ':'); putchar('\n'); read(q); while (q--) { static int l, r, g; read(l); read(r); g = ask(l, r); write(g, ' '); write(ans[g]); } } return 0; }
|