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

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll 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 { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll 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 { return mint(*this) /= a; }
};

int main() {
  int n0, m0, k0;
  cin >> n0 >> m0 >> k0;
  mint n = n0, m = m0, k = k0;
  mint ans = 0;
  vector<mint> c(n0 + 1);
  c[0] = 1;
  for (ll i = 1; i < n0 + 1; i++) {
    c[i] = c[i - 1];
    c[i] /= i;
    c[i] *= n0 - i;
  }
  rep(i, k0 + 1) {
    mint memo = m;
    memo *= (m - 1).pow(n0 - 1 - i);
    memo *= c[i];
    ans += memo;
  }
  cout << ans.x << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll 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 { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll 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 { return mint(*this) /= a; }
};

mint comb(ll a, ll b) {
  if (b * 2 > a)
    b = a - b;
  if (b == 0)
    return 1;
  mint x = 1;
  for (int i = 0; i < b; i++) {
    x *= mint(a - i);
    x /= mint(i + 1);
  }
  return x;
}

int main() {
  int n0, m0, k0;
  cin >> n0 >> m0 >> k0;
  mint n = n0, m = m0, k = k0;
  mint ans = 0;
  rep(i, k0 + 1) {
    mint memo = m;
    memo *= (m - 1).pow(n0 - 1 - i);
    memo *= comb(n0 - 1, i);
    ans += memo;
  }
  cout << ans.x << endl;
  return 0;
}