QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#137149#6759. 字符串hos_lyric#100 ✓380ms50552kbC++1410.9kb2023-08-09 23:34:372023-08-09 23:34:39

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-09 23:34:39]
  • 评测
  • 测评结果:100
  • 用时:380ms
  • 内存:50552kb
  • [2023-08-09 23:34:37]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }

////////////////////////////////////////////////////////////////////////////////
// SA-IS
//   String: string, vector<int>, vector<long long>
//   if sigma <= n,  O(n) time, O(n) space
//   if sigma >  n,  O(n log n) time, O(n) space
template <class String> vector<int> suffixArrayRec(const String &as) {
  const int n = as.size();
  if (n == 0) return {};
  const auto minmaxA = minmax_element(as.begin(), as.end());
  const auto minA = *minmaxA.first, maxA = *minmaxA.second;
  if (static_cast<unsigned long long>(maxA) - minA >=
      static_cast<unsigned long long>(n)) {
    vector<int> us(n);
    for (int u = 0; u < n; ++u) us[u] = u;
    std::sort(us.begin(), us.end(), [&](int u, int v) -> bool {
      return (as[u] < as[v]);
    });
    int b = 0;
    vector<int> bs(n, 0);
    for (int i = 1; i < n; ++i) {
      if (as[us[i - 1]] != as[us[i]]) ++b;
      bs[us[i]] = b;
    }
    return suffixArrayRec(bs);
  }
  const int sigma = maxA - minA + 1;
  vector<int> pt(sigma + 1, 0), poss(sigma);
  for (int u = 0; u < n; ++u) ++pt[as[u] - minA + 1];
  for (int a = 0; a < sigma; ++a) pt[a + 1] += pt[a];
  // cmp[u] := (as[u, n) < as[u + 1, n))
  vector<bool> cmp(n);
  cmp[n - 1] = false;
  for (int u = n - 1; --u >= 0; ) {
    cmp[u] = (as[u] != as[u + 1]) ? (as[u] < as[u + 1]) : cmp[u + 1];
  }
  // ><,  nn - id (0 <= id < n)
  int nn = 0;
  vector<int> ids(n, 0);
  int last = n;
  vector<int> nxt(n, 0);
  // put ><, from the tail of each bucket
  vector<int> us(n, 0);
  memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
  for (int u = n - 1; --u >= 1; ) if (!cmp[u - 1] && cmp[u]) {
    ids[u] = ++nn;
    nxt[u] = last;
    last = u;
    us[--poss[as[u] - minA]] = u;
  }
  // follow > backwards, from the head of each bucket
  memcpy(poss.data(), pt.data(), sigma * sizeof(int));
  us[poss[as[n - 1] - minA]++] = n - 1;
  for (int i = 0; i < n; ++i) {
    const int u = us[i];
    if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1;
  }
  // follow < backwards, from the tail of each bucket
  memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
  for (int i = n; --i >= 0; ) {
    const int u = us[i];
    if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1;
  }
  if (nn) {
    int vsLen = 0;
    vector<int> vs(nn);
    for (const int u : us) if (ids[u]) vs[vsLen++] = u;
    int b = 0;
    vector<int> bs(nn, 0);
    for (int j = 1; j < nn; ++j) {
      // as[v1, w1] (< or =) as[v0, w0]
      int v1 = vs[j - 1], v0 = vs[j];
      const int w1 = nxt[v1], w0 = nxt[v0];
      if (w1 - v1 != w0 - v0) {
        ++b;
      } else {
        for (; ; ++v1, ++v0) {
          if (v1 == n) { ++b; break; }
          if (as[v1] != as[v0]) { ++b; break; }
          if (v1 == w1) break;
        }
      }
      bs[nn - ids[vs[j]]] = b;
    }
    for (int u = 0; u < n; ++u) if (ids[u]) vs[nn - ids[u]] = u;
    const auto sub = suffixArrayRec(bs);
    // put ><, from the tail of each bucket
    memset(us.data(), 0, n * sizeof(int));
    memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
    for (int j = nn; --j >= 0; ) {
      const int u = vs[sub[j]];
      us[--poss[as[u] - minA]] = u;
    }
    // follow > backwards, from the head of each bucket
    memcpy(poss.data(), pt.data(), sigma * sizeof(int));
    us[poss[as[n - 1] - minA]++] = n - 1;
    for (int i = 0; i < n; ++i) {
      const int u = us[i];
      if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1;
    }
    // follow < backwards, from the tail of each bucket
    memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
    for (int i = n; --i >= 0; ) {
      const int u = us[i];
      if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1;
    }
  }
  return us;
}

// us[i]: i-th suffix
// su[u]: index of as[u, n)
// hs[i]: longest common prefix of as[us[i - 1], n) and as[us[i], n)
struct SuffixArray {
  int n;
  bool rmq;
  vector<int> us, su, hs, bsr;
  SuffixArray() : n(0), rmq(false) {}
  SuffixArray(const string &as, bool rmq_) : rmq(rmq_) { build(as); }
  SuffixArray(const vector<int> &as, bool rmq_) : rmq(rmq_) { build(as); }
  SuffixArray(const vector<long long> &as, bool rmq_) : rmq(rmq_) { build(as); }
  template <class String> void build(const String &as) {
    n = as.size();
    us = suffixArrayRec(as);
    su.resize(n + 1);
    for (int i = 0; i < n; ++i) su[us[i]] = i;
    su[n] = -1;
    hs.assign(n, 0);
    for (int h = 0, u = 0; u < n; ++u) if (su[u]) {
      for (int v = us[su[u] - 1]; v + h < n && as[v + h] == as[u + h]; ++h) {}
      hs[su[u]] = h;
      if (h) --h;
    }
    if (rmq) {
      const int logN = n ? (31 - __builtin_clz(n)) : 0;
      hs.resize((logN + 1) * n - (1 << logN) + 1);
      for (int e = 0; e < logN; ++e) {
        int *hes = hs.data() + e * n;
        for (int i = 0; i <= n - (1 << (e + 1)); ++i) {
          hes[n + i] = min(hes[i], hes[i + (1 << e)]);
        }
      }
      bsr.resize(n + 1);
      bsr[0] = -1;
      for (int i = 1; i <= n; ++i) bsr[i] = bsr[i >> 1] + 1;
    }
  }
  // Returns longest common prefix of as[u, n) and as[v, n).
  //   0 <= u, v <= n
  //   Assumes rmq.
  inline int lcp(int u, int v) const {
    if (u == v) return n - u;
    int i = su[u], j = su[v];
    if (i > j) swap(i, j);
    const int e = bsr[j - i];
    return min(hs[e * n + i + 1], hs[e * n + j + 1 - (1 << e)]);
  }
};
////////////////////////////////////////////////////////////////////////////////


// |as| = n ==> |rs| = 2 n + 1
// [i - rs[i], i + rs[i]] is palindrome for $ as[0] $ as[1] $ ... $ as[n-1] $
// as[i, j): palindrome <=> j - i <= rs[i + j]
template <class String> vector<int> manacher(const String &as) {
  const int n = as.size();
  vector<int> rs(2 * n + 1);
  for (int i = 0, j = 0, k; i <= 2 * n; i += k, j -= k) {
    for (; 0 < i - j && i + j < 2 * n &&
           (!((i + j + 1) & 1) || as[(i - j - 1) >> 1] == as[(i + j + 1) >> 1]);
         ++j) {}
    rs[i] = j;
    for (k = 1; k < j && k + rs[i - k] < j; ++k) rs[i + k] = rs[i - k];
  }
  return rs;
}


template <class T> void bAdd(vector<T> &bit, int pos, const T &val) {
  const int bitN = bit.size();
  for (int x = pos; x < bitN; x |= x + 1) bit[x] += val;
}
template <class T> T bSum(const vector<T> &bit, int pos) {
  T ret = 0;
  for (int x = pos; x > 0; x &= x - 1) ret += bit[x - 1];
  return ret;
}
template <class T> T bSum(const vector<T> &bit, int pos0, int pos1) {
  return bSum(bit, pos1) - bSum(bit, pos0);
}


int N, Q;
char S[200'010];
vector<int> I, R;

inline int idRev(int i) {
  return 2 * N + 2 - i;
}

SuffixArray sa;
vector<int> rs;

namespace brute {
vector<int> run() {
  vector<int> ans(Q, 0);
  for (int q = 0; q < Q; ++q) {
    for (int l = 1; l <= R[q]; ++l) {
      const int u = I[q];
      const int v = idRev(I[q] + 2 * l);
      if (sa.su[u] < sa.su[v] && sa.lcp(u, v) < l) {
        ++ans[q];
      }
    }
  }
  return ans;
}
}  // brute


namespace fast {
vector<int> run() {
  vector<int> ans(Q, 0);
  
  // sa.su[u] < sa.su[v]
  {
    vector<pair<int, int>> eventss[2];
    for (int q = 0; q < Q; ++q) {
      const int s = idRev(I[q]) & 1;
      eventss[s].emplace_back(idRev(I[q] + 2 * R[q]), q << 1    );
      eventss[s].emplace_back(idRev(I[q]           ), q << 1 | 1);
    }
    for (int s = 0; s < 2; ++s) {
      sort(eventss[s].begin(), eventss[s].end());
      int tot = 0;
      vector<int> bit(sa.n, 0);
      int v = s;
      for (const auto &event : eventss[s]) {
        for (; v < event.first; v += 2) {
          ++tot;
          bAdd(bit, sa.su[v], +1);
        }
        const int q = event.second >> 1;
        const int sig = (event.second & 1) ? +1 : -1;
        ans[q] += sig * (tot - bSum(bit, sa.su[I[q]]));
      }
    }
  }
  
  // sa.su[u] < sa.su[v] && rs[I[q] + l] >= l
  {
    vector<pair<int, int>> events;
    for (int q = 0; q < Q; ++q) {
      events.emplace_back(I[q]        + 1, q << 1    );
      events.emplace_back(I[q] + R[q] + 1, q << 1 | 1);
    }
    sort(events.begin(), events.end());
    vector<int> bit(N, 0);
    int x = 0;
    for (const auto &event : events) {
      for (; x < event.first; ++x) {
        const int rad = rs[2 * x] / 2;
        const char cR = (x + rad < N) ? S[x + rad    ] : '0';
        const char cL = (x - rad > 0) ? S[x - rad - 1] : '1';
        assert(cR != cL);
        if (cR < cL) {
          bAdd(bit, x - rad, +1);
        }
      }
      const int q = event.second >> 1;
      const int sig = (event.second & 1) ? +1 : -1;
      ans[q] -= sig * bSum(bit, I[q] + 1);
    }
  }
  
  return ans;
}
}  // fast


int main() {
  int Subtask;
  for (int numCases; ~scanf("%d%d", &Subtask, &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    scanf("%d%d", &N, &Q);
    scanf("%s", S);
    I.resize(Q);
    R.resize(Q);
    for (int q = 0; q < Q; ++q) {
      scanf("%d%d", &I[q], &R[q]);
      --I[q];
    }
    
    string s = S;
    string t = s;
    reverse(t.begin(), t.end());
    sa = SuffixArray(s + "01" + t, true);
    rs = manacher(string(S));
    
    const int maxH = *max_element(sa.hs.begin(), sa.hs.begin() + sa.n);
    bool adjdiff = true;
    for (int i = 0; i < N - 1; ++i) adjdiff = adjdiff && (S[i] != S[i + 1]);
cerr<<"N = "<<N<<", Q = "<<Q<<", maxH = "<<maxH<<", adjdiff = "<<adjdiff<<endl;
    
    const auto ans = fast::run();
    for (int q = 0; q < Q; ++q) {
      printf("%d\n", ans[q]);
    }
#ifdef LOCAL
if(N<=4000&&Q<=4000){
 const auto brt=brute::run();
 for(int q=0;q<Q;++q)if(brt[q]!=ans[q]){
  cerr<<S<<endl;
  cerr<<"q = "<<q<<"; "<<I[q]<<" "<<R[q]<<": "<<brt[q]<<" "<<ans[q]<<endl;
  break;
 }
 assert(brt==ans);
}
#endif
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 4
Accepted
time: 0ms
memory: 3744kb

input:

1 5
5 5
aaaaa
4 1
2 1
2 2
2 2
4 1
5 5
abaab
1 1
1 2
2 1
3 1
1 1
5 5
baaaa
1 2
2 2
2 2
2 1
2 2
5 5
babab
1 2
2 2
4 1
2 2
2 2
5 5
baaab
2 2
2 1
1 1
1 2
2 2

output:

0
0
0
0
0
1
1
0
0
1
0
0
0
0
0
0
2
1
2
2
1
0
0
0
1

result:

ok 25 numbers

Test #2:

score: 4
Accepted
time: 1ms
memory: 3868kb

input:

2 5
10 10
babbbbbaaa
1 4
9 1
6 2
2 3
2 1
1 5
1 5
4 2
1 2
2 4
10 10
baabbaabab
1 5
2 4
2 4
2 3
3 4
5 3
2 3
1 5
3 1
4 1
10 10
aaaaabaabb
1 5
6 2
3 2
2 2
1 1
5 1
1 5
1 5
1 4
3 3
10 10
babbaababb
5 3
7 1
7 2
1 4
6 1
4 2
2 4
2 4
4 1
1 5
10 10
babbbaabba
2 3
1 5
6 2
2 4
1 5
4 1
2 3
5 2
2 3
1 5

output:

2
0
0
3
1
2
2
0
1
3
1
2
2
1
3
1
1
1
1
0
3
0
1
0
0
1
3
3
2
2
2
0
1
1
1
0
3
3
0
2
2
1
1
3
1
0
2
0
2
1

result:

ok 50 numbers

Test #3:

score: 4
Accepted
time: 2ms
memory: 3880kb

input:

3 5
19 19
baaababbbbbaaabbbaa
13 1
1 7
7 6
10 2
4 5
18 1
16 1
7 6
6 5
6 5
3 8
4 7
6 3
2 8
14 3
11 4
11 3
1 4
1 9
19 19
bbaababbaaaaababbba
9 1
10 4
16 1
1 9
4 6
5 4
15 1
1 7
2 6
5 7
14 3
1 9
11 3
1 7
8 6
2 8
10 1
2 8
8 2
20 19
baabaaabaabaaaababba
3 7
1 8
4 5
14 2
7 7
1 5
1 8
15 2
2 8
2 6
3 5
2 4
6 ...

output:

0
2
0
0
4
0
0
0
4
4
6
6
3
7
2
1
1
1
3
0
2
0
2
3
1
1
1
1
2
1
2
2
1
1
2
0
2
0
4
0
1
1
4
0
0
2
5
4
3
2
2
2
6
0
5
5
2
6
1
7
2
2
1
2
0
0
2
4
5
1
5
2
4
3
1
0
6
1
0
4
7
0
0
0
4
0
4
1
1
7
1
7
4
0
0
2

result:

ok 96 numbers

Test #4:

score: 4
Accepted
time: 2ms
memory: 3756kb

input:

4 5
46 50
abababbbbbbbbbabaabaaababbaaaaabababbababaaaaa
1 13
19 1
7 19
22 7
3 21
3 17
3 21
31 6
1 23
11 4
19 11
4 17
2 21
7 20
10 8
25 9
9 19
6 4
7 18
5 4
2 1
12 9
6 20
17 13
7 1
9 17
1 21
12 6
11 11
1 23
5 16
28 6
4 17
10 2
3 19
34 5
21 13
1 23
13 6
31 5
6 20
4 20
3 21
16 1
1 21
17 2
2 19
23 4
1 7...

output:

9
0
0
3
12
11
12
5
14
0
3
5
6
0
0
0
0
0
0
4
0
0
0
10
0
0
14
0
0
14
10
4
5
0
12
1
9
14
0
5
0
5
12
0
14
1
6
1
7
8
8
7
5
15
5
0
16
0
0
13
0
5
14
9
1
14
6
0
5
15
13
9
7
1
0
3
15
4
5
5
3
1
6
1
12
3
0
16
1
7
8
3
13
0
2
0
14
0
3
1
4
0
8
7
3
8
8
13
7
1
13
13
15
3
9
3
9
10
1
10
1
0
10
15
5
0
5
6
2
9
0
12
5
4...

result:

ok 246 numbers

Test #5:

score: 4
Accepted
time: 2ms
memory: 3844kb

input:

5 5
97 98
bbaabaabbababaaaababaabababaabaababbbbbbaababaabaaabbaabbabaabbaababbabbbbbaababbbbabbbbbbbaaaaab
71 13
77 7
13 3
14 40
18 21
13 38
20 18
2 48
30 27
84 4
3 34
4 15
18 36
55 20
3 44
9 36
10 24
15 8
55 15
48 6
38 18
30 16
19 7
4 47
76 4
36 28
3 38
7 43
13 3
7 22
55 12
9 22
26 17
30 23
24 31
...

output:

1
7
0
38
7
18
8
25
15
4
30
9
17
15
40
5
14
7
11
3
1
8
4
31
3
3
34
24
0
8
8
3
12
13
20
8
0
0
6
5
9
10
16
21
2
16
1
24
2
9
6
9
31
15
26
7
9
12
25
0
18
0
19
24
31
18
41
3
9
13
11
19
0
1
25
1
0
22
27
17
35
0
7
10
3
9
21
5
15
0
33
2
5
5
18
24
9
20
5
14
1
12
4
15
0
10
9
4
6
18
2
10
3
31
14
26
13
1
13
6
4
...

result:

ok 482 numbers

Test #6:

score: 4
Accepted
time: 4ms
memory: 3916kb

input:

6 5
998 992
bibbaabpaapbaabbibbbbabbaabbaafbaabbaabbaabbaabfaabbaabbabbbabbbaabpaapbaabbbabobabbaabbaafbaabbaabbaabbaabfaabbaabbabbbbbbbaabpaapbaabbbbbbbabbaabbaafbaabbaabbaabbaabfaabbaabbabobabbbaabpaapbaabbbabbbabbaabbaafbaabbaabbaabbaabfaabbaabbabbbbibbaabpaapbaabbibbbbabbaabbaafbaabbaabbaabbaabf...

output:

32
48
41
62
267
134
145
111
337
312
88
335
139
173
174
97
77
19
155
291
125
23
110
17
13
167
61
126
6
74
214
59
0
200
42
47
125
9
3
119
132
216
0
31
105
61
4
355
141
39
53
60
89
39
52
208
259
17
12
194
355
77
65
53
298
60
138
308
198
120
118
114
207
8
9
68
264
307
366
2
356
6
15
32
120
56
6
93
67
36...

result:

ok 4970 numbers

Test #7:

score: 4
Accepted
time: 7ms
memory: 4176kb

input:

7 5
1988 1997
bvappavbbaabavavpavbbaabbvappavbbaabbvappavbbaabbvappavbbaaqbvappavbbaabbvappavbqaabbvappavbbaabbvappavbbaabbvappavbbaabbvappavabaabbvappavbbaaqbvappavbbaabbpappavbqaabbvappavbbaabavappavbbaabbvappavbbapbbvappavbbaabbvappavbbaaqbvappavbbaabbvappavbqaabbvappavbbaabbvappavbbpabbvappavbba...

output:

20
302
418
404
6
12
674
787
520
153
145
127
633
4
157
792
146
103
326
395
41
38
130
151
222
2
8
301
48
314
90
194
37
53
6
124
558
828
512
89
13
140
221
47
492
94
123
111
63
12
436
34
1
169
681
135
205
303
114
453
96
227
420
112
663
85
43
104
713
28
42
8
17
180
613
81
19
713
89
446
260
171
14
501
58
...

result:

ok 9977 numbers

Test #8:

score: 4
Accepted
time: 11ms
memory: 4680kb

input:

8 5
2977 2978
aabaaaabaabaaaabaabaaaabaabnaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaanbaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaaabaabaaa...

output:

465
335
734
0
461
942
8
412
161
207
532
754
409
375
164
1044
567
1109
1112
1041
96
400
888
1263
30
1029
887
56
614
1099
98
765
128
841
54
372
16
9
935
124
1072
706
232
790
558
16
371
264
52
1255
661
391
0
358
492
696
1197
441
183
129
493
740
256
128
584
53
1185
239
1214
891
844
151
401
243
395
1134
...

result:

ok 14916 numbers

Test #9:

score: 4
Accepted
time: 11ms
memory: 4564kb

input:

9 5
3992 3963
baaamarbaaaabramaaabbaabmarbaaaabrambaabbaabmarbaaaabrambaabbaabmarbaaaabrambbabbaabmarbaaaabrambaabbaabmarbaaaabrambaabbaaamarbaaaabramaaabbaabmarbaaaabrambaabbaabmarbaaaabrambaabbabbmarbaaaabrambaabbaabmarbaaaabrambaabbaabmarbaaaabrambaabbaaamarbaaaabramaaabbaabmarbaaaabrambaabbaabma...

output:

336
177
335
701
21
1027
512
925
1488
1776
101
446
1240
1607
541
20
314
269
324
474
86
1128
18
372
311
1047
462
80
1177
972
201
5
626
845
652
719
476
86
361
699
827
499
1740
121
325
435
40
891
201
204
62
132
1255
52
813
492
867
845
793
61
1432
1544
5
27
51
19
428
842
1242
752
1302
225
176
108
1154
31...

result:

ok 19867 numbers

Test #10:

score: 4
Accepted
time: 81ms
memory: 11512kb

input:

10 5
21773 22026
babaaababbabbbbbaababbababaabbababbabbbbabaaaaabaabbabbbbbbaaaabaababbbabbbbbabaababbaababbbbaaabaabbaaaabbbbaaaabbbbbabaaaaaaaabbabbabbaabbbbabbbaaabbabbabababbaabaabbaabbaaaaabaabaababaabbaaaaaabababbaabbababaabbabbaaaaaaaaabbaabbbaabbababbbbbaaaaaabaaabbbaabaaabbabbabaabbbbabbabb...

output:

605
606
870
1877
2959
7799
3875
2952
39
6360
3249
67
2082
6986
393
190
1152
892
2734
2346
25
1130
3784
44
2336
1221
547
1244
169
4481
7213
4746
125
1065
6830
1658
2057
3586
1335
1398
1853
1172
6995
37
5093
2550
3490
3992
161
604
45
935
1271
334
250
1312
7945
6837
0
3573
332
1692
6539
5739
1893
8430
...

result:

ok 109320 numbers

Test #11:

score: 4
Accepted
time: 168ms
memory: 21268kb

input:

11 5
47391 45803
aababababbbbbbbbbbbaabaabababaababbbbabbabbabaabbbbabbbbaaaababbabababbabbabbaabbbaaabaaaabbbabababbaababbbabbabbabbbabaabbabaabaaaabbbbbbbbabbbbbababbaaababbbababbabaababbaaaaaaaaabaababaababbaaaabbbbaaababbaabaaabbabaaaaaabbababababbababaaaabbbaaaaaaaaaaabbbbbbbabbbaaabababaaababa...

output:

4187
3688
103
3723
2769
13827
13979
644
1458
10311
4489
1555
3876
1467
347
635
253
31
13744
3311
4567
8057
8348
1535
688
13113
483
8597
8775
4255
9923
3224
1010
79
8866
3892
7422
1582
7336
3033
10456
7159
3243
16103
3328
8457
364
1124
2163
1566
7741
4080
2561
18853
1239
10074
1016
2604
1382
4437
663...

result:

ok 236962 numbers

Test #12:

score: 4
Accepted
time: 252ms
memory: 38940kb

input:

12 5
69918 68763
bbabbbabbbaaabaabbbbbbbbbabbbbbbbbaaabaabaabbbaabaaaabbabbabbbbbabbbbaabaaaabaabbababbbbbaaababbabbbbaabbbaaabaaaaababaaabbbbababababbababbbaaabbbbaabbabbbbbbbbabbabbbbaaabbbbaaababbbaaabaaabbabaaabbaabbbaaabbbbbbaabaabbbaabaaabbbabbbbbaabaabbabbbabbababbaaaabbbbbbbaabaaaabbabaaaaab...

output:

614
22361
3580
4071
4361
2218
17420
6215
4184
8818
7589
19792
8267
1773
46
1287
3821
25097
6602
10307
7693
26755
13718
3090
148
340
23284
4036
10048
3451
2001
686
4222
3169
17770
13155
8108
10690
4354
3601
9031
8539
3752
15172
18009
2243
1614
220
6139
22729
21068
1844
14873
1759
5013
2672
4514
13169...

result:

ok 346956 numbers

Test #13:

score: 4
Accepted
time: 334ms
memory: 45484kb

input:

13 5
86855 85346
bbaaababbbbbaaaabbbaabbabbabbaabbbabbbbbbbaabaabbaabbaaaabbabaaaabbaaaabababbbbbbbbaabbabababaabaababaaabbabaabaaabaabaabbbabbabbbaabaaaabbbbabbbaabbabaabbabbbaabbbbbaaaaabbbbaaaababbbabaaabaababaababaaabbbaaaababbabaababaabababaababbabbbabaabaaabbabababbbbbbaaaababababbabaabaaaaabb...

output:

23397
497
37521
13084
16190
5014
39195
2818
7676
13149
29388
16788
9755
6909
28102
17426
12692
25728
7138
20352
5086
8739
2657
496
34203
3815
7691
18151
208
26131
7994
21088
3198
3550
17965
13389
12003
1097
4936
2049
9546
460
8972
3717
24214
1105
1632
6965
22150
16897
21692
16810
2538
32408
12849
11...

result:

ok 425266 numbers

Test #14:

score: 4
Accepted
time: 380ms
memory: 38964kb

input:

14 5
95651 97657
babbbababaaaabaaaaaaaabbaaabaababbaabbaabbabbaaabababaabaaaabaaaabbbabbabbaaaabbaabbbbbbbbaabbaabbbaaabaaabababbabababbabbbababaabaababbaabbababbbabbbbabbbbabbaaaaabaaaabbabbbabbbabbaababbbabbaaabaabbbbbbbabbabbbabaaaabaaaaaaaababaababbabaaaabaababaababbaaaaababaaabbbbabababbbabaabb...

output:

16666
26260
7301
9219
17848
7041
13588
538
10511
5249
30399
9348
28342
3847
7512
9692
41788
18592
13194
7424
18368
4493
734
9223
199
13124
6893
31855
5054
34072
32148
1798
17634
3488
15848
8095
2947
5926
5118
5355
12682
10064
37287
36556
10884
2817
19446
15735
2004
27803
26891
12199
14432
36470
9212...

result:

ok 479775 numbers

Test #15:

score: 4
Accepted
time: 66ms
memory: 13868kb

input:

15 5
21209 21227
babababakakababfbrbabababauabababfbababababakabababfbababababakabababfbababababakabababfbababababakabababfbababababakabababfbalabababakabababfbababababakabababfbrbabababauabababfbababababakabababfbababababakabababfbababababakabababfbababababakabababfbababababakabababfbababababakabab...

output:

875
8428
694
990
9417
1004
607
3128
8428
528
10263
451
1510
1
8757
272
5285
2056
178
54
3412
1160
491
6677
6540
7486
707
409
919
377
3178
339
3932
28
338
421
39
992
1206
2689
712
20
437
5233
6165
9492
49
1315
3028
778
981
4525
4967
10255
1306
1015
1095
2415
1040
933
3517
1255
1117
8592
55
124
780
52...

result:

ok 110426 numbers

Test #16:

score: 4
Accepted
time: 233ms
memory: 31064kb

input:

16 5
73529 68306
babsbababwbsbabsbabababsbabsbababwbsbadsbababwbsbabsbabsbwbsbabsbababwbsbabsbababwbsbabsbababwbsbabsbabababsbabsbababwbsbadsbababwbsbabsbabsbwbsbabsbababwbsbabsbababwbsbrbsbababwbsbabsbabababsbabsbababwbsbadsbababwbsbabsbabsbwbsbabsbababwbsbabsbababwbsbabsbababwbsbabsbabababsbabsbab...

output:

1
1262
2399
1
0
1
10730
1
0
1
30489
3312
4248
935
8645
14603
10699
0
0
13368
35363
10221
11654
32384
7456
15281
0
1
3274
17255
1
14138
8866
11994
34570
1
15001
8483
10232
4695
1473
635
16542
0
16332
4700
9621
2069
11473
0
3673
23257
4871
1
5197
13419
7209
1
14946
1
1
2201
9540
455
12114
15470
1
0
17...

result:

ok 359447 numbers

Test #17:

score: 4
Accepted
time: 304ms
memory: 35108kb

input:

17 5
87085 88577
ababababababvbabababababababababababvbabababababababababababvbabababatabababababababvbabababababababababababvbabababababababababababvbabababababababababababvbabababababababababababvbabababababababababababvbabababababababababababvbabababababababababababvbabababatabababababababvbababa...

output:

11254
27
760
3385
0
4335
871
683
513
2774
1250
1998
2174
2463
974
6871
13078
2817
2453
1096
3372
2677
0
513
36833
1611
6019
1681
43004
1353
12306
18622
39485
2589
37733
9624
589
1277
3638
526
1272
0
14907
40922
484
536
1394
35414
3661
37
0
1118
40967
2330
2103
1458
2071
3250
982
1862
190
1907
0
0
0
...

result:

ok 436679 numbers

Test #18:

score: 4
Accepted
time: 332ms
memory: 38720kb

input:

18 5
99489 97628
abababacababababacababababacababababacababababacababababacababababacababababacababababacababayabacababababacabababxbacababababacababababacababababacababababacababababacababababacababababacababababacababababacababayabacababababacababababacababababacababababacababababacababababacababa...

output:

445
707
42132
580
26762
655
39
497
44253
161
2051
39980
19110
40444
266
38617
11389
4931
29403
376
333
626
12248
653
10
648
686
24032
665
291
252
24777
44211
463
313
566
178
49505
25926
647
36
407
625
44175
46231
461
30278
25555
46539
15563
365
46328
554
624
524
27228
32996
577
33323
11690
459
36932...

result:

ok 475535 numbers

Test #19:

score: 4
Accepted
time: 73ms
memory: 11184kb

input:

19 5
23197 23286
abbbaabaaabbaabbaaabaabbbabbabbhaabaaabbaabbaaabaabbbabbambbaabaaabbaabbaaabaabbmabbabbbaabaaabbaabbaaabaabbbabbabbbaabaaabbaabbaaabaabbbabbabbbaabaaabbaabbaaabaabbaabbambbaabaaabbaabbaaabaabbmabbabbbaabaaabbaabbaaabaahbbabbabbbaabaaabbaabbbaabaabbbabbabbbaabaaabbaabbaaabaabbbabbamb...

output:

1910
2681
1805
4506
5628
1091
2562
4919
2073
2056
437
2767
11
3749
3029
1096
8737
7615
2668
792
3839
5638
3108
4975
97
879
4765
40
669
7198
4567
2552
132
823
163
429
5242
6149
3667
4357
276
5174
2958
5078
5646
372
3909
6780
1673
2847
2623
1161
269
4198
2351
2212
1827
482
8844
2890
4024
10343
6667
18...

result:

ok 116305 numbers

Test #20:

score: 4
Accepted
time: 166ms
memory: 21444kb

input:

20 5
49694 49669
bbabbbbbbabbbbbbbaabbbbbbaabbbbbbasbbbbbbaabbbbbbaabbbbbbaabbbbbbaabbbbbbaabbbbbbaabbbbbbsabbbbbbaabbbbbbaabbbbbbbabbbbbbabbbbbbbaabbbbbbaabbbbbbasbbbbbbaabbbbbbaabbbbbbaabbbbbbaabbbbbbaabbbbbbaabbbbbbsabbbbbbaabbbbbbaabbbbbbbabbbbbbabbbbbbbaabbbbbbaabbbbbbasbbbbbbaabbbbbbaabbbbbbaa...

output:

13569
1017
5376
9522
1695
1795
30
1977
2437
2430
20681
5755
7264
1043
11900
19152
10706
2558
2222
1560
634
5203
22613
9927
5386
16731
4148
14322
439
1311
20353
5062
6228
213
8656
3351
4789
4209
2195
4609
15384
5467
4252
21900
10075
6406
297
2151
2470
34
763
6939
3391
374
11741
6217
141
1034
2377
26
...

result:

ok 248726 numbers

Test #21:

score: 4
Accepted
time: 243ms
memory: 32880kb

input:

21 5
74422 74421
baabbllbbaabblabbaabbllbbaabbllbblabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbblbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllbbaabbllpbaabbllbbaabbllbbaabpllbbaa...

output:

8464
12807
14893
15417
2087
15616
4844
25180
3562
9948
10860
455
8990
3493
13291
6657
9948
1378
10607
9951
20948
10719
284
345
678
19341
23364
3209
6241
30
23127
3419
17635
4451
5776
15877
25368
32446
23797
8009
17233
10470
24072
1541
17220
20146
954
15956
325
4402
7356
29544
13598
3148
19995
4767
9...

result:

ok 372342 numbers

Test #22:

score: 4
Accepted
time: 298ms
memory: 36852kb

input:

22 5
89983 89431
bajbbbzbbaabbzabbjabbaabbajbbazbbaabbzbbbjabbaabbajbbazbbaabbzabbjabbaabbajbbazbbaabbzabbjabbaabbajbbazbbaabbzabbjabbaabbajbbazbbaabbzabbjabbaabbajbbazbbaabbzabbjabbaabbajbbbzbbaabbzabbjabbaabbajbbazbbaabbzbbbjabbaabbajbbazbbaabbzabbjabbaabbajbbazbbaabbzabbjabbaabbajbbazbbaabbzabbja...

output:

11849
4468
9663
10840
25353
5872
5144
8020
8833
7876
14465
396
6544
4573
23184
18637
2242
20334
733
1462
517
21135
32899
12086
10322
27907
26177
7145
2502
34753
4768
9997
1162
17014
5015
247
570
19530
26198
15076
445
105
12044
15357
8025
284
19813
25567
31741
7990
11746
12187
178
9919
19047
14274
30...

result:

ok 447256 numbers

Test #23:

score: 4
Accepted
time: 319ms
memory: 50552kb

input:

23 5
94783 94700
baabaaaabaabbaabbaabaaaabaabbaabbaabaaaabaabbaaabaabavaabaabaaabbaabaaaabaabbaabbaapaaaabaabbaabbaabaaaabaabbaaabaabaaaabaabaaabbaabaaaabaabbaaabaabaaaabaabbaabbaabaaaabaabbaaabaabaaaabaabaaabbaabaaaabaabbaabbaabaaaabaabbaabbaabaaaabaabbaaabaabaaaabaabaaabbaabaaaabaabbaabbaabaaaabaa...

output:

8010
2025
11768
26959
10324
14089
36040
9210
29681
9970
26256
3343
1328
4309
3852
938
29531
20419
13041
2472
14574
8863
15587
216
761
8774
3063
10327
15475
1153
7846
9743
11343
13
1843
8620
272
3102
14916
2032
8297
1531
6956
536
2855
16589
2050
26687
37559
3143
31236
5568
3355
2908
1758
7870
5869
27...

result:

ok 472841 numbers

Test #24:

score: 4
Accepted
time: 329ms
memory: 40896kb

input:

24 5
99576 99977
babbbaabbaabbbabbaabbadbbaabbaabbdabbaabbadbbaabbaabbdabbaabbabbbaabbaabbbabbaabbadbbaabbaabbdabbaabbadbbaabbaabbdabbaabbabbbaabbaabbbabbaabbadbbaabbaabbdabbaabbadbbaabbaabbdabbaabbabbbbabbaabbbabbaabbadbbaabbaabbdabbaabbadbbaabbaabbdabbaabbabbbaabbaabbbabbaabbadbbaabbaabbdabbaabbad...

output:

25224
22877
4205
25967
30495
8683
15578
4263
23
3
680
3531
18268
724
29245
5669
2295
34056
1095
2139
1224
17795
26151
308
13479
7892
946
13545
7388
17394
16706
13910
4718
35014
5761
16848
1660
3842
5424
13559
7243
925
39106
2824
1098
13489
20008
19224
16472
4053
1262
16435
543
13223
11878
15992
1914...

result:

ok 498719 numbers

Test #25:

score: 4
Accepted
time: 344ms
memory: 40908kb

input:

25 5
99821 99309
aabbaabbaxbbaabbaabbaabbxabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbabbbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbbabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaxbbaabbaabbaabbxabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbabbbaabeaabbaab...

output:

9293
10232
15501
4884
29723
9466
6164
10263
13792
30288
4377
12591
23521
10296
1233
14620
18817
3745
8903
7653
2500
27158
3512
12919
14126
24131
20
967
4125
15323
1064
3211
5257
320
32583
183
5353
15125
7337
5497
20821
1606
7635
17280
9821
9697
265
2996
503
33947
5003
2390
13469
1894
815
7458
27100
...

result:

ok 497857 numbers

Extra Test:

score: 0
Extra Test Passed