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

/*
Author: Babin Dutta
*/

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
#define pb push_back
#define mp make_pair
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define f(i, n) for (int i = 0; i < n; i++)
#define ff(i, a, b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define endl "\\n"
#define inf 1e18
#define mm 998244353

ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll modInverse(ll A, ll M) { return power(A, M - 2, M); }

const int N = 2e6 + 10;
ll fact[N], pow25[N];

ll NCR1(ll n, ll k) {
  ll nCr = fact[n];
  ll d = (fact[n - k] * fact[k]) % mod;
  d = modInverse(d, mod);
  nCr = ((nCr % mod) * (d % mod)) % mod;
  return nCr;
}

void solve() {
  ll k;
  cin >> k;
  string s;
  cin >> s;
  fact[0] = 1;
  pow25[0] = 1;
  ll n = s.length();
  for (int i = 1; i <= n + k + 5; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
    pow25[i] = (pow25[i - 1] * 25) % mod;
  }
  ll ans = 1ll, tmp = 0ll;
  ans = power(26ll, n + k, mod);
  for (ll i = 0; i < n; i++) {
    ll z = NCR1(n + k, i);
    z = ((z % mod) * (pow25[n + k - i] % mod)) % mod;
    ans = (ans - z + mod) % mod;
  }
  cout << ans << endl;
}

int main() {
#ifdef JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  boost;
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

/*
Author: Babin Dutta
*/

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
#define pb push_back
#define mp make_pair
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define f(i, n) for (int i = 0; i < n; i++)
#define ff(i, a, b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define endl "\\n"
#define inf 1e18
#define mm 998244353

ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll modInverse(ll A, ll M) { return power(A, M - 2, M); }

const int N = 2e6 + 10;
ll fact[N], pow25[N];

ll NCR(ll n, ll k) {
  ll nCr = fact[n];
  ll d = (fact[n - k] * fact[k]) % mod;
  d = modInverse(d, mod);
  nCr = ((nCr % mod) * (d % mod)) % mod;
}

void solve() {
  ll k;
  cin >> k;
  string s;
  cin >> s;
  fact[0] = 1;
  pow25[0] = 1;
  ll n = s.length();
  for (int i = 1; i <= n + k + 5; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
    pow25[i] = (pow25[i - 1] * 25) % mod;
  }
  ll ans = 1ll, tmp = 0ll;
  ans = power(26ll, n + k, mod);
  for (ll i = 0; i < n; i++) {
    ll z = NCR(n + k, i);
    z = ((z % mod) * (pow25[n + k - i] % mod)) % mod;
    ans = (ans - z + mod) % mod;
  }
  cout << ans << endl;
}

int main() {
#ifdef JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  boost;
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++)
    solve();
  return 0;
}