91502024-02-15 22:18:34111Periodikus Szavakcpp17Time limit exceeded 41/100541ms70344 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1824 KiB
subtask214/14
2Accepted3ms2064 KiB
3Accepted3ms2440 KiB
4Accepted3ms2480 KiB
5Accepted3ms2436 KiB
6Accepted3ms2552 KiB
7Accepted3ms2768 KiB
8Accepted3ms2856 KiB
9Accepted3ms3200 KiB
10Accepted2ms3072 KiB
11Accepted3ms3076 KiB
12Accepted3ms3160 KiB
subtask327/27
13Accepted4ms3644 KiB
14Accepted4ms3556 KiB
15Accepted4ms3668 KiB
16Accepted3ms3200 KiB
17Accepted4ms3516 KiB
18Accepted4ms3568 KiB
19Accepted4ms3576 KiB
20Accepted3ms3708 KiB
21Accepted4ms3936 KiB
22Accepted4ms3928 KiB
subtask40/59
23Accepted19ms9352 KiB
24Accepted4ms4528 KiB
25Accepted17ms8692 KiB
26Accepted18ms9348 KiB
27Accepted10ms9356 KiB
28Accepted27ms9352 KiB
29Accepted29ms9352 KiB
30Accepted18ms9348 KiB
31Accepted321ms70232 KiB
32Accepted291ms70280 KiB
33Accepted41ms15928 KiB
34Accepted250ms62520 KiB
35Accepted294ms70344 KiB
36Accepted293ms70232 KiB
37Accepted141ms70240 KiB
38Accepted388ms70232 KiB
39Accepted451ms70240 KiB
40Accepted465ms70232 KiB
41Time limit exceeded541ms36200 KiB