Problem p02632 (C++) — buggy submission, verdict: Time Limit Exceeded

#define DEBUGGING // Enables DEBUG macro.
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
#define REP(i, n) for (int i = 0; (i64)(i) < (i64)(n); ++i)

#ifndef DEBUGGING
#define debug(...)
#define DEBUG(...)
#else
template <typename T> void debug(T value) { std::cerr << value; }
template <typename T, typename... Ts> void debug(T value, Ts... args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define DEBUG(...)                                                             \\
  do {                                                                         \\
    cerr << " (L" << __LINE__ << ") ";                                         \\
    cerr << #__VA_ARGS__ << ": ";                                              \\
    debug(__VA_ARGS__);                                                        \\
    cerr << endl;                                                              \\
  } while (0)
#endif

// auto mod int

const i64 MOD = 1'000'000'007;

struct mint {
  long long x;
  mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime MOD
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

vector<mint> cache;
vector<bool> cached;

mint comb(i64 n, i64 k) {
  if (k > n)
    return 0;
  if (cached[n])
    return cache[n];
  if (k > n - k) {
    k = n - k;
  }
  mint num = 1;
  mint den = 1;
  REP(i, k) {
    num *= (n - i);
    den *= (1 + i);
  }
  mint res = num * den.pow(MOD - 2);
  cache[n] = res;
  cached[n] = true;
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  i64 K;
  cin >> K;
  string S;
  cin >> S;
  i64 N = S.size();
  vector<mint> pow25(N + K + 1);
  vector<mint> pow26(N + K + 1);
  pow25[0] = pow26[0] = 1;
  cache.resize(N + K + 1);
  cached.resize(N + K + 1);
  REP(i, N + K) {
    pow25[i + 1] = pow25[i] * 25;
    pow26[i + 1] = pow26[i] * 26;
  }

  mint ans = 0;
  mint comb = 1;
  for (int r = N - 1; r <= N + K - 1; ++r) {
    if (N + K - 1 - r < 0)
      break;
    ans += comb * pow25[r + 1 - N] * pow26[N + K - 1 - r];
    comb *= (r + 1);
    comb *= mint(r - N + 2).inv();
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#define DEBUGGING // Enables DEBUG macro.
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
#define REP(i, n) for (int i = 0; (i64)(i) < (i64)(n); ++i)

#ifndef DEBUGGING
#define debug(...)
#define DEBUG(...)
#else
template <typename T> void debug(T value) { std::cerr << value; }
template <typename T, typename... Ts> void debug(T value, Ts... args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define DEBUG(...)                                                             \\
  do {                                                                         \\
    cerr << " (L" << __LINE__ << ") ";                                         \\
    cerr << #__VA_ARGS__ << ": ";                                              \\
    debug(__VA_ARGS__);                                                        \\
    cerr << endl;                                                              \\
  } while (0)
#endif

// auto mod int

const i64 MOD = 1'000'000'007;

struct mint {
  long long x;
  mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime MOD
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

vector<mint> cache;
vector<bool> cached;

mint comb(i64 n, i64 k) {
  if (k > n)
    return 0;
  if (cached[n])
    return cache[n];
  if (k > n - k) {
    k = n - k;
  }
  mint num = 1;
  mint den = 1;
  REP(i, k) {
    num *= (n - i);
    den *= (1 + i);
  }
  mint res = num * den.pow(MOD - 2);
  cache[n] = res;
  cached[n] = true;
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  i64 K;
  cin >> K;
  string S;
  cin >> S;
  i64 N = S.size();
  vector<mint> pow25(N + K + 1);
  vector<mint> pow26(N + K + 1);
  pow25[0] = pow26[0] = 1;
  cache.resize(N + K + 1);
  cached.resize(N + K + 1);
  REP(i, N + K) {
    pow25[i + 1] = pow25[i] * 25;
    pow26[i + 1] = pow26[i] * 26;
  }

  mint ans = 0;
  for (int r = N + K - 1; r >= N - 1; --r) {
    if (r + 1 - N < 0)
      break;
    ans += comb(r, N - 1) * pow25[r + 1 - N] * pow26[N + K - 1 - r];
  }
  cout << ans << endl;
}