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

#include <bits/stdc++.h>
#define int int64_t
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define REPP(i, n) for (int i = 1; (i) < (int)(n + 1); ++(i))
#define REPR(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REPRP(i, n) for (int i = (int)(n); (i) >= 0; --(i))
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORP(i, m, n) for (int i = m; i < n + 1; i++)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define ALL(v) v.begin(), v.end()
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())
#define SORTR(c) sort((c).begin(), (c).end(), greater<int>());
#define BIT(n) (1LL << (n))
#define PCNT(x) __builtin_popcountll(x)
#define GCD(x, y) __gcd(x, y)
#define LCM(x, y) x / __gcd(x, y) * y
#define PB push_back
#define MP make_pair
#define endl "\\n"
#define pa cout << ans << endl
#define p(x) cout << x << endl
#define pall(x)                                                                \\
  for (auto iter : x)                                                          \\
  p(iter)
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define yn(x) cout << (x ? "Yes" : "No") << endl
using namespace std;
typedef long double LD;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
using Graph = vector<vector<int>>;
using WeightedGraph = vector<vector<pair<int, int>>>;
const int INF = 2000000000ll;
const long double PI = acosl(-1);
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int MOD = 998244353;

int power(int a, int b) {
  return b ? power(a * a, b / 2) * (b % 2 ? a : 1) : 1;
}
int perm(int N, int K) {
  int x = 1;
  for (int i = 1; i <= K; i++) {
    x = x * (N - i + 1);
  }
  return x;
}
int factorial(int N) { return perm(N, N); }
int comb(int N, int K) {
  return factorial(N) / factorial(K) / factorial(N - K);
}
int hcomb(int N, int K) { return comb(N + K - 1, N - 1); }

int dtoi(double d) { return round(d); }

int isprime(int n) {
  if (n < 2)
    return 0;
  for (int i = 2; i * i <= n; ++i) {
    if (n % i == 0)
      return 0;
  }
  return 1;
}

int mystoi(string S) {
  int num = 0;
  for (int i = (int)S.length() - 1; i >= 0; i--) {
    num += (S[i] - '0') * power(10, S.length() - 1 - i);
  }
  return num;
}

template <class T> int SUM(T a) {
  int sum = 0;
  for (auto iter : a)
    sum += iter;
  return sum;
}

int modpow(int a, int b) {
  return b ? modpow(a * a % MOD, b / 2) * (b % 2 ? a : 1) % MOD : 1;
}

int modperm(int N, int K) {
  int x = 1;
  for (int i = 1; i <= K; i++) {
    x = x * (N - i + 1) % MOD;
  }
  return x % MOD;
}

int modfactorial(int N) { return modperm(N, N); }

int modcomb(int N, int K) {
  if (K < 0 || N < K)
    return 0;
  int x = 1, y = 1;
  for (int i = 1; i <= K; i++) {
    x = x * (N - i + 1) % MOD;
    y = y * i % MOD;
  }
  return x * modpow(y, MOD - 2) % MOD;
}

int modhcomb(int N, int K) { return modcomb(N + K - 1, N - 1); }

struct mint {
  int x;
  constexpr mint(int x = 0) : x((x % MOD + MOD) % MOD) {}
  constexpr mint operator-() const { return mint(-x); }
  constexpr mint &operator+=(const mint &a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  constexpr mint &operator-=(const mint &a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  constexpr mint &operator*=(const mint &a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  constexpr mint operator+(const mint &a) const {
    mint res(*this);
    return res += a;
  }
  constexpr mint operator-(const mint &a) const {
    mint res(*this);
    return res -= a;
  }
  constexpr mint operator*(const mint &a) const {
    mint res(*this);
    return res *= a;
  }
  constexpr mint pow(int t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  constexpr mint inv() const { return pow(MOD - 2); }
  constexpr mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
  constexpr mint operator/(const mint &a) const {
    mint res(*this);
    return res /= a;
  }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

void solve(int N, int M, int K);

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cout << fixed << setprecision(10);
  int N, M, K;
  cin >> N >> M >> K;
  solve(N, M, K);
  return 0;
}

void prepare_comb(int N, VI &fact, VI &invfact) {
  fact.resize(N + 1);
  invfact.resize(N + 1);
  fact[0] = 1;
  REP(i, N) {
    fact[i + 1] = fact[i] * (i + 1) % MOD;
    fact[i + 1] %= MOD;
  }
  invfact[N] = modpow(fact[N], MOD - 2);
  REPR(i, N) {
    invfact[i] = invfact[i + 1] * (i + 1) % MOD;
    invfact[i] %= MOD;
  }
}

void solve(int N, int M, int K) {
  VI fact, invfact;
  prepare_comb(N - 1, fact, invfact);
  mint ans = 0;
  REP(i, K + 1) {
    ans += M * modpow(M - 1, N - i - 1) % MOD * fact[N - 1] % MOD * invfact[i] %
           MOD * invfact[N - 1 - i] % MOD;
  }
  pa;
}

/*
cd ~/Desktop/Programming/AtCoder/contest/abc167/abc167_e && sh test.sh
cd ~/Desktop/Programming/AtCoder/contest/abc167/abc167_e && sh etest.sh
cd ~/Desktop/Programming/AtCoder/contest/abc167/abc167_e && sh submit.sh
*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int int64_t
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define REPP(i, n) for (int i = 1; (i) < (int)(n + 1); ++(i))
#define REPR(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REPRP(i, n) for (int i = (int)(n); (i) >= 0; --(i))
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORP(i, m, n) for (int i = m; i < n + 1; i++)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define ALL(v) v.begin(), v.end()
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())
#define SORTR(c) sort((c).begin(), (c).end(), greater<int>());
#define BIT(n) (1LL << (n))
#define PCNT(x) __builtin_popcountll(x)
#define GCD(x, y) __gcd(x, y)
#define LCM(x, y) x / __gcd(x, y) * y
#define PB push_back
#define MP make_pair
#define endl "\\n"
#define pa cout << ans << endl
#define p(x) cout << x << endl
#define pall(x)                                                                \\
  for (auto iter : x)                                                          \\
  p(iter)
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define yn(x) cout << (x ? "Yes" : "No") << endl
using namespace std;
typedef long double LD;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
using Graph = vector<vector<int>>;
using WeightedGraph = vector<vector<pair<int, int>>>;
const int INF = 2000000000ll;
const long double PI = acosl(-1);
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int MOD = 998244353;

int power(int a, int b) {
  return b ? power(a * a, b / 2) * (b % 2 ? a : 1) : 1;
}
int perm(int N, int K) {
  int x = 1;
  for (int i = 1; i <= K; i++) {
    x = x * (N - i + 1);
  }
  return x;
}
int factorial(int N) { return perm(N, N); }
int comb(int N, int K) {
  return factorial(N) / factorial(K) / factorial(N - K);
}
int hcomb(int N, int K) { return comb(N + K - 1, N - 1); }

int dtoi(double d) { return round(d); }

int isprime(int n) {
  if (n < 2)
    return 0;
  for (int i = 2; i * i <= n; ++i) {
    if (n % i == 0)
      return 0;
  }
  return 1;
}

int mystoi(string S) {
  int num = 0;
  for (int i = (int)S.length() - 1; i >= 0; i--) {
    num += (S[i] - '0') * power(10, S.length() - 1 - i);
  }
  return num;
}

template <class T> int SUM(T a) {
  int sum = 0;
  for (auto iter : a)
    sum += iter;
  return sum;
}

int modpow(int a, int b) {
  return b ? modpow(a * a % MOD, b / 2) * (b % 2 ? a : 1) % MOD : 1;
}

int modperm(int N, int K) {
  int x = 1;
  for (int i = 1; i <= K; i++) {
    x = x * (N - i + 1) % MOD;
  }
  return x % MOD;
}

int modfactorial(int N) { return modperm(N, N); }

int modcomb(int N, int K) {
  if (K < 0 || N < K)
    return 0;
  int x = 1, y = 1;
  for (int i = 1; i <= K; i++) {
    x = x * (N - i + 1) % MOD;
    y = y * i % MOD;
  }
  return x * modpow(y, MOD - 2) % MOD;
}

int modhcomb(int N, int K) { return modcomb(N + K - 1, N - 1); }

struct mint {
  int x;
  constexpr mint(int x = 0) : x((x % MOD + MOD) % MOD) {}
  constexpr mint operator-() const { return mint(-x); }
  constexpr mint &operator+=(const mint &a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  constexpr mint &operator-=(const mint &a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  constexpr mint &operator*=(const mint &a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  constexpr mint operator+(const mint &a) const {
    mint res(*this);
    return res += a;
  }
  constexpr mint operator-(const mint &a) const {
    mint res(*this);
    return res -= a;
  }
  constexpr mint operator*(const mint &a) const {
    mint res(*this);
    return res *= a;
  }
  constexpr mint pow(int t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  constexpr mint inv() const { return pow(MOD - 2); }
  constexpr mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
  constexpr mint operator/(const mint &a) const {
    mint res(*this);
    return res /= a;
  }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

void solve(int N, int M, int K);

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cout << fixed << setprecision(10);
  int N, M, K;
  cin >> N >> M >> K;
  solve(N, M, K);
  return 0;
}

void solve(int N, int M, int K) {
  mint ans = 0;
  REP(i, K + 1) {
    ans += ((M * modpow(M - 1, N - i - 1) % MOD) * modcomb(N - 1, i) % MOD);
  }
  pa;
}

/*
cd ~/Desktop/Programming/AtCoder/contest/abc167/abc167_e && sh test.sh
cd ~/Desktop/Programming/AtCoder/contest/abc167/abc167_e && sh etest.sh
cd ~/Desktop/Programming/AtCoder/contest/abc167/abc167_e && sh submit.sh
*/