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

#pragma GCC optimize("O3")

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
tree<int, int, greater<int>, rb_tree_tag, tree_order_statistics_node_update>
    map_t;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); ++i)
#define sz(x) (int)x.size()
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
#define REV(x) reverse(x.begin(), x.end())
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
const int MOD = 1e9 + 7;
int n;
const int mxN = 2e6 + 5;
ll fact[mxN];

ll mul(ll a, ll b) { return (a * b) % MOD; }

void precompute() {
  fact[0] = 1;
  FOR(i, 1, mxN)
  fact[i] = mul(fact[i - 1], i);
}

ll modpow(ll a, ll b) {
  if (b == 0)
    return 1;
  ll res = modpow(a, b / 2);
  if (b % 2)
    return mul(a, mul(res, res));
  return mul(res, res);
}

ll nCr(ll a, ll b) {
  if (b > a)
    return 0;
  ll num = fact[a];
  ll den = mul(fact[b], fact[a - b]);
  return mul(num, modpow(den, MOD - 2));
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  precompute();
  int k;
  cin >> k;
  string s;
  cin >> s;
  n = sz(s) + k;
  ll tot = 0;
  F0R(i, n) {
    ll cur = modpow(26, i);
    cur = mul(cur, mul(nCr(n - i - 1, sz(s) - 1), modpow(25, n - i - sz(s))));
    tot = (tot + cur) % MOD;
  }
  cout << tot;

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O3")

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
tree<int, int, greater<int>, rb_tree_tag, tree_order_statistics_node_update>
    map_t;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); ++i)
#define sz(x) (int)x.size()
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
#define REV(x) reverse(x.begin(), x.end())
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
const int MOD = 1e9 + 7;
int n;
const int mxN = 2e6 + 5;
ll fact[mxN];

ll mul(ll a, ll b) {
  a = (a % MOD + MOD) % MOD;
  b = (b % MOD + MOD) % MOD;
  return (a * b) % MOD;
}

void precompute() {
  fact[0] = 1;
  FOR(i, 1, mxN)
  fact[i] = mul(fact[i - 1], i);
}

ll modpow(ll a, ll b) {
  a = (a % MOD + MOD) % MOD;
  if (b == 0)
    return 1;
  ll res = modpow(a, b / 2);
  if (b % 2)
    return mul(a, mul(res, res));
  return mul(res, res);
}

ll nCr(ll a, ll b) {
  if (b > a)
    return 0;
  ll num = fact[a];
  ll den = mul(fact[b], fact[a - b]);
  return mul(num, modpow(den, MOD - 2));
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  precompute();
  int k;
  cin >> k;
  string s;
  cin >> s;
  n = sz(s) + k;
  ll tot = 0;
  F0R(i, n) {
    ll cur = modpow(26, i);
    cur = mul(cur, mul(nCr(n - i - 1, sz(s) - 1), modpow(25, n - i - sz(s))));
    tot = (tot + cur) % MOD;
  }
  cout << tot;

  return 0;
}