6413 | 2023-11-28 16:53:14 | szil | Periodikus Szavak | cpp17 | Elfogadva 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;
}
Részfeladat | Összpont | Teszt | Verdikt | Idő | Memória | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Elfogadva | 4ms | 2752 KiB | ||||
subtask2 | 14/14 | ||||||
2 | Elfogadva | 3ms | 2944 KiB | ||||
3 | Elfogadva | 4ms | 3196 KiB | ||||
4 | Elfogadva | 4ms | 3412 KiB | ||||
5 | Elfogadva | 4ms | 3620 KiB | ||||
6 | Elfogadva | 4ms | 3796 KiB | ||||
7 | Elfogadva | 4ms | 4008 KiB | ||||
8 | Elfogadva | 4ms | 4300 KiB | ||||
9 | Elfogadva | 4ms | 4480 KiB | ||||
10 | Elfogadva | 4ms | 4688 KiB | ||||
11 | Elfogadva | 3ms | 4644 KiB | ||||
12 | Elfogadva | 4ms | 4872 KiB | ||||
subtask3 | 27/27 | ||||||
13 | Elfogadva | 4ms | 5744 KiB | ||||
14 | Elfogadva | 4ms | 5788 KiB | ||||
15 | Elfogadva | 4ms | 6044 KiB | ||||
16 | Elfogadva | 4ms | 5412 KiB | ||||
17 | Elfogadva | 4ms | 5964 KiB | ||||
18 | Elfogadva | 4ms | 5996 KiB | ||||
19 | Elfogadva | 4ms | 5948 KiB | ||||
20 | Elfogadva | 4ms | 5920 KiB | ||||
21 | Elfogadva | 6ms | 6064 KiB | ||||
22 | Elfogadva | 4ms | 6332 KiB | ||||
subtask4 | 59/59 | ||||||
23 | Elfogadva | 23ms | 13608 KiB | ||||
24 | Elfogadva | 7ms | 6924 KiB | ||||
25 | Elfogadva | 20ms | 12544 KiB | ||||
26 | Elfogadva | 23ms | 13632 KiB | ||||
27 | Elfogadva | 24ms | 13584 KiB | ||||
28 | Elfogadva | 24ms | 13720 KiB | ||||
29 | Elfogadva | 24ms | 13716 KiB | ||||
30 | Elfogadva | 23ms | 13768 KiB | ||||
31 | Elfogadva | 209ms | 87168 KiB | ||||
32 | Elfogadva | 209ms | 87168 KiB | ||||
33 | Elfogadva | 41ms | 22220 KiB | ||||
34 | Elfogadva | 179ms | 78480 KiB | ||||
35 | Elfogadva | 209ms | 87312 KiB | ||||
36 | Elfogadva | 210ms | 87328 KiB | ||||
37 | Elfogadva | 331ms | 87412 KiB | ||||
38 | Elfogadva | 224ms | 87320 KiB | ||||
39 | Elfogadva | 212ms | 87320 KiB | ||||
40 | Elfogadva | 217ms | 87320 KiB | ||||
41 | Elfogadva | 216ms | 87400 KiB |