91462024-02-15 21:55:29111Periodikus Szavakcpp17Time limit exceeded 41/100583ms9716 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1952 KiB
subtask214/14
2Accepted3ms2100 KiB
3Accepted3ms2320 KiB
4Accepted3ms2488 KiB
5Accepted3ms2724 KiB
6Accepted3ms2792 KiB
7Accepted3ms2904 KiB
8Accepted3ms3000 KiB
9Accepted3ms3228 KiB
10Accepted3ms3196 KiB
11Accepted3ms3432 KiB
12Accepted3ms3412 KiB
subtask327/27
13Accepted7ms3568 KiB
14Accepted7ms3616 KiB
15Accepted7ms3508 KiB
16Accepted3ms3560 KiB
17Accepted6ms3732 KiB
18Accepted7ms3744 KiB
19Accepted7ms3900 KiB
20Accepted4ms3976 KiB
21Accepted7ms4232 KiB
22Accepted8ms4180 KiB
subtask40/59
23Accepted72ms4716 KiB
24Accepted13ms4172 KiB
25Accepted64ms4488 KiB
26Accepted74ms4676 KiB
27Accepted17ms4600 KiB
28Accepted120ms4848 KiB
29Accepted138ms4808 KiB
30Accepted72ms4960 KiB
31Time limit exceeded570ms6156 KiB
32Time limit exceeded583ms6336 KiB
33Accepted168ms5396 KiB
34Time limit exceeded558ms6164 KiB
35Time limit exceeded558ms6452 KiB
36Time limit exceeded550ms6424 KiB
37Accepted172ms9716 KiB
38Time limit exceeded561ms6516 KiB
39Time limit exceeded526ms6616 KiB
40Time limit exceeded566ms6436 KiB
41Time limit exceeded558ms6656 KiB