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

#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

// Some constants don't work
#ifndef INT_MAX
#define INT_MAX 2147483647
#endif
#ifndef INT_MIN
#define INT_MIN -2147483648
#endif
// short hand tricks
typedef long long ll;

using namespace std;

ll mod = 1e9 + 7;
vector<ll> modfact(2e6 + 1);

// Extended Euclidean Algorithm
ll extended_euclidean(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll x1, y1;
  ll d = extended_euclidean(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return d;
}

void loadModFact(ll n) {
  modfact[0] = 1; // 0!
  for (ll i = 1; i <= n; i++) {
    modfact[i] = (modfact[i - 1] * i) % mod;
  }
}

// Mod arithmetic mod
ll inverse(ll n, ll m) {
  ll x, y;
  ll g = extended_euclidean(n, m, x, y);
  if (g != 1) {
    // No solution should never occur if m is prime
    return 0l;
  }
  x = (x % m + m) % m;
  return x;
}

ll n_choose_k(ll n, ll k) {
  ll res = modfact[n];
  ll div = inverse(modfact[k], mod) * inverse(modfact[n - k], mod) % mod;
  res *= div;
  return res % mod;
}

// all positive in our case
ll powermod(ll n, ll e) {
  if (e == 0) {
    return 1l;
  }
  if (e == 1) {
    return n;
  }
  ll res = (powermod(n, e / 2) * powermod(n, e / 2)) % mod;
  if (e % 2 == 1)
    res *= n;
  res = res % mod;
  return res;
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  ll k;
  string s;
  cin >> k >> s;
  int n = k + s.size();
  // 26^(n)-(number of strings which don't have s as subsequence)
  ll all = powermod(26, n);
  // consider strings with 0,1,s.size-1 subsequence of s
  loadModFact(n);
  for (int i = 0; i < s.size(); i++) {
    ll rem = (n_choose_k(n, i) % mod) * powermod(25, n - i);
    rem = rem % mod;
    all -= rem;
  }
  while (all < 0) {
    all += mod;
  }
  cout << all << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

// Some constants don't work
#ifndef INT_MAX
#define INT_MAX 2147483647
#endif
#ifndef INT_MIN
#define INT_MIN -2147483648
#endif
// short hand tricks
typedef long long ll;

using namespace std;

ll mod = 1e9 + 7;

ll binomialCoefficients(ll n, ll k) {
  ll C[k + 1];
  memset(C, 0, sizeof(C));
  C[0] = 1;
  for (ll i = 1; i <= n; i++) {
    for (ll j = min(i, k); j > 0; j--)
      C[j] = (C[j] + C[j - 1]) % mod;
  }
  return C[k] % mod;
}

// all positive in our case
ll powermod(ll n, ll e) {
  if (e == 0) {
    return 1l;
  }
  if (e == 1) {
    return n;
  }
  ll res = (powermod(n, e / 2) * powermod(n, e / 2)) % mod;
  if (e % 2 == 1)
    res *= n;
  res = res % mod;
  return res;
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  ll k;
  string s;
  cin >> k >> s;
  int n = k + s.size();
  // 26^(n)-(number of strings which don't have s as subsequence)
  ll all = powermod(26, n);
  // consider strings with 0,1,s.size-1 subsequence of s
  for (int i = 0; i < s.size(); i++) {
    ll rem = (binomialCoefficients(n, i) % mod) * powermod(25, n - i);
    rem = rem % mod;
    all -= rem;
  }
  while (all < 0) {
    all += mod;
  }
  cout << all << "\\n";
  return 0;
}