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

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define ll long long
#define dd double
#define MOD1 1000000007
#define MOD2 998244353
#define nl cout << endl
#define rt ll Q[100005] = {0}
#define mp make_pair
#define test()                                                                 \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define s(ar, n) sort(ar, ar + n)
#define rs(ar, n) sort(ar, ar + n, greater<ll>())
#define oa(a, n)                                                               \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i] << " ";                                                       \\
  nl
#define cn(a, n)                                                               \\
  for (ll i = 0; i < n; i++)                                                   \\
    cin >> a[i];
#define ov(a, n)                                                               \\
  for (ll i = n - 1; i >= 0; i--)                                              \\
    cout << a[i] << " ";                                                       \\
  nl
#define ovp(a, n)                                                              \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i].ff << " " << a[i].ss << endl;
#define maxa(ar, N) *max_element(ar, ar + N)
#define mina(ar, N) *min_element(ar, ar + N)
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
typedef pair<ll, ll> pii;
ll powe(ll n, ll m) {
  if (m == 0)
    return 1;
  ll t = powe(n, m / 2);
  if (m % 2 == 0)
    return (t * t);
  return (((t * t)) * n);
}
ll mpowe(ll n, ll m, ll MOD) {
  if (m == 0)
    return 1;
  ll t = mpowe(n, m / 2, MOD);
  t %= MOD;
  if (m % 2 == 0)
    return (t * t) % MOD;
  return (((t * t) % MOD) * n) % MOD;
}
ll logtwo(ll n) {
  if (n == 1)
    return 0;
  if (n == 0)
    return -1;
  return logtwo(n / 2) + 1;
}
#define maxn 2000005
ll fact[maxn] = {0};
ll ifact[maxn] = {0};
void precompute(ll mod) {
  fact[0] = 1;
  ifact[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] = fact[i] % mod;
    ifact[i] = mpowe(fact[i], mod - 2, mod);
  }
}
ll ncr(ll n, ll r, ll mod) {
  if (r > n)
    return 0;
  ll ans = fact[n] * ifact[n - r];
  ans = ans % mod;
  ans = ans * ifact[r];
  ans = ans % mod;
  return ans;
}

int main() {
  fastio();

  int t = 1;
  precompute(MOD1);
  ll p[maxn] = {0};
  p[0] = 1;
  for (int i = 1; i < maxn; i++)
    p[i] = (p[i - 1] * 25) % MOD1;
  while (t--) {
    ll k;
    cin >> k;
    string s;
    cin >> s;
    ll n = s.size();
    ll ans = mpowe(26, k + n, MOD1);
    for (ll i = 0; i < n; i++) {
      ll d = ncr(n + k, i, MOD1);
      // cout<<n+k<<" "<<i<<" "<<d<<endl;
      d = d * p[n + k - i];
      d = d % MOD1;
      ans = ans - d;
      ans = (ans + MOD1) % MOD1;
      // cout<<ans<<endl;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define ll long long
#define dd double
#define MOD1 1000000007
#define MOD2 998244353
#define nl cout << endl
#define rt ll Q[100005] = {0}
#define mp make_pair
#define test()                                                                 \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define s(ar, n) sort(ar, ar + n)
#define rs(ar, n) sort(ar, ar + n, greater<ll>())
#define oa(a, n)                                                               \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i] << " ";                                                       \\
  nl
#define cn(a, n)                                                               \\
  for (ll i = 0; i < n; i++)                                                   \\
    cin >> a[i];
#define ov(a, n)                                                               \\
  for (ll i = n - 1; i >= 0; i--)                                              \\
    cout << a[i] << " ";                                                       \\
  nl
#define ovp(a, n)                                                              \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i].ff << " " << a[i].ss << endl;
#define maxa(ar, N) *max_element(ar, ar + N)
#define mina(ar, N) *min_element(ar, ar + N)
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
typedef pair<ll, ll> pii;
ll powe(ll n, ll m) {
  if (m == 0)
    return 1;
  ll t = powe(n, m / 2);
  if (m % 2 == 0)
    return (t * t);
  return (((t * t)) * n);
}
ll mpowe(ll n, ll m, ll MOD) {
  if (m == 0)
    return 1;
  ll t = mpowe(n, m / 2, MOD);
  t %= MOD;
  if (m % 2 == 0)
    return (t * t) % MOD;
  return (((t * t) % MOD) * n) % MOD;
}
ll logtwo(ll n) {
  if (n == 1)
    return 0;
  if (n == 0)
    return -1;
  return logtwo(n / 2) + 1;
}
#define maxn 2000005
ll fact[maxn] = {0};
ll ifact[maxn] = {0};
void precompute(ll mod) {
  fact[0] = 1;
  ifact[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] = fact[i] % mod;
    ifact[i] = mpowe(fact[i], mod - 2, mod);
  }
}
ll ncr(ll n, ll r, ll mod) {
  if (r > n)
    return 0;
  ll ans = fact[n] * mpowe(fact[n - r], mod - 2, mod);
  ans = ans % mod;
  ans = ans * mpowe(fact[r], mod - 2, mod);
  ans = ans % mod;
  return ans;
}

int main() {
  fastio();

  int t = 1;
  precompute(MOD1);
  ll p[maxn] = {0};
  p[0] = 1;
  for (int i = 1; i < maxn; i++)
    p[i] = (p[i - 1] * 25) % MOD1;
  while (t--) {
    ll k;
    cin >> k;
    string s;
    cin >> s;
    ll n = s.size();
    ll ans = mpowe(26, k + n, MOD1);
    for (ll i = 0; i < n; i++) {
      ll d = ncr(n + k, i, MOD1);
      // cout<<n+k<<" "<<i<<" "<<d<<endl;
      d = d * p[n + k - i];
      d = d % MOD1;
      ans = ans - d;
      ans = (ans + MOD1) % MOD1;
      // cout<<ans<<endl;
    }
    cout << ans << endl;
  }
}