9150 | 2024-02-15 22:18:34 | 111 | Periodikus Szavak | cpp17 | Time limit exceeded 41/100 | 541ms | 70344 KiB |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define MOD 1000000007
#define BASE 256
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
#ifdef CB
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
int N;
string S;
cin >> N >> S;
vector<int> h(N + 1);
vector<int> p(N + 1);
vector<vector<int>> g(N + 1);
p[0] = 1;
for (int i = 1; i <= N; i++) {
h[i] = (h[i - 1] * BASE + S[i - 1]) % MOD;
p[i] = (p[i - 1] * BASE) % MOD;
g[i].resize(N / i + 1);
g[i][0] = 1;
for (int j = 1, k = p[i]; i * j <= N; j++, k = k * p[i] % MOD) {
g[i][j] = (g[i][j - 1] + k) % MOD;
}
}
vector<vector<int>> f(N + 1);
for (int i = 1; i <= N; i++) {
f[i].reserve(8);
}
for (int i = 2; i <= N; i++) {
for (int j = 2; i * j <= N; j++) {
f[i * j].push_back(i);
}
}
int Q;
cin >> Q;
while (Q--) {
int l, r;
cin >> l >> r;
r++;
int ans = 0;
auto check = [&](int i) {
int x = ((h[l + i] - h[l] * p[i]) % MOD + MOD) % MOD;
int y = 0;
y += x * g[i][(r - l) / i - 1] % MOD;
y += h[l] * p[r - l];
y %= MOD;
return y == h[r];
};
if (check(1)) {
cout << "YES" << '\n';
continue;
}
for (int i : f[r - l]) {
if (check(i) || check((r - l) / i)) {
ans = 1;
break;
}
}
cout << (ans ? "YES" : "NO") << '\n';
}
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1824 KiB | ||||
subtask2 | 14/14 | ||||||
2 | Accepted | 3ms | 2064 KiB | ||||
3 | Accepted | 3ms | 2440 KiB | ||||
4 | Accepted | 3ms | 2480 KiB | ||||
5 | Accepted | 3ms | 2436 KiB | ||||
6 | Accepted | 3ms | 2552 KiB | ||||
7 | Accepted | 3ms | 2768 KiB | ||||
8 | Accepted | 3ms | 2856 KiB | ||||
9 | Accepted | 3ms | 3200 KiB | ||||
10 | Accepted | 2ms | 3072 KiB | ||||
11 | Accepted | 3ms | 3076 KiB | ||||
12 | Accepted | 3ms | 3160 KiB | ||||
subtask3 | 27/27 | ||||||
13 | Accepted | 4ms | 3644 KiB | ||||
14 | Accepted | 4ms | 3556 KiB | ||||
15 | Accepted | 4ms | 3668 KiB | ||||
16 | Accepted | 3ms | 3200 KiB | ||||
17 | Accepted | 4ms | 3516 KiB | ||||
18 | Accepted | 4ms | 3568 KiB | ||||
19 | Accepted | 4ms | 3576 KiB | ||||
20 | Accepted | 3ms | 3708 KiB | ||||
21 | Accepted | 4ms | 3936 KiB | ||||
22 | Accepted | 4ms | 3928 KiB | ||||
subtask4 | 0/59 | ||||||
23 | Accepted | 19ms | 9352 KiB | ||||
24 | Accepted | 4ms | 4528 KiB | ||||
25 | Accepted | 17ms | 8692 KiB | ||||
26 | Accepted | 18ms | 9348 KiB | ||||
27 | Accepted | 10ms | 9356 KiB | ||||
28 | Accepted | 27ms | 9352 KiB | ||||
29 | Accepted | 29ms | 9352 KiB | ||||
30 | Accepted | 18ms | 9348 KiB | ||||
31 | Accepted | 321ms | 70232 KiB | ||||
32 | Accepted | 291ms | 70280 KiB | ||||
33 | Accepted | 41ms | 15928 KiB | ||||
34 | Accepted | 250ms | 62520 KiB | ||||
35 | Accepted | 294ms | 70344 KiB | ||||
36 | Accepted | 293ms | 70232 KiB | ||||
37 | Accepted | 141ms | 70240 KiB | ||||
38 | Accepted | 388ms | 70232 KiB | ||||
39 | Accepted | 451ms | 70240 KiB | ||||
40 | Accepted | 465ms | 70232 KiB | ||||
41 | Time limit exceeded | 541ms | 36200 KiB |