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

/*
   Created by Rahul Goel.
*/
/*******************************************************************************/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
/*******************************************************************************/
using namespace std;
using ll = long long;
using ld = long double;
ll mod = 1000000007;
const int INF = 1e9;
const ll LINF = 1e18;
/*******************************************************************************/
/*
.-.. . -. -.- .- / .. ... / .-.. --- ...- . -.-.--
 */
/* For PBDS */
/*Two new methods X.order_of_key() and X.find_by_order()*/
/*
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
*/

/*******************************************************************************/
ll mod_sum() { return 0LL; }
template <typename T, typename... Args> T mod_sum(T a, Args... args) {
  return ((a + mod_sum(args...)) % mod + mod) % mod;
}

ll mod_prod() { return 1LL; }
template <typename T, typename... Args> T mod_prod(T a, Args... args) {
  return (a * mod_prod(args...)) % mod;
}
/*******************************************************************************/
using pii = pair<ll, ll>;
using vi = vector<ll>;
using vb = vector<bool>;
using vvi = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vpii = vector<pii>;
using vvpii = vector<vector<pii>>;
#ifdef ONLINE_JUDGE
#define endl '\\n'
#endif
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define all(c) (c).begin(), (c).end()
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define uniq(c) (c).resize(distance((c).begin(), unique(all(c))))
#define min_pq(t) priority_queue<t, vector<t>, greater<t>>
#define max_pq(t) priority_queue<t>
#define pb push_back
#define ff first
#define ss second
/*******************************************************************************/

/*
    Code begins after this.
*/

vi fac(2e6 + 5);

ll power(ll x, ll y) {
  ll ret = 1;
  while (y) {
    if (y & 1)
      ret = (x * ret) % mod;
    y >>= 1;
    x = (x * x) % mod;
  }
  return ret;
}

ll inv(ll x) { return power(x, mod - 2); }

ll choose(ll n, ll r) {
  ll ret = fac[n];
  ret = (ret * inv(fac[r])) % mod;
  ret = (ret * inv(fac[n - r])) % mod;
  return ret;
}

ll solve() {
  fac[0] = 1;
  for (ll i = 1; i < (ll)fac.size(); i++)
    fac[i] = (fac[i - 1] * i) % mod;

  ll k;
  cin >> k;
  string str;
  cin >> str;

  ll n = str.size() + k;
  ll ans = 0;

  for (ll i = str.size(); i <= n; i++) {
    ans = (ans + (choose(n, i) * power(25, n - i)) % mod) % mod;
  }

  cout << ans << endl;

  return 0;
}

signed main() {
  fastio;

  ll t = 1;
  while (t--)
    solve();

  return 0;
}

/*******************************************************************************/
// Remeber - In a custom comparator cmp(a, b) a==b must always return to false.


Error: TLE

Fixed submission:

/*
   Created by Rahul Goel.
*/
/*******************************************************************************/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
/*******************************************************************************/
using namespace std;
using ll = long long;
using ld = long double;
ll mod = 1000000007;
const int INF = 1e9;
const ll LINF = 1e18;
/*******************************************************************************/
/*
.-.. . -. -.- .- / .. ... / .-.. --- ...- . -.-.--
 */
/* For PBDS */
/*Two new methods X.order_of_key() and X.find_by_order()*/
/*
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
*/

/*******************************************************************************/
ll mod_sum() { return 0LL; }
template <typename T, typename... Args> T mod_sum(T a, Args... args) {
  return ((a + mod_sum(args...)) % mod + mod) % mod;
}

ll mod_prod() { return 1LL; }
template <typename T, typename... Args> T mod_prod(T a, Args... args) {
  return (a * mod_prod(args...)) % mod;
}
/*******************************************************************************/
using pii = pair<ll, ll>;
using vi = vector<ll>;
using vb = vector<bool>;
using vvi = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vpii = vector<pii>;
using vvpii = vector<vector<pii>>;
#ifdef ONLINE_JUDGE
#define endl '\\n'
#endif
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define all(c) (c).begin(), (c).end()
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define uniq(c) (c).resize(distance((c).begin(), unique(all(c))))
#define min_pq(t) priority_queue<t, vector<t>, greater<t>>
#define max_pq(t) priority_queue<t>
#define pb push_back
#define ff first
#define ss second
/*******************************************************************************/

/*
    Code begins after this.
*/

vi fac(2e6 + 5);

ll power(ll x, ll y) {
  ll ret = 1;
  while (y) {
    if (y & 1)
      ret = mod_prod(x, ret);
    y >>= 1;
    x = mod_prod(x, x);
  }
  return ret;
}

ll inv(ll x) { return power(x, mod - 2); }

ll choose(ll n, ll r) {
  ll ret = fac[n];
  ret = mod_prod(ret, inv(fac[r]));
  ret = mod_prod(ret, inv(fac[n - r]));
  return ret;
}

ll solve() {
  fac[0] = 1;
  for (ll i = 1; i < (ll)fac.size(); i++)
    fac[i] = mod_prod(fac[i - 1], i);

  ll k;
  cin >> k;
  string str;
  cin >> str;

  ll n = str.size() + k;
  ll ans = 0;

  for (ll i = str.size(); i <= n; i++) {
    ans = mod_sum(ans, mod_prod(choose(n, i), power(25, n - i)));
  }

  cout << ans << endl;

  return 0;
}

signed main() {
  fastio;

  ll t = 1;
  while (t--)
    solve();

  return 0;
}

/*******************************************************************************/
// Remeber - In a custom comparator cmp(a, b) a==b must always return to false.