Submission #1034706

# Submission time Handle Problem Language Result Execution time Memory
1034706 2024-07-25T16:56:09 Z andrei_iorgulescu Brperm (RMI20_brperm) C++14
100 / 100
296 ms 79512 KB
#include <bits/stdc++.h>
#include "brperm.h"

using namespace std;

const int max_size = 5e5 + 3, baza = 29, mod = 1e9 + 7, rmax = 20;

int v[max_size], hsh[rmax][max_size], hshinv[rmax][max_size], ptr[rmax + 5], invptr[rmax + 5], nn;

void init ( int n, const char s[])
{
	nn = n;
    for (int i = 1; i <= n; i++)
    {
        v[i] = (s[i - 1] - 'a') + 1;
        hsh[0][i] = v[i];
        hshinv[0][i] = v[i];
    }
    ptr[0] = 1;
    ptr[1] = baza;
    invptr[0] = 1;
    invptr[1] = 758620695;
    for (int i = 2; i <= rmax; i++)
    {
        ptr[i] = (1LL * ptr[i - 1] * ptr[i - 1]) % mod;
        invptr[i] = (1LL * invptr[i - 1] * invptr[i - 1]) % mod;
    }
    for (int e = 1; e < rmax; e++)
    {
        if (n < (1 << e))
        {
            continue;
        }
        int vf = 1;
        for (int i = 1; i < (1 << e); i++)
        {
            vf = (1LL * vf * ptr[rmax - e]) % mod;
        }
        for (int i = n - (1 << e) + 1; i <= n; i++)
        {
            hsh[e][n - (1 << e) + 1] = ((1LL * hsh[e][n - (1 << e) + 1] * ptr[rmax - e]) % mod + v[i]) % mod;
        }
        for (int i = n - (1 << e); i > 0; i--)
        {
            int x = hsh[e][i + 1];
            x = (x + mod - v[i + (1 << e)]) % mod;
            x = (1LL * x * invptr[rmax - e]) % mod;
            x = (x + (1LL * vf * v[i]) % mod) % mod;
            hsh[e][i] = x;
        }
        for (int i = 1; i + (1 << e) - 1 <= n; i++)
        {
            hshinv[e][i] = ((1LL * hshinv[e - 1][i] * ptr[rmax - e]) % mod + hshinv[e - 1][i + (1 << (e - 1))]) % mod;
        }
    }
}

int query ( int x, int k)
{
    x++;
    //out << hsh[k][x] << " " << hshinv[k][x] << '\n';
	if (x + (1 << k) - 1 > nn)
	{
		return 0;
	}
    if (hsh[k][x] == hshinv[k][x])
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 48 ms 14336 KB Output is correct
4 Correct 46 ms 14420 KB Output is correct
5 Correct 46 ms 14416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 259 ms 79512 KB Output is correct
2 Correct 279 ms 79124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 48 ms 14336 KB Output is correct
4 Correct 46 ms 14420 KB Output is correct
5 Correct 46 ms 14416 KB Output is correct
6 Correct 259 ms 79512 KB Output is correct
7 Correct 279 ms 79124 KB Output is correct
8 Correct 296 ms 79180 KB Output is correct
9 Correct 273 ms 79120 KB Output is correct
10 Correct 274 ms 79156 KB Output is correct