$\Large\color{pink}{Describe}$

这题的题意是要求出不能整除区间内所有数的个数,正难则反一下就变成了区间长度减去能整除区间内所有数的个数,显然这个数就是区间的$gcd$(窝太菜了一开始还以为可以是$gcd$的约数,后来发现是窝傻了),然后就可以发现这个$sum$跟$gcd$是具有可加性的,之后就大力上线段树就完事了。

目前是本题最优解,欢迎超越。

维护部分的代码:

1
2
3
4
5
6
node operator +(node a, node b) {
node c;
c.gcd = gcd(a.gcd, b.gcd);
c.sum = (a.gcd == c.gcd)*a.sum + (b.gcd == c.gcd)*b.sum;
return c;
}

$\large Code:$

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#pragma region revive
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define inl inline
#define re register int
#define fa(x) t[x].fa
#define son(x,y) t[x].child[y]
#define ls(x) t[x].child[0]
#define rs(x) t[x].child[1]
#define ll long long
const int inf = 0x3f3f3f3f;
#define lowbit(x) ((x) & (-x))
using namespace std;
#ifndef _DEBUG
#define getchar() (*(IOB.in.p++))
#define putchar(c) (*(IOB.out.p++)=(c))
#define io_eof() (IOB.in.p>=IOB.in.pend)
struct IOBUF { struct { char buff[1 << 26], *p, *pend; }in; struct { char buff[1 << 26], *p; }out; IOBUF() { in.p = in.buff; out.p = out.buff; in.pend = in.buff + fread(in.buff, 1, 1 << 26, stdin); }~IOBUF() { fwrite(out.buff, 1, out.p - out.buff, stdout); } }IOB;
#endif
template<typename IO>
inl void write(IO x) {
if (x == 0) return (void)putchar('0');
if (x < 0)putchar('-'), x = -x;
static char buf[30];
char* p = buf;
while (x) {
*(p++) = x % 10 + '0';
x /= 10;
}
while (p > buf)putchar(*(--p));
}
inl void writestr(const char *s) { while (*s != 0)putchar(*(s++)); }
template<typename IO>inl void writeln(IO x) { write(x), putchar('\n'); }
template<typename IO>inl void writesp(IO x) { write(x), putchar(' '); }
inl int readstr(char *s) {
char *begin = s, c = getchar();
while (c < 33 || c>127) {
c = getchar();
}
while (c >= 33 && c <= 127) {
*(s++) = c;
c = getchar();
}
*s = 0;
return s - begin;
}
template<typename IO>
inl IO read() {
IO x = 0;
register bool w = 0;
register char c = getchar();
while (c > '9' || c < '0') {
if (c == '-') w = 1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return w ? -x : x;
}
#pragma endregion
inl int gcd(int x, int y) { if (x < y)swap(x, y); return y ? gcd(y, x%y) : x; }
struct node {
int gcd, sum;
}t[500001];
node operator +(node a, node b) {
node c;
c.gcd = gcd(a.gcd, b.gcd);
c.sum = (a.gcd == c.gcd)*a.sum + (b.gcd == c.gcd)*b.sum;
return c;
}
int a[100001];
inl void maintain(int x) { t[x] = t[x << 1] + t[x << 1 | 1]; }
inl void build(int k, int l, int r) {
if (l == r) { return (void)(t[k].gcd = a[l], t[k].sum = 1); }
re mid = l + r >> 1;
build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r);
maintain(k);
}
inl node query(int k, int l, int r, int x, int y) {
if (l >= x && r <= y)return t[k];
re mid = l + r >> 1;
node ans = { 0,0 };
if (x <= mid)ans = ans + query(k << 1, l, mid, x, y);
if (y > mid)ans = ans + query(k << 1 | 1, mid + 1, r, x, y);
return ans;
}
signed main() {
re n = read<int>(), m;
for (re i = 1; i <= n; i++)a[i] = read<int>();
build(1, 1, n);
m = read<int>();
while (m--) {
re x = read<int>(), y = read<int>();
writeln(y - x + 1 - query(1, 1, n, x, y).sum);
}
}