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

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\\n';
constexpr double eps = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\\n';
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

constexpr int MOD = 1e9 + 7;
template <int MOD> struct Modint {
  long long val;
  constexpr Modint(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Modint operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Modint operator+(const Modint &r) const noexcept {
    return Modint(*this) += r;
  }
  constexpr Modint operator-(const Modint &r) const noexcept {
    return Modint(*this) -= r;
  }
  constexpr Modint operator*(const Modint &r) const noexcept {
    return Modint(*this) *= r;
  }
  constexpr Modint &operator+=(const Modint &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Modint &operator-=(const Modint &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Modint &operator*=(const Modint &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr bool operator==(const Modint &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Modint &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os,
                                       const Modint<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr istream &operator>>(istream &is, Modint<MOD> &x) noexcept {
    return is >> x.val;
  }

  constexpr Modint<MOD> pow(long long n) const noexcept {
    if (!n)
      return 1;
    auto t = pow(n >> 1);
    t *= t;
    if (n & 1)
      t *= *this;
    return t;
  }
  friend constexpr Modint<MOD> modpow(const Modint<MOD> &a,
                                      long long n) noexcept {
    return a.pow(n);
  }

  friend constexpr Modint<MOD> Ncr(Modint<MOD> a, int n) {
    Modint<MOD> p = 1, q = 1;

    while (n > 0) {
      p *= a;
      a -= 1;
      q *= n--;
    }

    return p / q;
  }

  // for prime mod
  constexpr Modint &operator/=(const Modint &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Modint operator/(const Modint &r) const noexcept {
    return Modint(*this) /= r;
  }
};
using mint = Modint<MOD>;

struct Factorial {

  int modpow(int a, u64 n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = (int)((i64)t * t % mod);
    if (n & 1)
      t = (int)((i64)t * a % mod);
    return t;
  }

  Factorial(int count, int mod, bool calcInverse = true) : mod(mod) {

    f.reserve(count + 1);
    if (calcInverse)
      fi.reserve(count + 1);

    f.push_back(1);
    if (calcInverse)
      fi.push_back(1);
    FOR(i, 1, count + 1) {
      f.push_back((int)(((i64)f[i - 1] * i) % mod));
      if (calcInverse)
        fi.push_back(modpow(f[i], mod - 2));
      // cout << i << " " << f[i] << " " << fi[i] << newl;
    }
  }

  int Ncr(int n, int r) {
    if (n < r)
      return 0;
    return (int)((i64)f[n] * fi[r] % mod * fi[n - r] % mod);
  }

  // without inverse array
  int Ncr2(int n, int r) {
    if (n < r)
      return 0;
    return Div(Div(f[n], f[r]), f[n - r]);
  }

  int Div(int l, int r) noexcept {
    long long a = r, b = mod, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    l = l * u % mod;
    if (l < 0)
      l += mod;
    return l;
  }

  int mod;

  // 階乗
  vector<int> f;

  // 階乗の逆元
  vector<int> fi;
};

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int K;
  string S;
  cin >> K >> S;

  // 部分列として S が存在する、長さ |S| + K の文字列は何通りか

  // = 全事象 - 部分列として S が存在しない、長さ |S| + K の文字列は何通りか

  // 部分列として S[0:i] が存在する文字列Tのパターン数
  // 0: Sの最初の1文字すら含まれない
  // 1: Sの最初1文字だけはどこかにある
  // 2: Sの最初2文字だけは部分列としてある

  // |T|: 1 2 3 ...
  // 0: 25^1 25^2   25^3 ...
  // 1: 1   25^1*2 25^2*3 25^3*4 ...
  // 2: 0      1    25^1*3 25^2*6 25^3*10 ...

  // |T|: 1 2 3 ...
  // 0: 25^1 25^2     25^3 ...
  // 1: 1   25^1*2C1 25^2*3C1 25^3*4C1 ...
  // 2: 0      1     25^1*3C2 25^2*4C2 25^3*5C2 ...

  int ks = S.size() + K;

  Factorial fc(ks, MOD, true);

  mint result = modpow(mint(26), ks);

  vector<mint> p25(ks + 1);
  p25[0] = 1;
  F0R(i, p25.size() - 1) { p25[i + 1] = p25[i] * 25; }

  F0R(i, S.size()) { result -= p25[ks - i] * fc.Ncr(ks, i); }

  cout << result;
}
#endif


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\\n';
constexpr double eps = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\\n';
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

constexpr int MOD = 1e9 + 7;
template <int MOD> struct Modint {
  long long val;
  constexpr Modint(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Modint operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Modint operator+(const Modint &r) const noexcept {
    return Modint(*this) += r;
  }
  constexpr Modint operator-(const Modint &r) const noexcept {
    return Modint(*this) -= r;
  }
  constexpr Modint operator*(const Modint &r) const noexcept {
    return Modint(*this) *= r;
  }
  constexpr Modint &operator+=(const Modint &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Modint &operator-=(const Modint &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Modint &operator*=(const Modint &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr bool operator==(const Modint &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Modint &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os,
                                       const Modint<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr istream &operator>>(istream &is, Modint<MOD> &x) noexcept {
    return is >> x.val;
  }

  constexpr Modint<MOD> pow(long long n) const noexcept {
    if (!n)
      return 1;
    auto t = pow(n >> 1);
    t *= t;
    if (n & 1)
      t *= *this;
    return t;
  }
  friend constexpr Modint<MOD> modpow(const Modint<MOD> &a,
                                      long long n) noexcept {
    return a.pow(n);
  }

  friend constexpr Modint<MOD> Ncr(Modint<MOD> a, int n) {
    Modint<MOD> p = 1, q = 1;

    while (n > 0) {
      p *= a;
      a -= 1;
      q *= n--;
    }

    return p / q;
  }

  // for prime mod
  constexpr Modint &operator/=(const Modint &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Modint operator/(const Modint &r) const noexcept {
    return Modint(*this) /= r;
  }
};
using mint = Modint<MOD>;

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int K;
  string S;
  cin >> K >> S;

  // 部分列として S が存在する、長さ |S| + K の文字列は何通りか

  // = 全事象 - 部分列として S が存在しない、長さ |S| + K の文字列は何通りか

  // 部分列として S[0:i] が存在する文字列Tのパターン数
  // 0: Sの最初の1文字すら含まれない
  // 1: Sの最初1文字だけはどこかにある
  // 2: Sの最初2文字だけは部分列としてある

  // |T|: 1 2 3 ...
  // 0: 25^1 25^2   25^3 ...
  // 1: 1   25^1*2 25^2*3 25^3*4 ...
  // 2: 0      1    25^1*3 25^2*6 25^3*10 ...

  // |T|: 1 2 3 ...
  // 0: 25^1 25^2     25^3 ...
  // 1: 1   25^1*2C1 25^2*3C1 25^3*4C1 ...
  // 2: 0      1     25^1*3C2 25^2*4C2 25^3*5C2 ...

  int ks = S.size() + K;

  mint result = modpow(mint(26), ks);

  F0R(i, S.size()) { result -= modpow(mint(25), ks - i) * Ncr(mint(ks), i); }

  cout << result;
}
#endif