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

#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < n; i++)
#define REPR(i, n) for (long long i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (long long i = m; i <= n; i++)
#define FORR(i, m, n) for (long long i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(), (v).end()
#define FIN                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using namespace std;
using ll = long long;
using ull = unsigned long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
const ll inf = 1e15;
ll power(ll a, ll b) {
  return b ? power(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1;
}

ll fac[2000010], finv[2000010], inv[2000010];

void init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < 2000010; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

ll C(ll n, ll k) { return fac[n] % mod * finv[k] % mod * finv[n - k] % mod; }

// E
int main() {
  FIN

      ll k;
  cin >> k;
  string s;
  cin >> s;
  ull ans = 0;
  init();
  for (ll i = 0; i <= k; i++) {
    ans = (ans + power(26, i) * power(25, k - i) % mod *
                     C(k + s.size() - i - 1, k - i)) %
          mod;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < n; i++)
#define REPR(i, n) for (long long i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (long long i = m; i <= n; i++)
#define FORR(i, m, n) for (long long i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(), (v).end()
#define FIN                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using namespace std;
using ll = long long;
using ull = unsigned long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
const ll inf = 1e15;
ll power(ll a, ll b) {
  return b ? power(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1;
}

ll C(ll n, ll k) {
  ll x = 1, y = 1;
  for (ll i = 1; i <= k; i++) {
    x = x * (n - i + 1) % mod;
    y = y * i % mod;
  }
  return x * power(y, mod - 2) % mod;
}

// E
int main() {
  FIN

      ll k;
  cin >> k;
  string s;
  cin >> s;
  ll ans = 0;
  for (ll i = 0; i <= k; i++) {
    ll sub = power(26, i);
    sub *= C(k + s.size() - i - 1, s.size() - 1);
    sub %= mod;
    sub *= power(25, k - i);
    sub %= mod;
    ans += sub;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}