6413 | 2023-11-28 16:53:14 | szil | Periodikus Szavak | cpp17 | Accepted 100/100 | 331ms | 87412 KiB |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 100'001;
const ll MOD = 1e9+7;
const int SQ = 100;
const ll P = 31;
ll H[MAXN], inv[MAXN];
int cnt[MAXN][SQ];
char s[MAXN];
int szita[MAXN];
ll bpow(ll a, ll k) {
if (k == 0) return 1;
if (k & 1) return (bpow(a, k-1) * a) % MOD;
ll x = bpow(a, k/2);
return (x*x)%MOD;
}
ll calc_hash(int l, int r) {
return (((H[r] - H[l-1]) * inv[l-1]) % MOD + MOD) % MOD;
}
bool check(int l, int r, int period) {
int len = r-l+1;
if (len == period) return false;
if (period < SQ) {
return cnt[l][period] >= len/period;
} else {
for (int i = l; i <= r-period; i+=period) {
if (calc_hash(i, i+period-1) != calc_hash(i+period, i+2*period-1)) return false;
}
return true;
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
for (int i = 2; i < MAXN; i++) {
if (szita[i] == 0) {
for (int j = 2*i; j < MAXN; j+=i) {
szita[j] = i;
}
}
}
int n; cin >> n;
for (int i = 1; i <= n; i++) cin >> s[i];
ll x = 1;
inv[0] = 1;
for (int i = 1; i <= n; i++) {
H[i] = (H[i-1] + (s[i] - 'a') * x) % MOD;
x = (x * P) % MOD;
inv[i] = bpow(x, MOD-2);
}
for (int i = n; i >= 1; i--) {
for (int j = 1; j < SQ; j++) {
if (i+2*j-1 <= n) {
if (calc_hash(i, i+j-1) == calc_hash(i+j, i+2*j-1)) {
cnt[i][j] = cnt[i+j][j]+1;
} else {
cnt[i][j] = 1;
}
}
else {
cnt[i][j] = (i+j-1 <= n ? 1 : 0);
}
}
}
int q; cin >> q;
while (q--) {
int l, r; cin >> l >> r; l++; r++;
int len = r-l+1; int x = len;
bool g = false;
vector<int> to_check = {1};
while (szita[x] != 0) {
to_check.emplace_back(len/szita[x]);
x /= szita[x];
}
to_check.emplace_back(len/x);
for (int period : to_check) {
if (check(l, r, period)) g = true;
}
cout << (g?"YES":"NO") << "\n";
}
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 4ms | 2752 KiB | ||||
subtask2 | 14/14 | ||||||
2 | Accepted | 3ms | 2944 KiB | ||||
3 | Accepted | 4ms | 3196 KiB | ||||
4 | Accepted | 4ms | 3412 KiB | ||||
5 | Accepted | 4ms | 3620 KiB | ||||
6 | Accepted | 4ms | 3796 KiB | ||||
7 | Accepted | 4ms | 4008 KiB | ||||
8 | Accepted | 4ms | 4300 KiB | ||||
9 | Accepted | 4ms | 4480 KiB | ||||
10 | Accepted | 4ms | 4688 KiB | ||||
11 | Accepted | 3ms | 4644 KiB | ||||
12 | Accepted | 4ms | 4872 KiB | ||||
subtask3 | 27/27 | ||||||
13 | Accepted | 4ms | 5744 KiB | ||||
14 | Accepted | 4ms | 5788 KiB | ||||
15 | Accepted | 4ms | 6044 KiB | ||||
16 | Accepted | 4ms | 5412 KiB | ||||
17 | Accepted | 4ms | 5964 KiB | ||||
18 | Accepted | 4ms | 5996 KiB | ||||
19 | Accepted | 4ms | 5948 KiB | ||||
20 | Accepted | 4ms | 5920 KiB | ||||
21 | Accepted | 6ms | 6064 KiB | ||||
22 | Accepted | 4ms | 6332 KiB | ||||
subtask4 | 59/59 | ||||||
23 | Accepted | 23ms | 13608 KiB | ||||
24 | Accepted | 7ms | 6924 KiB | ||||
25 | Accepted | 20ms | 12544 KiB | ||||
26 | Accepted | 23ms | 13632 KiB | ||||
27 | Accepted | 24ms | 13584 KiB | ||||
28 | Accepted | 24ms | 13720 KiB | ||||
29 | Accepted | 24ms | 13716 KiB | ||||
30 | Accepted | 23ms | 13768 KiB | ||||
31 | Accepted | 209ms | 87168 KiB | ||||
32 | Accepted | 209ms | 87168 KiB | ||||
33 | Accepted | 41ms | 22220 KiB | ||||
34 | Accepted | 179ms | 78480 KiB | ||||
35 | Accepted | 209ms | 87312 KiB | ||||
36 | Accepted | 210ms | 87328 KiB | ||||
37 | Accepted | 331ms | 87412 KiB | ||||
38 | Accepted | 224ms | 87320 KiB | ||||
39 | Accepted | 212ms | 87320 KiB | ||||
40 | Accepted | 217ms | 87320 KiB | ||||
41 | Accepted | 216ms | 87400 KiB |