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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define ff first
#define ss second
#define MOD 1000000007LL

using namespace std;
using pii = pair<int, int>;
using ll = long long;

vector<ll> f(3000000);

tuple<ll, ll, ll> exgcd(ll a, ll b) {
  if (b == 0)
    return make_tuple(a, 1, 0);
  ll g, x, y;
  tie(g, x, y) = exgcd(b, a % b);
  return make_tuple(g, y, x - (a / b) * y);
}

ll moddiv(ll a, ll b) {
  ll bb;
  tie(ignore, bb, ignore) = exgcd(b, MOD);
  if (bb < 0)
    b += MOD;
  return (a * bb) % MOD;
}

ll getC(int a, int b) {
  if (a < b)
    return 0;
  return moddiv(moddiv(f[a], f[a - b]), f[b]);
}

ll getH(int a, int b) { return getC(a + b - 1, b); }

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

  f[0] = 1;
  for (int i = 1; i < f.size(); i++)
    f[i] = (f[i - 1] * i) % MOD;

  int k;
  cin >> k;
  string s;
  cin >> s;
  ll answer = 0;
  vector<ll> p26(k + 1), p25(k + 1);
  p26[0] = p25[0] = 1;
  for (int i = 1; i <= k; i++) {
    p26[i] = (p26[i - 1] * 26) % MOD;
    p25[i] = (p25[i - 1] * 25) % MOD;
  }
  for (int i = 0; i <= k; i++) {
    ll tmp = (p26[i] * getH(s.length(), k - i)) % MOD;
    tmp = (tmp * p25[k - i]) % MOD;
    answer = (answer + tmp) % MOD;
  }
  if (answer < 0)
    answer += MOD;
  cout << answer;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)

using namespace std;
long long modPow(long long a, long long x, long long p) {
  // calculates a^x mod p in logarithmic time.
  long long res = 1;
  while (x > 0) {
    if (x % 2 != 0) {
      res = (res * a) % p;
    }
    a = (a * a) % p;
    x /= 2;
  }
  return res;
}

long long modInverse(long long a, long long p) {
  // calculates the modular multiplicative of a mod m.
  //(assuming p is prime).
  return modPow(a, p - 2, p);
}
long long modBinomial(long long n, long long k, long long p) {
  // calculates C(n,k) mod p (assuming p is prime).

  long long numerator = 1; // n * (n-1) * ... * (n-k+1)
  for (int i = 0; i < k; i++) {
    numerator = (numerator * (n - i)) % p;
  }

  long long denominator = 1; // k!
  for (int i = 1; i <= k; i++) {
    denominator = (denominator * i) % p;
  }

  // numerator / denominator mod p.
  return (numerator * modInverse(denominator, p)) % p;
}

int main() {
  IOS;

  long long n;
  long long j;
  string s;
  long long mul = 1;
  long long sum = 0;

  cin >> n;
  cin >> s;
  vector<long long> mul25(n + 1);
  vector<long long> mul26(n + 1);
  mul25[0] = 1;
  for (int i = 1; i <= n; i++)
    mul25[i] = (mul25[i - 1] * 25) % 1000000007;
  mul26[0] = 1;
  for (int i = 1; i <= n; i++)
    mul26[i] = (mul26[i - 1] * 26) % 1000000007;

  for (long long i = 0; i <= n; i++) {
    // s.length() H n-i = s.length()+n-i+1 C n-i
    long long h = modBinomial(s.length() + n - i - 1, n - i, 1000000000 + 7);
    sum +=
        (((mul26[i] * mul25[n - i]) % (1000000000 + 7)) * h) % (1000000000 + 7);
    sum %= 1000000000 + 7;
  }
  cout << sum;
}