给出一个长度为nn,只由数字构成的字符串SS,给出一个质数PP

mm次询问,每次询问区间[l,r][l, r]中有多少个子串是PP的倍数(包括00

n,m105n, m\le 10^5

LOJ 2053

Solution

考虑莫队,先考虑把条件转化成只和l,rl, r有关的东西

numinum_i表示SS串中以第ii位开始的后缀所对应的数字

那么条件可以转化成:

  • PP不为2255

    预处理下numinum_i在模PP意义下的值,莫队随便搞搞就可以了

  • PP2255

    只需要判末位数是否为PP的倍数,莫队再随便搞搞就行了

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair <int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }
template <typename T> inline T read ()
{
T sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

inline void proc_status ()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) << endl;
}

const int Maxn = 1e5 + 100;

int BLOCK;
int N, M, P, Belong[Maxn], A[Maxn];
char S[Maxn];

struct query
{
int l, r, id;
} Q[Maxn];


inline int cmp (query a, query b)
{
if (Belong[a.l] == Belong[b.l]) return (Belong[a.l] & 1) ? (a.r < b.r) : (a.r > b.r);
return Belong[a.l] < Belong[b.l];
}

inline void Init ()
{
BLOCK = sqrt (N);
for (int i = 1; i <= N; ++i) Belong[i] = (i - 1) / BLOCK + 1;
sort (Q + 1, Q + M + 1, cmp);
}

namespace Sub1
{
inline void init ()
{
int sum = 0, pow10 = 1;
for (int i = N; i >= 1; --i)
{
sum = (sum + (LL) pow10 * (S[i] - '0') % P) % P;
A[i] = sum;
pow10 = (LL) pow10 * 10 % P;
}
}

__gnu_pbds :: gp_hash_table <int, LL> Map;
LL ans;
int Vis[Maxn];

inline void Update (int l, int r, int op)
{
if (op)
{
if (!Vis[r]) ++Map[A[r]], ans += Map[A[r + 1]];
else ans -= Map[A[r + 1]], --Map[A[r]];
Vis[r] ^= 1;
}
else
{
if (!Vis[l]) ans += Map[A[l]] + (A[l] == A[r + 1]), ++Map[A[l]];
else --Map[A[l]], ans -= Map[A[l]] + (A[l] == A[r + 1]);
Vis[l] ^= 1;
}
}

LL Ans[Maxn];

inline void solve ()
{
init ();

int l = 1, r = 0;
for (int i = 1; i <= M; ++i)
{
while (r < Q[i].r) Update (l, ++r, 1);
while (l > Q[i].l) Update (--l, r, 0);
while (r > Q[i].r) Update (l, r--, 1);
while (l < Q[i].l) Update (l++, r, 0);
Ans[Q[i].id] = ans;
}

for (int i = 1; i <= M; ++i) printf("%lld\n", Ans[i]);
}
}

namespace Sub2
{
inline void init ()
{
for (int i = 1; i <= N; ++i) A[i] = S[i] - '0';
}

LL ans, cnt;
int Vis[Maxn];

inline void Update (int l, int r, int op)
{
if (op)
{
if (!Vis[r]) { if (!(A[r] % P)) ans += r - l + 1, ++cnt; }
else { if (!(A[r] % P)) ans -= r - l + 1, --cnt; }
Vis[r] ^= 1;
}
else
{
if (!Vis[l]) cnt += !(A[l] % P), ans += cnt;
else ans -= cnt, cnt -= !(A[l] % P);
Vis[l] ^= 1;
}
}

LL Ans[Maxn];

inline void solve ()
{
init ();

int l = 1, r = 0;
for (int i = 1; i <= M; ++i)
{
while (r < Q[i].r) Update (l, ++r, 1);
while (l > Q[i].l) Update (--l, r, 0);
while (r > Q[i].r) Update (l, r--, 1);
while (l < Q[i].l) Update (l++, r, 0);
Ans[Q[i].id] = ans;
}

for (int i = 1; i <= M; ++i) printf("%lld\n", Ans[i]);
}
}

inline void Solve ()
{
Init ();
if (P != 2 && P != 5) Sub1 :: solve ();
else Sub2 :: solve ();
}

inline void Input ()
{
P = read<int>();
scanf("%s", S + 1); N = strlen(S + 1);
M = read<int>();
for (int i = 1; i <= M; ++i) Q[i].l = read<int>(), Q[i].r = read<int>(), Q[i].id = i;
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif

Input ();
Solve ();

return 0;
}