64122023-11-28 16:51:54szilPeriodikus Szavakcpp17Wrong answer 41/100331ms165736 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 = 200;
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(l, l+period-1) != calc_hash(l+period, l+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
2Accepted3ms3072 KiB
3Accepted4ms3468 KiB
4Accepted3ms3440 KiB
5Accepted4ms3680 KiB
6Accepted3ms3696 KiB
7Accepted3ms3844 KiB
8Accepted4ms4104 KiB
9Accepted4ms4316 KiB
10Accepted4ms4440 KiB
11Accepted4ms4680 KiB
12Accepted4ms4772 KiB
subtask327/27
13Accepted6ms6220 KiB
14Accepted6ms6192 KiB
15Accepted6ms6156 KiB
16Accepted4ms5172 KiB
17Accepted4ms6316 KiB
18Accepted6ms6408 KiB
19Accepted6ms6408 KiB
20Accepted6ms6412 KiB
21Accepted6ms6696 KiB
22Accepted6ms6624 KiB
subtask40/59
23Accepted32ms21216 KiB
24Accepted8ms8544 KiB
25Accepted28ms19660 KiB
26Accepted32ms21496 KiB
27Accepted32ms21404 KiB
28Wrong answer32ms21632 KiB
29Wrong answer32ms21712 KiB
30Accepted32ms21600 KiB
31Accepted293ms165312 KiB
32Accepted310ms165320 KiB
33Accepted59ms37636 KiB
34Accepted275ms147904 KiB
35Accepted293ms165324 KiB
36Accepted293ms165328 KiB
37Accepted331ms165440 KiB
38Wrong answer323ms165736 KiB
39Wrong answer321ms165660 KiB
40Wrong answer303ms165548 KiB
41Wrong answer301ms165532 KiB