답안 #1013610

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1013610 2024-07-03T17:22:11 Z andrei_iorgulescu Brperm (RMI20_brperm) C++14
100 / 100
461 ms 59732 KB
#include "brperm.h"
#include <bits/stdc++.h>

using namespace std;

using integer = int;

int n;
char a[500005];
int h[20][500005];///hashul pentru prefix de 2^p din sirul gen 0,8,4,12,2,10,6,14
bool ans[20][500005];///pentru k = i, p = j
int pref_h[500005];

int base = 37;
int modulo = 1e9 + 123;

int powbase[500005],invpowbase[500005];

int lgpow(int x,int y)
{
    int z = 1;
    while (y != 0)
    {
        if (y % 2 == 1)
            z = 1ll * z * x % modulo;
        x = 1ll * x * x % modulo;
        y /= 2;
    }
    return z;
}

void init(integer N, const char S[])
{
    powbase[0] = invpowbase[0] = 1;
    for (int i = 1; i <= 500000; i++)
    {
        powbase[i] = 1ll * powbase[i - 1] * base % modulo;
        invpowbase[i] = lgpow(powbase[i],modulo - 2);
    }
    n = N;
    for (int i = 1; i <= n; i++)
        a[i] = S[i - 1];
    for (int i = 1; i <= n; i++)
        ans[0][i] = true;
    for (int k = 1; k <= 18; k++)
    {
        memset(h,0,sizeof(h));
        memset(pref_h,0,sizeof(pref_h));
        for (int i = 1; i <= n; i++)
            h[0][i] = (a[i] - 'a' + 1);
        for (int j = 1; j <= k; j++)
            for (int i = 1; i + (1 << k) - (1 << (k - j)) <= n; i++)
                h[j][i] = ((long long)h[j - 1][i] + 1ll * powbase[(1 << (j - 1))] * h[j - 1][i + (1 << (k - j))]) % modulo;
        for (int i = 1; i <= n; i++)
            pref_h[i] = ((long long)pref_h[i - 1] + 1ll * powbase[i - 1] * (a[i] - 'a' + 1)) % modulo;
        for (int i = 1; i <= n - (1 << k) + 1; i++)
        {
            int hh = (modulo + pref_h[i + (1 << k) - 1] - pref_h[i - 1]) % modulo;
            hh = 1ll * hh * invpowbase[i - 1] % modulo;
            //cout << i << ' ' << hh << ' ' << h[k][i] << endl;
            if (hh == h[k][i])
                ans[k][i] = true;
        }
    }
}

integer query(integer i,integer k)
{
    i++;
    if (ans[k][i])
        return (integer)1;
    return (integer)0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 45392 KB Output is correct
2 Correct 113 ms 45488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 45392 KB Output is correct
2 Correct 113 ms 45488 KB Output is correct
3 Correct 166 ms 47156 KB Output is correct
4 Correct 174 ms 47184 KB Output is correct
5 Correct 181 ms 47232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 446 ms 59732 KB Output is correct
2 Correct 461 ms 56468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 45392 KB Output is correct
2 Correct 113 ms 45488 KB Output is correct
3 Correct 166 ms 47156 KB Output is correct
4 Correct 174 ms 47184 KB Output is correct
5 Correct 181 ms 47232 KB Output is correct
6 Correct 446 ms 59732 KB Output is correct
7 Correct 461 ms 56468 KB Output is correct
8 Correct 430 ms 54232 KB Output is correct
9 Correct 435 ms 54360 KB Output is correct
10 Correct 405 ms 54396 KB Output is correct