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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define call(var) cout << #var << "=" << var << endl;
#define dup(x, y) (((x) + (y)-1) / (y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using Graph = vector<vi>;
using P = pair<int, int>;
const int MOD = (int)998244353;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
template <class t, class u> inline bool chmax(t &a, u b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class t, class u> inline bool chmin(t &a, u b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

struct mint {
  long long x;
  mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime MOD
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
  friend ostream &operator<<(ostream &os, const mint &oj);
};

ostream &operator<<(ostream &os, const mint &oj) {
  os << oj.x;
  return os;
}

mint factorial(int n) {
  mint res = 1;
  for (int i = 1; i <= n; ++i) {
    res *= i;
  }
  return res;
}

mint nCr(int n, int r) {
  mint numerator = 1;
  if (r > n - r)
    r = n - r;
  for (int i = n; i > n - r; --i) {
    numerator *= i;
  }
  mint denominator = 1;
  for (int i = 1; i <= r; ++i) {
    denominator *= i;
  }
  return numerator / denominator;
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  mint ans = 0;
  mint pp = 1;
  vector<mint> mpow(n);
  rep(i, n) {
    mpow[i] = pp;
    pp *= m - 1;
  }
  vector<mint> ncr(k + 10);
  ncr[k] = nCr(n - 1, k);
  for (int x = k - 1; x >= 0; --x) {
    ncr[x] = ncr[x + 1] * (x + 1) / (n - 1 - x);
  }
  // for(auto& t : ncr) cout << t << " ";
  // cout << endl;
  for (int i = 0; i <= k; ++i) {
    // TLE mint tmp = nCr(n-1, i);
    mint tmp = ncr[i];
    tmp *= m;
    // TLE tmp *= mint(m-1).pow(n-1-i);
    tmp *= mpow[n - 1 - i];
    ans += tmp;
  }
  cout << ans << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define call(var) cout << #var << "=" << var << endl;
#define dup(x, y) (((x) + (y)-1) / (y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using Graph = vector<vi>;
using P = pair<int, int>;
const int MOD = (int)998244353;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
template <class t, class u> inline bool chmax(t &a, u b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class t, class u> inline bool chmin(t &a, u b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

struct mint {
  long long x;
  mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime MOD
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
  friend ostream &operator<<(ostream &os, const mint &oj);
};

ostream &operator<<(ostream &os, const mint &oj) {
  os << oj.x;
  return os;
}

mint factorial(int n) {
  mint res = 1;
  for (int i = 1; i <= n; ++i) {
    res *= i;
  }
  return res;
}

mint nCr(int n, int r) {
  mint numerator = 1;
  if (r > n - r)
    r = n - r;
  for (int i = n; i > n - r; --i) {
    numerator *= i;
  }
  mint denominator = 1;
  for (int i = 1; i <= r; ++i) {
    denominator *= i;
  }
  return numerator / denominator;
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  mint ans = 0;
  mint pp = 1;
  vector<mint> mpow(n);
  rep(i, n) {
    mpow[i] = pp;
    pp *= m - 1;
  }
  for (int i = 0; i <= k; ++i) {
    mint tmp = nCr(n - 1, i);
    tmp *= m;
    // TLE tmp *= mint(m-1).pow(n-1-i);
    tmp *= mpow[n - 1 - i];
    ans += tmp;
  }
  cout << ans << "\\n";
}