Luckyleaves's Blog

stay hungry,stay foolish greedy and lucky

P3538

自己伞兵了。

Solution

看见字符串循环节,果断hash,不会看图。

image

之后记录每个数的最小质因子剪枝,别用map,然后就A了。

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
#include<bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &res)
{
res = 0; bool flag = 0;
char c = getchar();
while('0' > c || c > '9') { if(c == '-') flag = 1; c = getchar();}
while('0' <= c && c <= '9') res = (res << 3) + (res << 1) + (c ^ 48), c = getchar();
if(flag) res = -res;
}
template <class T, class ...Arg>
inline void read(T &res, Arg &...com){ read(res), read(com...);}
template <class T>
void write(T res)
{
if(res > 9) write(res / 10);
putchar(res % 10 + '0');
}
const int N = 2e6 + 5;
int n, m, l, r;
char c[N];
int ne[N];
unsigned int a[N], pw[N];
int primes[N], p[N], least[N], cnt, ans;
void init()
{
for(int i = 2;i < N;i ++)
{
if(!p[i]) primes[++ cnt] = i, least[i] = i;
for(int j = 1; primes[j] * i < N && j <= cnt;j ++)
p[primes[j] * i] = 1, least[primes[j] * i] = min(least[primes[j] * i], primes[j]);
}
}
inline unsigned int calc(int l, int r)
{
return a[r] - a[l - 1] * pw[r - l + 1];
}
int main()
{
memset(least, 0x3f, sizeof(least));
init();
read(n);
scanf("%s", c + 1);
for(int i = 2, j = 0;i <= n;i ++)
{
while(j && c[j + 1] != c[i]) j = ne[j];
if(c[j + 1] == c[i]) j ++;
ne[i] = j;
}
for(int i = 1;i <= n;i ++)
a[i] = a[i - 1] * 31 + c[i];
pw[0] = 1;
for(int i = 1;i <= n;i ++)
pw[i] = pw[i - 1] * 31;
read(m);
while(m --)
{
read(l, r);
ans = cnt = r - l + 1;
while(cnt > 1)
{
if(calc(l, r - ans / least[cnt]) == calc(l + ans / least[cnt], r)) ans /= least[cnt];
cnt /= least[cnt];
}
write(ans), puts("");
}
return 0;
}