Problem p02891 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef __int128 ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

// const ll mod = 3006703054056749LL;
// const ll mod = 1000000009;
const ll mod = 31443539979727LL;

ll modpow(ll b, ll e) {
  ll ans = 1;
  for (; e; b = b * b % mod, e /= 2)
    if (e & 1)
      ans = ans * b % mod;
  return ans;
}

vector<ll> BerlekampMassey(vector<ll> s) {
  int n = sz(s), L = 0, m = 0;
  vector<ll> C(n), B(n), T;
  C[0] = B[0] = 1;

  ll b = 1;
  rep(i, 0, n) {
    ++m;
    ll d = s[i] % mod;
    rep(j, 1, L + 1) d = (d + C[j] * s[i - j]) % mod;
    if (!d)
      continue;
    T = C;
    ll coef = d * modpow(b, mod - 2) % mod;
    rep(j, m, n) C[j] = (C[j] - coef * B[j - m]) % mod;
    if (2 * L > i)
      continue;
    L = i + 1 - L;
    B = T;
    b = d;
    m = 0;
  }

  C.resize(L + 1);
  C.erase(C.begin());
  trav(x, C) x = (mod - x) % mod;
  return C;
}

typedef vector<ll> Poly;
ll linearRec(Poly S, Poly tr, ll k) {
  int n = sz(tr);

  auto combine = [&](Poly a, Poly b) {
    Poly res(n * 2 + 1);
    rep(i, 0, n + 1) rep(j, 0, n + 1) res[i + j] =
        (res[i + j] + a[i] * b[j]) % mod;
    for (int i = 2 * n; i > n; --i)
      rep(j, 0, n) res[i - 1 - j] = (res[i - 1 - j] + res[i] * tr[j]) % mod;
    res.resize(n + 1);
    return res;
  };

  Poly pol(n + 1), e(pol);
  pol[0] = e[1] = 1;

  for (++k; k; k /= 2) {
    if (k % 2)
      pol = combine(pol, e);
    e = combine(e, e);
  }

  ll res = 0;
  rep(i, 0, n) res = (res + pol[i + 1] * S[i]) % mod;
  return res;
}

int slv(string s) {
  char lst = '#';
  int cnt = 0;
  trav(c, s) {
    if (c == lst) {
      ++cnt;
      lst = '#';
    } else
      lst = c;
  }
  return cnt;
}

#ifdef LOCAL
#include "pretty_debug.h"
#else
#define debug(...) // ignore
#endif

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  string s;
  int k;
  cin >> s >> k;
  vector<ll> S(50);
  string t = s;
  rep(i, 0, 50) {
    S[i] = slv(t);
    t += s;
  }
  vector<ll> tr = BerlekampMassey(S);
  debug(vector<long long>(all(tr)));
  ll ans = linearRec(S, tr, k - 1);
  cout << (long long)(ans) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef __int128 ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

// const ll mod = 3006703054056749LL;
// const ll mod = 1000000009;
const ll mod = 31443539979727LL;

ll modpow(ll b, ll e) {
  ll ans = 1;
  for (; e; b = b * b % mod, e /= 2)
    if (e & 1)
      ans = ans * b % mod;
  return ans;
}

vector<ll> BerlekampMassey(vector<ll> s) {
  int n = sz(s), L = 0, m = 0;
  vector<ll> C(n), B(n), T;
  C[0] = B[0] = 1;

  ll b = 1;
  rep(i, 0, n) {
    ++m;
    ll d = s[i] % mod;
    rep(j, 1, L + 1) d = (d + C[j] * s[i - j]) % mod;
    if (!d)
      continue;
    T = C;
    ll coef = d * modpow(b, mod - 2) % mod;
    rep(j, m, n) C[j] = (C[j] - coef * B[j - m]) % mod;
    if (2 * L > i)
      continue;
    L = i + 1 - L;
    B = T;
    b = d;
    m = 0;
  }

  C.resize(L + 1);
  C.erase(C.begin());
  trav(x, C) x = (mod - x) % mod;
  return C;
}

typedef vector<ll> Poly;
ll linearRec(Poly S, Poly tr, ll k) {
  int n = sz(tr);

  auto combine = [&](Poly a, Poly b) {
    Poly res(n * 2 + 1);
    rep(i, 0, n + 1) rep(j, 0, n + 1) res[i + j] =
        (res[i + j] + a[i] * b[j]) % mod;
    for (int i = 2 * n; i > n; --i)
      rep(j, 0, n) res[i - 1 - j] = (res[i - 1 - j] + res[i] * tr[j]) % mod;
    res.resize(n + 1);
    return res;
  };

  Poly pol(n + 1), e(pol);
  pol[0] = e[1] = 1;

  for (++k; k; k /= 2) {
    if (k % 2)
      pol = combine(pol, e);
    e = combine(e, e);
  }

  ll res = 0;
  rep(i, 0, n) res = (res + pol[i + 1] * S[i]) % mod;
  return res;
}

int slv(string s) {
  char lst = '#';
  int cnt = 0;
  trav(c, s) {
    if (c == lst) {
      ++cnt;
      lst = '#';
    } else
      lst = c;
  }
  return cnt;
}

#ifdef LOCAL
#include "pretty_debug.h"
#else
#define debug(...) // ignore
#endif

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  string s;
  int k;
  cin >> s >> k;
  vector<ll> S(50);
  string t = s;
  rep(i, 0, 50) {
    S[i] = slv(t);
    t += s;
  }
  vector<ll> tr = BerlekampMassey(S);
  debug(vector<long long>(all(tr)));
  ll ans = tr.empty() ? 0 : linearRec(S, tr, k - 1);
  cout << (long long)(ans) << endl;
}