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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

namespace {
struct modint {
  static constexpr ll MOD = 1000000007;
  ll x_;
  modint() : x_(0) {}
  modint(ll x) : x_(x % MOD) {}
  modint &operator+=(const modint &src) {
    if ((x_ += src.x_) >= MOD)
      x_ -= MOD;
    return *this;
  }
  modint &operator-=(const modint &src) {
    if ((x_ += MOD - src.x_) >= MOD)
      x_ -= MOD;
    return *this;
  }
  modint &operator*=(const modint &src) {
    (x_ *= src.x_) %= MOD;
    return *this;
  }

  modint &operator/=(const modint &src) { return (*this) *= src.inv(); }
  modint inv() const { return pow(MOD - 2); }
  modint pow(ll t) const {
    if (t == 0)
      return 1;
    modint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
};

ll ModPow(ll a, ll p, ll mod) {
  if (p == 0)
    return 1;
  if (p % 2 == 0) {
    ll halfp = p / 2;
    ll half = ModPow(a, halfp, mod);
    return (half * half) % mod;
  } else {
    return (a * ModPow(a, p - 1, mod)) % mod;
  }
}
ll Combination(int n, int r, ll mod) {
  r = min(r, n - r);
  ll num = 1;
  ll den = 1;
  for (int i = 0; i < r; ++i) {
    num *= (n - i);
    num %= mod;
    den *= (i + 1);
    den %= mod;
  }
  ll ret = (num * ModPow(den, mod - 2, mod)) % mod;
  return ret;
}
} // namespace

void ABC171F() {
  const ll MOD = 1000000007;
  int K;
  cin >> K;
  string S;
  cin >> S;
  int N = S.size();
  modint last = 1;
  modint first = 1;
  for (int i = 0; i < K; ++i) {
    first *= 25;
  }

  modint sum = 0;
  modint comb = Combination(K + N - 1, N - 1, MOD);
  for (int i = 0; i <= K; ++i) {
    int m = K - i + N - 1;
    modint tmp = last;
    tmp *= first;
    tmp *= comb;
    comb *= m - (N - 1);
    comb /= m;

    sum += tmp;
    first /= 25;
    last *= 26;
  }
  cout << sum.x_ << endl;
}

int main() {
  ABC171F();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

namespace {
struct modint {
  static constexpr ll MOD = 1000000007;
  ll x_;
  modint() : x_(0) {}
  modint(ll x) : x_(x % MOD) {}
  modint &operator+=(const modint &src) {
    if ((x_ += src.x_) >= MOD)
      x_ -= MOD;
    return *this;
  }
  modint &operator-=(const modint &src) {
    if ((x_ += MOD - src.x_) >= MOD)
      x_ -= MOD;
    return *this;
  }
  modint &operator*=(const modint &src) {
    (x_ *= src.x_) %= MOD;
    return *this;
  }

  modint &operator/=(const modint &src) { return (*this) *= src.inv(); }
  modint inv() const { return pow(MOD - 2); }
  modint pow(ll t) const {
    if (t == 0)
      return 1;
    modint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
};

ll ModPow(ll a, ll p, ll mod) {
  if (p == 0)
    return 1;
  if (p % 2 == 0) {
    ll halfp = p / 2;
    ll half = ModPow(a, halfp, mod);
    return (half * half) % mod;
  } else {
    return (a * ModPow(a, p - 1, mod)) % mod;
  }
}
ll Combination(int n, int r, ll mod) {
  r = min(r, n - r);
  ll num = 1;
  ll den = 1;
  for (int i = 0; i < r; ++i) {
    num *= (n - i);
    num %= mod;
    den *= (i + 1);
    den %= mod;
  }
  ll ret = (num * ModPow(den, mod - 2, mod)) % mod;
  return ret;
}
} // namespace

void ABC171F() {
  const ll MOD = 1000000007;
  int K;
  cin >> K;
  string S;
  cin >> S;
  int N = S.size();
  modint last = 1;
  modint first = 1;
  for (int i = 0; i < K; ++i) {
    first *= 25;
  }

  modint sum = 0;
  for (int i = 0; i <= K; ++i) {
    int m = K - i + N - 1;
    modint tmp = last;
    tmp *= first;
    tmp *= Combination(m, N - 1, MOD);
    sum += tmp;
    first /= 25;
    last *= 26;
  }
  cout << sum.x_ << endl;
}
int main() {
  ABC171F();
  return 0;
}