题意:每次询问一个区间的gcd,和这个gcd相同的区间的个数。
区间的gcd个数最多不会超过
log(max)
个, 而且gcd序列又是递减的,所以直接暴力枚举左端点,对右端点二分求出每一个值的区间扔到map里面统计一下就好了。
using namespace std;
int dp[maxn][21];
int a[maxn];
int n, q;
map <int, long long> gg;
int scan () {
char ch=' ';
while(ch<'0'||ch>'9')ch=getchar();
int x=0;
while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar();
return x;
}
void rmq_init () {
for (int i = 0; i < n; i++) dp[i][0] = a[i];
for (int j = 1; (1<<j) <= n; j++) {
for (int i = 0; i+(1<<j)-1 < n; i++) {
dp[i][j] = __gcd (dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
}
}
}
int rmq (int l, int r) {
int k = 0;
while ((1<<(k+1)) <= r-l+1) k++;
return __gcd (dp[l][k], dp[r-(1<<k)+1][k]);
}
void solve () {
for (int i = 0; i < n; i++) {
int pos = n-1;
while (pos >= i) {
int tmp = rmq (i, pos);
int l = i, r = pos;
while (r-l > 1) {
int mid = (l+r)>>1;
if (rmq (i, mid) == tmp) r = mid;
else l = mid;
}
int cur;
if (rmq (i, l) == tmp) cur = l;
else cur = r;
gg[tmp] += (pos-cur+1);
pos = cur-1;
}
}
}
int main () {
int t, kase = 0;
scanf ("%d", &t);
while (t--) {
printf ("Case #%d:\n", ++kase);
gg.clear ();
n = scan ();
for (int i = 0; i < n; i++) a[i] = scan ();
rmq_init ();
solve ();
q = scan ();
while (q--) {
int l = scan (), r = scan ();
int ans = rmq (l-1, r-1);
printf ("%d %lld\n", ans, gg[ans]);
}
}
return 0;
}