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
| #include <cstdio>
#include <cstring>
#include <algorithm>
const int MAXN = 500001, MAXQ = 2000000;
const unsigned long long P = 131;
struct Link
{
int prime, times;
Link *next;
} g[1405799], *header[MAXN];
void AddLink(const int x, const int prime, const int times)
{
static int LinkSize;
Link* const node = g+(LinkSize++);
node->prime = prime;
node->times = times;
node->next = header[x];
header[x] = node;
}
int n, q, primes, prime[MAXN];
unsigned long long h[MAXN], p[MAXN] = {1};
char s[MAXN];
void GetPrimes(const int n)
{
static bool com[MAXN];
for (int i = 2; i <= n; ++i)
{
if (!com[i]) prime[primes++] = i;
for (int j = 0; j < primes && i*prime[j] <= n; ++j)
{
com[i*prime[j]] = true;
if (i%prime[j] == 0) break;
}
}
}
int main()
{
scanf("%d%s%d", &n, s+1, &q);
for (int i = 1; i <= n; ++i) p[i] = p[i-1]*P;
for (int i = 1; i <= n; ++i) h[i] = h[i-1]*P+s[i]-'a';
GetPrimes(n);
for (int i = 2, j, x, cnt; i <= n; ++i)
{
for (x = i, j = 0; j < primes && prime[j]*prime[j] <= x; ++j)
{
for (cnt = 0; x % prime[j] == 0; x /= prime[j]) ++cnt;
if (cnt) AddLink(i, prime[j], cnt);
}
if (x) AddLink(i, x, 1);
}
for (int a, b; q--; )
{
scanf("%d%d", &a, &b);
int ans = b-a+1;
for (Link *e = header[b-a+1]; e; e = e->next)
{
int x = 1;
for (int i = 0; i < e->times; ++i) x *= e->prime;
for (; x > 1; x /= e->prime)
{
const int l = (b-a+1)/x;
const unsigned long long tmp1 = h[b-l] - h[a-1]*p[b-a+1-l];
const unsigned long long tmp2 = h[b] - h[a+l-1]*p[b-a+1-l];
if (tmp1 == tmp2) { ans /= x; break; }
}
}
printf("%d\n", ans);
}
}
|