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

#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

template <long long mod> class modint {
private:
  long long num;
  long long extgcd(long long a, long long b, long long &x, long long &y) const {
    long long d = a;
    if (b != 0) {
      d = extgcd(b, a % b, y, x);
      y -= (a / b) * x;
    } else {
      x = 1;
      y = 0;
    }
    return d;
  }
  long long modinverse() const {
    long long x, y;
    extgcd(num, mod, x, y);
    return (mod + x % mod) % mod;
  }

public:
  modint() : num(0) {}
  modint(const long long x) : num((x < 0 ? (x + mod) : x) % mod) {}
  friend modint operator+(const modint &a, const modint &b) {
    return modint(a.num + b.num);
  }
  friend modint operator-(const modint &a, const modint &b) {
    return modint(a.num - b.num);
  }
  friend modint operator*(const modint &a, const modint &b) {
    return modint(a.num * b.num);
  }
  friend modint operator/(const modint &a, const modint &b) {
    return modint(a * b.modinverse());
  }
  modint &operator+=(const modint &b) { return (*this) = (*this) + b; }
  modint &operator-=(const modint &b) { return (*this) = (*this) - b; }
  modint &operator*=(const modint &b) { return (*this) = (*this) * b; }
  modint &operator/=(const modint &b) { return (*this) = (*this) / b; }
  modint &operator++() {
    (*this) += 1;
    return (*this);
  }
  modint &operator--() {
    (*this) -= 1;
    return (*this);
  }
  modint operator++(int) {
    modint tmp(*this);
    ++(*this);
    return tmp;
  }
  modint operator--(int) {
    modint tmp(*this);
    --(*this);
    return tmp;
  }
  modint pow(unsigned long long i) {
    if (i == 0)
      return 1;
    if (i & 1) {
      return (*this) * (this->pow(i - 1));
    }
    modint<mod> hpow = (this->pow(i >> 1));
    return hpow * hpow;
  }
  friend ostream &operator<<(ostream &os, const modint &a) {
    os << a.num;
    return os;
  }
  friend istream &operator>>(istream &is, modint &a) {
    long long x;
    is >> x;
    a = x;
    return is;
  }
};

template <ll mod> struct Factorial {
  const int size;
  vector<modint<mod>> v;
  Factorial(int n) : size(n + 1), v(vector<modint<mod>>(n + 1, 1)) {
    for (int i = 1; i < size; ++i) {
      v[i] = v[i - 1] * i;
    }
  }
  modint<mod> operator[](const int idx) const {
    if (idx >= size) {
      cerr << "Factorial operator[] idx >= size:" << idx << endl;
      return 1;
    }
    if (idx < 0) {
      cerr << "Factorial operator[] idx < 0:" << idx << endl;
      return 1;
    }
    return v[idx];
  }
};

using mint = modint<1000000007>;

int k;
int n;
string s;

mint Rec(int i, int j) {
  static map<P<int, int>, mint> dp;
  if (n + k - i + 1 < n - j)
    return 0;
  if (i > n + k) {
    if (j == n)
      return 1;
    else
      return 0;
  }
  if (j == n) {
    return mint(26).pow(n + k - i + 1);
  }
  if (dp.find({i, j}) != dp.end())
    return dp[{i, j}];
  if (j == n) {
    return dp[{i, j}] = Rec(i + 1, j) * 26;
  } else {
    return dp[{i, j}] = Rec(i + 1, j) * 25 + Rec(i + 1, j + 1);
  }
  return 0;
}

int main() {
  cin >> k >> s;
  n = (int)s.size();
  // cout << Rec(1,0) << endl;
  mint ans(0);
  Factorial<1000000007> fact(2000001);
  for (int i = 0; i <= k; ++i) {
    ans += mint(25).pow(i) * (fact[n + i - 1] / (fact[n - 1] * fact[i])) *
           mint(26).pow(k - i);
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

template <long long mod> class modint {
private:
  long long num;
  long long extgcd(long long a, long long b, long long &x, long long &y) const {
    long long d = a;
    if (b != 0) {
      d = extgcd(b, a % b, y, x);
      y -= (a / b) * x;
    } else {
      x = 1;
      y = 0;
    }
    return d;
  }
  long long modinverse() const {
    long long x, y;
    extgcd(num, mod, x, y);
    return (mod + x % mod) % mod;
  }

public:
  modint() : num(0) {}
  modint(const long long x) : num((x < 0 ? (x + mod) : x) % mod) {}
  friend modint operator+(const modint &a, const modint &b) {
    return modint(a.num + b.num);
  }
  friend modint operator-(const modint &a, const modint &b) {
    return modint(a.num - b.num);
  }
  friend modint operator*(const modint &a, const modint &b) {
    return modint(a.num * b.num);
  }
  friend modint operator/(const modint &a, const modint &b) {
    return modint(a * b.modinverse());
  }
  modint &operator+=(const modint &b) { return (*this) = (*this) + b; }
  modint &operator-=(const modint &b) { return (*this) = (*this) - b; }
  modint &operator*=(const modint &b) { return (*this) = (*this) * b; }
  modint &operator/=(const modint &b) { return (*this) = (*this) / b; }
  modint &operator++() {
    (*this) += 1;
    return (*this);
  }
  modint &operator--() {
    (*this) -= 1;
    return (*this);
  }
  modint operator++(int) {
    modint tmp(*this);
    ++(*this);
    return tmp;
  }
  modint operator--(int) {
    modint tmp(*this);
    --(*this);
    return tmp;
  }
  modint pow(unsigned long long i) {
    if (i == 0)
      return 1;
    if (i & 1) {
      return (*this) * (this->pow(i - 1));
    }
    modint<mod> hpow = (this->pow(i >> 1));
    return hpow * hpow;
  }
  friend ostream &operator<<(ostream &os, const modint &a) {
    os << a.num;
    return os;
  }
  friend istream &operator>>(istream &is, modint &a) {
    long long x;
    is >> x;
    a = x;
    return is;
  }
};
using mint = modint<1000000007>;

int k;
int n;
string s;

mint Rec(int i, int j) {
  static map<P<int, int>, mint> dp;
  if (n + k - i + 1 < n - j)
    return 0;
  if (i > n + k) {
    if (j == n)
      return 1;
    else
      return 0;
  }
  if (j == n) {
    return mint(26).pow(n + k - i + 1);
  }
  if (dp.find({i, j}) != dp.end())
    return dp[{i, j}];
  if (j == n) {
    return dp[{i, j}] = Rec(i + 1, j) * 26;
  } else {
    return dp[{i, j}] = Rec(i + 1, j) * 25 + Rec(i + 1, j + 1);
  }
  return 0;
}

int main() {
  cin >> k >> s;
  n = (int)s.size();
  cout << Rec(1, 0) << endl;
  return 0;
}