64132023-11-28 16:53:14szilPeriodikus Szavakcpp17Accepted 100/100331ms87412 KiB


#include <bits/stdc++.h>
 
using namespace std;
using ll = long long;
 
const int MAXN = 100'001;
const ll MOD = 1e9+7;
const int SQ = 100;
const ll P = 31;
 
ll H[MAXN], inv[MAXN];
int cnt[MAXN][SQ];
char s[MAXN];
int szita[MAXN];
 
ll bpow(ll a, ll k) {
    if (k == 0) return 1;
    if (k & 1) return (bpow(a, k-1) * a) % MOD;
    ll x = bpow(a, k/2);
    return (x*x)%MOD;
}
 
ll calc_hash(int l, int r) {
    return (((H[r] - H[l-1]) * inv[l-1]) % MOD + MOD) % MOD;
}
 
bool check(int l, int r, int period) {
    int len = r-l+1;
    if (len == period) return false;
 
    if (period < SQ) {
        return cnt[l][period] >= len/period;
    } else {
        for (int i = l; i <= r-period; i+=period) {
            if (calc_hash(i, i+period-1) != calc_hash(i+period, i+2*period-1)) return false;
        }
        return true;
    }
}
 
int main() {
    ios::sync_with_stdio(0); cin.tie(0);
 
    for (int i = 2; i < MAXN; i++) {
        if (szita[i] == 0) {
            for (int j = 2*i; j < MAXN; j+=i) {
                szita[j] = i;
            }
        }
    }
 
    int n; cin >> n;
    for (int i = 1; i <= n; i++) cin >> s[i];
 
    ll x = 1;
    inv[0] = 1;
    for (int i = 1; i <= n; i++) {
        H[i] = (H[i-1] + (s[i] - 'a') * x) % MOD;
        x = (x * P) % MOD;
        inv[i] = bpow(x, MOD-2);
    }
    
    for (int i = n; i >= 1; i--) {
        for (int j = 1; j < SQ; j++) {
            if (i+2*j-1 <= n) {
                if (calc_hash(i, i+j-1) == calc_hash(i+j, i+2*j-1)) {
                    cnt[i][j] = cnt[i+j][j]+1;
                } else {
                    cnt[i][j] = 1;
                }
            }
            else {
                cnt[i][j] = (i+j-1 <= n ? 1 : 0);
            }
        }
    }
 
    int q; cin >> q;
    while (q--) {
        int l, r; cin >> l >> r; l++; r++;
        int len = r-l+1; int x = len;
        bool g = false;
 
        vector<int> to_check = {1};
        while (szita[x] != 0) {
            to_check.emplace_back(len/szita[x]);
            x /= szita[x];
        }
        to_check.emplace_back(len/x);
 
        for (int period : to_check) {
            if (check(l, r, period)) g = true;
        }
        
        cout << (g?"YES":"NO") << "\n";
    }
    return 0;

}

SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted4ms2752 KiB
subtask214/14
2Accepted3ms2944 KiB
3Accepted4ms3196 KiB
4Accepted4ms3412 KiB
5Accepted4ms3620 KiB
6Accepted4ms3796 KiB
7Accepted4ms4008 KiB
8Accepted4ms4300 KiB
9Accepted4ms4480 KiB
10Accepted4ms4688 KiB
11Accepted3ms4644 KiB
12Accepted4ms4872 KiB
subtask327/27
13Accepted4ms5744 KiB
14Accepted4ms5788 KiB
15Accepted4ms6044 KiB
16Accepted4ms5412 KiB
17Accepted4ms5964 KiB
18Accepted4ms5996 KiB
19Accepted4ms5948 KiB
20Accepted4ms5920 KiB
21Accepted6ms6064 KiB
22Accepted4ms6332 KiB
subtask459/59
23Accepted23ms13608 KiB
24Accepted7ms6924 KiB
25Accepted20ms12544 KiB
26Accepted23ms13632 KiB
27Accepted24ms13584 KiB
28Accepted24ms13720 KiB
29Accepted24ms13716 KiB
30Accepted23ms13768 KiB
31Accepted209ms87168 KiB
32Accepted209ms87168 KiB
33Accepted41ms22220 KiB
34Accepted179ms78480 KiB
35Accepted209ms87312 KiB
36Accepted210ms87328 KiB
37Accepted331ms87412 KiB
38Accepted224ms87320 KiB
39Accepted212ms87320 KiB
40Accepted217ms87320 KiB
41Accepted216ms87400 KiB