6412 | 2023-11-28 16:51:54 | szil | Periodikus Szavak | cpp17 | Wrong answer 41/100 | 331ms | 165736 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 = 200;
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(l, l+period-1) != calc_hash(l+period, l+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 | 3072 KiB | ||||
3 | Accepted | 4ms | 3468 KiB | ||||
4 | Accepted | 3ms | 3440 KiB | ||||
5 | Accepted | 4ms | 3680 KiB | ||||
6 | Accepted | 3ms | 3696 KiB | ||||
7 | Accepted | 3ms | 3844 KiB | ||||
8 | Accepted | 4ms | 4104 KiB | ||||
9 | Accepted | 4ms | 4316 KiB | ||||
10 | Accepted | 4ms | 4440 KiB | ||||
11 | Accepted | 4ms | 4680 KiB | ||||
12 | Accepted | 4ms | 4772 KiB | ||||
subtask3 | 27/27 | ||||||
13 | Accepted | 6ms | 6220 KiB | ||||
14 | Accepted | 6ms | 6192 KiB | ||||
15 | Accepted | 6ms | 6156 KiB | ||||
16 | Accepted | 4ms | 5172 KiB | ||||
17 | Accepted | 4ms | 6316 KiB | ||||
18 | Accepted | 6ms | 6408 KiB | ||||
19 | Accepted | 6ms | 6408 KiB | ||||
20 | Accepted | 6ms | 6412 KiB | ||||
21 | Accepted | 6ms | 6696 KiB | ||||
22 | Accepted | 6ms | 6624 KiB | ||||
subtask4 | 0/59 | ||||||
23 | Accepted | 32ms | 21216 KiB | ||||
24 | Accepted | 8ms | 8544 KiB | ||||
25 | Accepted | 28ms | 19660 KiB | ||||
26 | Accepted | 32ms | 21496 KiB | ||||
27 | Accepted | 32ms | 21404 KiB | ||||
28 | Wrong answer | 32ms | 21632 KiB | ||||
29 | Wrong answer | 32ms | 21712 KiB | ||||
30 | Accepted | 32ms | 21600 KiB | ||||
31 | Accepted | 293ms | 165312 KiB | ||||
32 | Accepted | 310ms | 165320 KiB | ||||
33 | Accepted | 59ms | 37636 KiB | ||||
34 | Accepted | 275ms | 147904 KiB | ||||
35 | Accepted | 293ms | 165324 KiB | ||||
36 | Accepted | 293ms | 165328 KiB | ||||
37 | Accepted | 331ms | 165440 KiB | ||||
38 | Wrong answer | 323ms | 165736 KiB | ||||
39 | Wrong answer | 321ms | 165660 KiB | ||||
40 | Wrong answer | 303ms | 165548 KiB | ||||
41 | Wrong answer | 301ms | 165532 KiB |