Problem p02632 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl "\n"
#define F first
#define S second
#define def(x) cerr << #x << " is " << x << endl;
#define mod 1000000007
#define pb push_back
#define FOR(i, a, n) for (ll i = a; i < n; i++)
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(a) a.begin(), a.end()
const int NUM = 2e5 + 5;
#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
ll const N = 2e6;
ll dp[N], f[N], inv[N]; // f = factorial, inv[i] = 1/f[i]
ll len, n, ans = 0;
ll bp(ll x, ll n, ll MOD) {
  if (n == 0)
    return 1;
  ll ans = bp(x, n / 2, MOD) % MOD;
  ans = (ans * ans) % MOD;
  if (n % 2 == 0)
    return ans;
  else
    return (ans * x) % MOD;
}
ll calc(ll n, ll r) { return (f[n] * inv[n - r] % mod) * inv[r] % mod; }
int main() {
  ios int num_tests = 1;
  // cin>>num_tests;
  while (num_tests-- > 0) {
    string s;
    cin >> len >> s;
    n = s.size();
    len += n;
    // ans = summation of (C(len,i)*pow(25,len-i))
    // previous one got TLE
    // So we have to precompute
    f[0] = inv[0] = 1;
    FOR(i, 1, len + 1) {
      f[i] = f[i - 1] * i % mod;
      inv[i] = bp(f[i], mod - 2, mod);
    }
    dp[len] = 1;
    for (ll i = len - 1; i >= 1; i--) {
      dp[i] = calc(len, i) * bp(25, len - i, mod) % mod;
      dp[i] += mod;
      dp[i] %= mod;
    }

    FOR(i, n, len + 1) { ans = (ans + dp[i]) % mod; }
    cout << ans;
  }
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl "\n"
#define F first
#define S second
#define def(x) cerr << #x << " is " << x << endl;
#define mod 1000000007
#define pb push_back
#define FOR(i, a, n) for (ll i = a; i < n; i++)
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(a) a.begin(), a.end()
const int NUM = 2e5 + 5;
#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
ll const N = 2e6;
ll dp[N], f[N], inv[N]; // f = factorial, inv[i] = 1/f[i]
ll len, n, ans = 0;
ll bp(ll x, ll y, ll MOD) {
  ll ans = 1;
  while (y) {
    if (y & 1)
      ans = ans * x % MOD;
    x = x * x % MOD, y >>= 1;
  }
  return ans;
}
ll calc(ll n, ll r) { return (f[n] * inv[n - r] % mod) * inv[r] % mod; }
int main() {
  ios int num_tests = 1;
  // cin>>num_tests;
  while (num_tests-- > 0) {
    string s;
    cin >> len >> s;
    n = s.size();
    len += n;
    // ans = summation of (C(len,i)*pow(25,len-i))
    // previous one got TLE
    // So we have to precompute
    f[0] = inv[0] = 1;
    FOR(i, 1, len + 1) {
      f[i] = f[i - 1] * i % mod;
      inv[i] = bp(f[i], mod - 2, mod);
    }
    dp[len] = 1;
    for (ll i = len - 1; i >= 1; i--) {
      dp[i] = calc(len, i) * bp(25, len - i, mod) % mod;
      dp[i] += mod;
      dp[i] %= mod;
    }

    FOR(i, n, len + 1) { ans = (ans + dp[i]) % mod; }
    cout << ans;
  }
}