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

#include <bits/stdc++.h>
using namespace std;
#define int long long int
const int N = 2 * 1e6 + 10;
vector<int> fact;

const int mod = 1e9 + 7;

// Using binary exponentiation to calculate x^n in log(n) time.
int binExp(int base, int power) {
  int x = 1;
  while (power) {
    if (power % 2 == 1)
      x = (1LL * (x % mod) * (base % mod)) % mod;
    base = (1LL * (base % mod) * (base % mod)) % mod;
    power = power / 2;
  }
  return x;
}

int facto(int num) {
  int result = 1;
  for (int i = 1; i <= num; ++i) {
    result = (1LL * (result % mod) * (i % mod)) % mod;
  }
  return result;
}

int calculate_nCi(int N, int i) {
  // nCi = ( n! ) / (( n-i )! * i!)
  //  int nfact = fact(N);
  //  int ifact = fact(i);
  //  int dfact = fact(N-i);
  int nfact = fact[N];
  int ifact = fact[i];
  int dfact = fact[N - i];
  // Using Euler's theorem of
  // Modular multiplicative
  // inverse to
  // find the inverse of a number.
  // (1/a)%mod=a^(m−2)%mod
  int inv_ifact = binExp(ifact, mod - 2);
  int inv_dfact = binExp(dfact, mod - 2);

  int denm = (1LL * (inv_ifact % mod) * (inv_dfact % mod)) % mod;

  int answer = (1LL * (nfact % mod) * (denm % mod)) % mod;
  return answer;
}

void solve(int N, int s, int k) {
  // Number of ways to form all possible strings
  int allWays = binExp(26, N);

  // Number of ways to form strings that don't
  // contain the input string as a subsequence.
  int noWays = 0;

  for (int i = 0; i < s; ++i) {
    // to calculate nCi
    int nCi = calculate_nCi(N, i);
    // selecting the remaining characters
    //  25 ^ (N-i)
    int remaining = binExp(25, N - i);

    int multiply = (1LL * (nCi % mod) * (remaining % mod)) % mod;

    noWays = (1LL * (noWays % mod) + 1LL * (multiply % mod)) % mod;
  }

  // Answer is the difference of
  // allWays and noWays
  int answer = ((allWays % mod) - (noWays % mod)) % mod;

  if (answer < 0)
    answer += mod;

  cout << answer;
}

void preprocess() {
  fact[0] = 1;
  for (int i = 1; i < N; ++i)
    fact[i] = ((fact[i - 1] % mod) * (i % mod)) % mod;
}

int32_t main() {
  fact = vector<int>(N);
  preprocess();
  // string str = "abc";
  // int k = 2;
  int k;
  string str;
  cin >> k >> str;
  int s = str.length();
  int N = s + k;
  solve(N, s, k);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int

const int mod = 1e9 + 7;

// Using binary exponentiation to calculate x^n in log(n) time.
int binExp(int base, int power) {
  int x = 1;
  while (power) {
    if (power % 2 == 1)
      x = (1LL * (x % mod) * (base % mod)) % mod;
    base = (1LL * (base % mod) * (base % mod)) % mod;
    power = power / 2;
  }
  return x;
}

int fact(int num) {
  int result = 1;
  for (int i = 1; i <= num; ++i) {
    result = (1LL * (result % mod) * (i % mod)) % mod;
  }
  return result;
}

int calculate_nCi(int N, int i) {
  // nCi = ( n! ) / (( n-i )! * i!)
  int nfact = fact(N);
  int ifact = fact(i);
  int dfact = fact(N - i);
  // Using Euler's theorem of
  // Modular multiplicative
  // inverse to
  // find the inverse of a number.
  // (1/a)%mod=a^(m−2)%mod
  int inv_ifact = binExp(ifact, mod - 2);
  int inv_dfact = binExp(dfact, mod - 2);

  int denm = (1LL * (inv_ifact % mod) * (inv_dfact % mod)) % mod;

  int answer = (1LL * (nfact % mod) * (denm % mod)) % mod;
  return answer;
}

void solve(int N, int s, int k) {
  // Number of ways to form all possible strings
  int allWays = binExp(26, N);

  // Number of ways to form strings that don't
  // contain the input string as a subsequence.
  int noWays = 0;

  for (int i = 0; i < s; ++i) {
    // to calculate nCi
    int nCi = calculate_nCi(N, i);
    // selecting the remaining characters
    //  25 ^ (N-i)
    int remaining = binExp(25, N - i);

    int multiply = (1LL * (nCi % mod) * (remaining % mod)) % mod;

    noWays = (1LL * (noWays % mod) + 1LL * (multiply % mod)) % mod;
  }

  // Answer is the difference of
  // allWays and noWays
  int answer = ((allWays % mod) - (noWays % mod)) % mod;

  if (answer < 0)
    answer += mod;

  cout << answer;
}

int32_t main() {
  // string str = "abc";
  // int k = 2;
  int k;
  string str;
  cin >> k >> str;
  int s = str.length();
  int N = s + k;
  solve(N, s, k);
}