9151 | 2024-02-15 22:20:28 | 111 | Periodikus Szavak | cpp17 | Accepted 100/100 | 358ms | 71320 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)) {
ans = 1;
break;
}
}
cout << (ans ? "YES" : "NO") << '\n';
}
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1828 KiB | ||||
subtask2 | 14/14 | ||||||
2 | Accepted | 3ms | 2056 KiB | ||||
3 | Accepted | 3ms | 2436 KiB | ||||
4 | Accepted | 3ms | 2712 KiB | ||||
5 | Accepted | 3ms | 2952 KiB | ||||
6 | Accepted | 3ms | 2996 KiB | ||||
7 | Accepted | 3ms | 3136 KiB | ||||
8 | Accepted | 3ms | 3208 KiB | ||||
9 | Accepted | 3ms | 3552 KiB | ||||
10 | Accepted | 3ms | 3664 KiB | ||||
11 | Accepted | 3ms | 3848 KiB | ||||
12 | Accepted | 3ms | 3768 KiB | ||||
subtask3 | 27/27 | ||||||
13 | Accepted | 4ms | 4388 KiB | ||||
14 | Accepted | 3ms | 4472 KiB | ||||
15 | Accepted | 3ms | 4476 KiB | ||||
16 | Accepted | 3ms | 4132 KiB | ||||
17 | Accepted | 4ms | 4532 KiB | ||||
18 | Accepted | 4ms | 4680 KiB | ||||
19 | Accepted | 3ms | 4532 KiB | ||||
20 | Accepted | 3ms | 4472 KiB | ||||
21 | Accepted | 4ms | 4704 KiB | ||||
22 | Accepted | 4ms | 4788 KiB | ||||
subtask4 | 59/59 | ||||||
23 | Accepted | 14ms | 10156 KiB | ||||
24 | Accepted | 4ms | 5276 KiB | ||||
25 | Accepted | 14ms | 9680 KiB | ||||
26 | Accepted | 14ms | 10316 KiB | ||||
27 | Accepted | 12ms | 10312 KiB | ||||
28 | Accepted | 19ms | 10364 KiB | ||||
29 | Accepted | 20ms | 10412 KiB | ||||
30 | Accepted | 14ms | 10368 KiB | ||||
31 | Accepted | 272ms | 71260 KiB | ||||
32 | Accepted | 231ms | 71188 KiB | ||||
33 | Accepted | 32ms | 16676 KiB | ||||
34 | Accepted | 196ms | 63464 KiB | ||||
35 | Accepted | 230ms | 71292 KiB | ||||
36 | Accepted | 229ms | 71184 KiB | ||||
37 | Accepted | 138ms | 71176 KiB | ||||
38 | Accepted | 293ms | 71224 KiB | ||||
39 | Accepted | 319ms | 71184 KiB | ||||
40 | Accepted | 342ms | 71320 KiB | ||||
41 | Accepted | 358ms | 71312 KiB |