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

// Author rahuliitkgp

/*A thing of beauty is a joy forever,
Its loveliness increases,
it will never pass into nothingness.*/
// Men at Work :)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;

#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define ps(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void cpp() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

ll power(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return power((a * a) % mod, b / 2) % mod;
  return (power(a, b - 1) * a) % mod;
}

vector<ll> fact(5000007);

ll nCr(ll n, ll r) {
  ll num = fact[n];
  ll den = (fact[r] * fact[n - r]) % mod;
  return (num * power(den, mod - 2)) % mod;
}

int main() {
  cpp();
  ll k;
  fact[0] = 1;
  for (ll i = 1; i < 5000007; i++) {
    fact[i] = i * fact[i - 1] % mod;
  }
  string s;
  cin >> k >> s;
  ll ans = 0;
  for (ll i = 0; i <= k; i++) {
    ll n = k - i + s.size() - 1;
    ll r = k - i;
    ll a = nCr(n, r);
    ll b = power(26, i);
    ll c = power(25, k - i);
    ans += (((a * b) % mod) * c) % mod;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

// Author rahuliitkgp

/*A thing of beauty is a joy forever,
Its loveliness increases,
it will never pass into nothingness.*/
// Men at Work :)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;

#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define ps(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void cpp() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

ll power(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return power((a * a) % mod, b / 2) % mod;
  return (power(a, b - 1) * a) % mod;
}

ll nCr(ll n, ll r) {
  ll num = 1;
  ll den = 1;
  while (r) {
    num *= n;
    num %= mod;
    den *= r;
    den %= mod;
    n--;
    r--;
  }
  return (num * power(den, mod - 2)) % mod;
}

int main() {
  cpp();
  ll k;
  string s;
  cin >> k >> s;
  ll ans = 0;
  for (ll i = 0; i <= k; i++) {
    ll n = k - i + s.size() - 1;
    ll r = min((ll)s.size() - 1, k - i);
    ll a = nCr(n, r);
    ll b = power(26, i);
    ll c = power(25, k - i);
    ans += (((a * b) % mod) * c) % mod;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}