1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include <cstdio> #include <algorithm> #include <string> using namespace std; typedef long long LL; const int N = 2e6 + 10, mod = 19930726; char s[N]; int n, d[N], f[N]; LL m; void init() { s[n++] = '$', s[n++] = '#'; char c = getchar(); while (c < 'a' || c > 'z') c = getchar(); while (c >= 'a' && c <= 'z') s[n++] = c, s[n++] = '#', c = getchar(); s[n++] = '^'; } int binpow (int b, int k) { int res = 1; for (; k; k >>= 1, b = (LL)b * b % mod) if (k & 1) res = (LL)res * b % mod; return res; } int main() { int _n; scanf("%d %lld\n", &_n, &m); init(); for (int i = 0, l = 0, r = -1; i < n; i++) { int k = i <= r ? min(d[l + r - i], r - i + 1) : 1; while (i - k >= 0 && i + k < n && s[i - k] == s[i + k]) ++k; d[i] = k; if (i + k - 1 > r) l = i - k + 1, r = i + k - 1; } for (int i = 0; i < n; i++) ++f[d[i] - 1]; n = _n; for (int i = n - 2; i; --i) f[i] += f[i + 2]; int res = 1; for (int i = n - !(n & 1); i > 0; i -= 2) { if (m <= f[i]) res = (LL)res * binpow(i, m) % mod, m = 0; else res = (LL)res * binpow(i, f[i]) % mod, m -= f[i]; } printf("%d", m ? -1 : res); return 0; }
|