9146 | 2024-02-15 21:55:29 | 111 | Periodikus Szavak | cpp17 | Time limit exceeded 41/100 | 583ms | 9716 KiB |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define MOD 1000000007
#define BASE 256
int pow_mod(int x, int p) {
int r = 1;
while (p > 0) {
if (p % 2 == 1) {
r *= x;
r %= MOD;
}
p /= 2;
x *= x;
x %= MOD;
}
return r;
}
int inv_mod(int x) {
return pow_mod(x, MOD - 2);
}
int geo_mod(int d, int n) {
return (pow_mod(d, n) - 1) * inv_mod(d - 1) % MOD;
}
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<int> s(N + 1);
p[0] = 1;
for (int i = 0; i < N; i++) {
h[i + 1] = (h[i] * BASE + S[i]) % MOD;
p[i + 1] = (p[i] * BASE) % MOD;
s[i + 1] = (s[i] + p[i]) % MOD;
}
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 * geo_mod(pow_mod(BASE, i), (r - l) / i) % MOD;
y += h[l] * p[r - l];
y %= MOD;
return y == h[r];
};
if (check(1)) {
cout << "YES" << '\n';
continue;
}
for (int i = 2; i * i < r - l; i++) {
if ((r - l) % i) {
continue;
}
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 | 1952 KiB | ||||
subtask2 | 14/14 | ||||||
2 | Accepted | 3ms | 2100 KiB | ||||
3 | Accepted | 3ms | 2320 KiB | ||||
4 | Accepted | 3ms | 2488 KiB | ||||
5 | Accepted | 3ms | 2724 KiB | ||||
6 | Accepted | 3ms | 2792 KiB | ||||
7 | Accepted | 3ms | 2904 KiB | ||||
8 | Accepted | 3ms | 3000 KiB | ||||
9 | Accepted | 3ms | 3228 KiB | ||||
10 | Accepted | 3ms | 3196 KiB | ||||
11 | Accepted | 3ms | 3432 KiB | ||||
12 | Accepted | 3ms | 3412 KiB | ||||
subtask3 | 27/27 | ||||||
13 | Accepted | 7ms | 3568 KiB | ||||
14 | Accepted | 7ms | 3616 KiB | ||||
15 | Accepted | 7ms | 3508 KiB | ||||
16 | Accepted | 3ms | 3560 KiB | ||||
17 | Accepted | 6ms | 3732 KiB | ||||
18 | Accepted | 7ms | 3744 KiB | ||||
19 | Accepted | 7ms | 3900 KiB | ||||
20 | Accepted | 4ms | 3976 KiB | ||||
21 | Accepted | 7ms | 4232 KiB | ||||
22 | Accepted | 8ms | 4180 KiB | ||||
subtask4 | 0/59 | ||||||
23 | Accepted | 72ms | 4716 KiB | ||||
24 | Accepted | 13ms | 4172 KiB | ||||
25 | Accepted | 64ms | 4488 KiB | ||||
26 | Accepted | 74ms | 4676 KiB | ||||
27 | Accepted | 17ms | 4600 KiB | ||||
28 | Accepted | 120ms | 4848 KiB | ||||
29 | Accepted | 138ms | 4808 KiB | ||||
30 | Accepted | 72ms | 4960 KiB | ||||
31 | Time limit exceeded | 570ms | 6156 KiB | ||||
32 | Time limit exceeded | 583ms | 6336 KiB | ||||
33 | Accepted | 168ms | 5396 KiB | ||||
34 | Time limit exceeded | 558ms | 6164 KiB | ||||
35 | Time limit exceeded | 558ms | 6452 KiB | ||||
36 | Time limit exceeded | 550ms | 6424 KiB | ||||
37 | Accepted | 172ms | 9716 KiB | ||||
38 | Time limit exceeded | 561ms | 6516 KiB | ||||
39 | Time limit exceeded | 526ms | 6616 KiB | ||||
40 | Time limit exceeded | 566ms | 6436 KiB | ||||
41 | Time limit exceeded | 558ms | 6656 KiB |