This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |