91512024-02-15 22:20:28111Periodikus Szavakcpp17Accepted 100/100358ms71320 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
subtask214/14
2Accepted3ms2056 KiB
3Accepted3ms2436 KiB
4Accepted3ms2712 KiB
5Accepted3ms2952 KiB
6Accepted3ms2996 KiB
7Accepted3ms3136 KiB
8Accepted3ms3208 KiB
9Accepted3ms3552 KiB
10Accepted3ms3664 KiB
11Accepted3ms3848 KiB
12Accepted3ms3768 KiB
subtask327/27
13Accepted4ms4388 KiB
14Accepted3ms4472 KiB
15Accepted3ms4476 KiB
16Accepted3ms4132 KiB
17Accepted4ms4532 KiB
18Accepted4ms4680 KiB
19Accepted3ms4532 KiB
20Accepted3ms4472 KiB
21Accepted4ms4704 KiB
22Accepted4ms4788 KiB
subtask459/59
23Accepted14ms10156 KiB
24Accepted4ms5276 KiB
25Accepted14ms9680 KiB
26Accepted14ms10316 KiB
27Accepted12ms10312 KiB
28Accepted19ms10364 KiB
29Accepted20ms10412 KiB
30Accepted14ms10368 KiB
31Accepted272ms71260 KiB
32Accepted231ms71188 KiB
33Accepted32ms16676 KiB
34Accepted196ms63464 KiB
35Accepted230ms71292 KiB
36Accepted229ms71184 KiB
37Accepted138ms71176 KiB
38Accepted293ms71224 KiB
39Accepted319ms71184 KiB
40Accepted342ms71320 KiB
41Accepted358ms71312 KiB