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

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

vector<int64_t> fact;

int64_t factorial(int64_t n, int64_t MOD) { return fact[n]; }

//-RECIPE-START-//
// Use 64 bits integers to avoid overflow errors during multiplication.
int64_t modPow(int64_t a, int64_t x, int64_t p) {
  // calculates a^x mod p in logarithmic time.
  int64_t res = 1;
  while (x > 0) {
    if (x % 2)
      res = (res * a) % p;
    a = (a * a) % p;
    x /= 2;
  }
  return res;
}

int64_t modInverse(int64_t a, int64_t p) { // (assuming p is prime).
  return modPow(a, p - 2,
                p); // calculates the modular multiplicative of a mod m.
}
int64_t
modBinomial(int64_t n, int64_t k,
            int64_t p) { // calculates C(n,k) mod p (assuming p is prime).
  int64_t numerator = factorial(n, p); // n!
  int64_t denominator =
      (factorial(k, p) * (factorial(n - k, p))) % p; // k!*(n-k)!
  return (numerator * modInverse(denominator, p)) %
         p; // numerator / denominator mod p.
}

const int64_t mod = 1e9 + 7;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int K, S;
  string s;
  cin >> K >> s;
  S = s.size();
  vector<int64_t> pow26(K + S + 5, 1), pow25(K + S + 5, 1);
  fact.assign(K + S + 5, 1);
  for (int i = 1; i < K + S + 5; ++i) {
    pow26[i] = (pow26[i - 1] * 26) % mod;
    pow25[i] = (pow25[i - 1] * 25) % mod;
    fact[i] = (fact[i - 1] * int64_t(i)) % mod;
  }

  int64_t ans = 0;
  for (int i = S + K - 1; i >= S - 1; --i) {
    int64_t right = (S + K - 1) - i;
    int64_t rem = K - right;
    int64_t others = (pow26[right] * pow25[rem]) % mod;
    ans += (others * modBinomial(i, S - 1, mod)) % mod;
    ans %= mod;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

map<int64_t, int64_t> dpt;

int64_t factorial(int64_t n, int64_t MOD) {
  if (n == 0)
    return 1;
  if (dpt.count(n))
    return dpt[n];
  return dpt[n] = (n * factorial(n - 1, MOD)) % MOD;
}

//-RECIPE-START-//
// Use 64 bits integers to avoid overflow errors during multiplication.
int64_t modPow(int64_t a, int64_t x, int64_t p) {
  // calculates a^x mod p in logarithmic time.
  int64_t res = 1;
  while (x > 0) {
    if (x % 2)
      res = (res * a) % p;
    a = (a * a) % p;
    x /= 2;
  }
  return res;
}

int64_t modInverse(int64_t a, int64_t p) { // (assuming p is prime).
  return modPow(a, p - 2,
                p); // calculates the modular multiplicative of a mod m.
}
int64_t
modBinomial(int64_t n, int64_t k,
            int64_t p) { // calculates C(n,k) mod p (assuming p is prime).
  int64_t numerator = factorial(n, p); // n!
  int64_t denominator =
      (factorial(k, p) * (factorial(n - k, p))) % p; // k!*(n-k)!
  return (numerator * modInverse(denominator, p)) %
         p; // numerator / denominator mod p.
}

const int64_t mod = 1e9 + 7;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int K, S;
  string s;
  cin >> K >> s;
  S = s.size();
  vector<int64_t> pow26(K + S + 5, 1), pow25(K + S + 5, 1);
  for (int i = 1; i < K + S + 5; ++i) {
    pow26[i] = (pow26[i - 1] * 26) % mod;
    pow25[i] = (pow25[i - 1] * 25) % mod;
  }

  int64_t ans = 0;
  for (int i = S + K - 1; i >= S - 1; --i) {
    int64_t right = (S + K - 1) - i;
    int64_t rem = K - right;
    int64_t others = (pow26[right] * pow25[rem]) % mod;
    ans += (others * modBinomial(i, S - 1, mod)) % mod;
    ans %= mod;
  }

  cout << ans << endl;
}