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

#include <iostream>
#include <string>
#include <vector>

#define ll long long
#define REP(i, n) for (int i = 0; i < n; i++)

#define MOD 1000000007

using namespace std;

int mod = 1000000007;
struct mint {
  ll x;
  // コンストラクタ
  mint(ll x = 0) : x((x % mod + mod) % mod) {}

  // %は若干遅いから，%を使わないようにしている
  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 {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }

  mint power(ll t) const { // x^t
    if (!t)
      return 1;
    mint ans = power(t >> 1);
    ans *= ans;
    if (t & 1)
      ans *= *this;
    return ans;
  }

  // 素数が絡むやつ
  mint inv() const { return power(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

struct combination {
  vector<mint> factorial, ifactorial;
  combination(int n) : factorial(n + 1), ifactorial(n + 1) {
    factorial[0] = 1;
    // 分子　そのまま計算
    for (int i = 1; i <= n; i++)
      factorial[i] = factorial[i - 1] * i;

    // 分母　処理が重いので一発で済ませたい
    ifactorial[n] = factorial[n].inv();
    for (int i = n; i >= 1; i--)
      ifactorial[i - 1] = ifactorial[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || n < k)
      return 0;
    return factorial[n] * ifactorial[k] * ifactorial[n - k];
  }
};

int main(void) {
  // abc171F

  int k;
  string s;
  cin >> k >> s;

  int n = s.size();
  mint ans = 0;
  combination c(n + k);
  for (int i = 0; i <= k; i++) {
    mint tmp = mint(26).power(k - i);
    tmp *= mint(25).power(i);
    tmp *= c(n + i - 1, n - 1);
    ans += tmp;
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#define ll long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define PI 3.141592653589
#define MOD 1000000007

using namespace std;

ll power(ll n, ll r, ll mod) {
  ll ans = 1;
  while (r > 0) {
    if ((r & 1) == 1) {
      ans = ans * n % mod;
    }
    n = n * n % mod;
    r >>= 1;
  }
  return ans % mod;
}

ll comb(ll n, ll r, ll mod) {
  ll x[n + 1], y[n + 1];
  x[0] = 1;
  for (int i = 1; i <= n; i++)
    x[i] = x[i - 1] * i % mod;
  y[n] = power(x[n], mod - 2, mod);
  for (int i = n; i >= 1; i--)
    y[i - 1] = y[i] * i % mod;
  return ((x[n] * y[r]) % mod) * y[n - r] % mod;
}

void solve_F() {
  int k;
  string s;
  cin >> k >> s;

  int n = s.size();
  ll ans = 0;
  for (int i = 0; i <= k; i++) {
    ll tmp = 1;
    tmp = tmp * power(26, k - i, MOD) % MOD;
    tmp = tmp * power(25, i, MOD) % MOD;
    tmp = tmp * comb(n + i - 1, n - 1, MOD) % MOD;
    ans = (ans + tmp) % MOD;
  }
  cout << ans << endl;
}

int main(void) {
  solve_F();

  return 0;
}