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

#ifdef MY_LOCAL
#define MY_NAMESPACE(ns) namespace ns {
#define MY_NAMESPACE_ }
#define MY_DEBUG(s) s
#else
#define MY_NAMESPACE(ns)
#define MY_NAMESPACE_
#define MY_DEBUG(s)
#endif

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define EPS (1e-7)
#define IINF ((int)1e9)

MY_NAMESPACE(testbed)
ll modpow(ll base, ll exponent, ll mod) {
  ll retval = 1;
  while (exponent > 0) {
    if (exponent % 2)
      retval = (retval * base) % mod;
    exponent /= 2;
    base = (base * base) % mod;
  }
  return retval;
}

class ModComb {
public:
  ModComb(int max_n, ll mod)
      : m_mod(mod) // mod and any i (1<i<=max_n) must be mutually prime. in
                   // reality mod must be a prime number and mod>max_n
  {
    m_fac.resize(max_n + 1);
    m_inv.resize(max_n + 1);
    m_fin.resize(max_n + 1);
    m_fac[0] = 1;
    m_fac[1] = 1;
    m_inv[0] = 0; // dummy
    m_inv[1] = 1;
    m_fin[0] = 1;
    m_fin[1] = 1;
    for (int i = 2; i <= max_n; i++) {
      m_fac[i] = m_fac[i - 1] * i % mod;
      m_inv[i] = mod - m_inv[mod % i] * (mod / i) % mod;
      m_fin[i] = m_fin[i - 1] * m_inv[i] % mod;
    }
  }

  ll v(int n, int k) {
    if (n < 0 || k < 0)
      return 0;
    if (n < k)
      return 0;
    return m_fac[n] * (m_fin[k] * m_fin[n - k] % m_mod) % m_mod;
  }

private:
  ll m_mod;
  vector<ll> m_fac; // factorial
  vector<ll> m_inv; // inverse element
  vector<ll> m_fin; // factorial of inverse
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  ll const modval = 998244353;

  int n, m, k;
  cin >> n >> m >> k;

  ModComb modcomb(n, modval);

  ll res = 0;
  for (int this_k = 0; this_k <= k; this_k++) {
    ll this_res = modcomb.v(n - 1, this_k);
    this_res = this_res * m % modval;
    this_res =
        this_res * modpow((ll)m - 1, (ll)n - 1 - (ll)this_k, modval) % modval;
    res = (res + this_res) % modval;
  }
  cout << res << "\\n";
  return 0;
}
MY_NAMESPACE_

Error: TLE

Fixed submission:

#ifdef MY_LOCAL
#define MY_NAMESPACE(ns) namespace ns {
#define MY_NAMESPACE_ }
#define MY_DEBUG(s) s
#else
#define MY_NAMESPACE(ns)
#define MY_NAMESPACE_
#define MY_DEBUG(s)
#endif

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define EPS (1e-7)
#define IINF ((int)1e9)

MY_NAMESPACE(testbed)
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  ll const modval = 998244353;

  int n, m, k;
  cin >> n >> m >> k;

  vector<ll> cases_slice1(k + 1);
  vector<ll> cases_slice2(k + 1);
  vector<ll> &this_slice = cases_slice1;
  vector<ll> &next_slice = cases_slice2;
  this_slice[0] = m;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      next_slice[j] = (this_slice[j] * ((ll)m - 1)) % modval;
      if (j != 0)
        next_slice[j] = (this_slice[j - 1] + next_slice[j]) % modval;
    }
    this_slice.swap(next_slice);
  }

  ll res = 0;
  for (int j = 0; j <= k; j++) {
    res += this_slice[j];
    res %= modval;
  }
  cout << res << "\\n";
  return 0;
}
MY_NAMESPACE_