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

#include <bits/stdc++.h>
#define x first
#define y second
#define int int long long // 2e9 & 9e18
using namespace std;
const int inf = numeric_limits<int>::max();
struct __io {
  __io() {
    ios_base::Init i;
    ios_base::sync_with_stdio(0);
    cin.tie(0);
  }
} __io;      // fast I/O
#ifdef LOCAL // setting up print debugging (yes lol)
template <class K, class V>
ostream &operator<<(ostream &s, const pair<K, V> &p) {
  s << '<' << p.x << ", " << p.y << '>';
  return s;
}
template <class T, class = typename T::value_type,
          class = typename enable_if<!is_same<T, string>::value>::type>
ostream &operator<<(ostream &s, const T &v) {
  s << '[';
  for (auto &x : v) {
    s << x << ", ";
  }
  if (!v.empty()) {
    s << "\\b\\b";
  }
  s << ']';
  return s;
}
void __prnt() { cerr << endl; }
template <class T, class... Ts> void __prnt(T &&a, Ts &&...etc) {
  cerr << a << ' ';
  __prnt(etc...);
}
#define print(...) __prnt(__VA_ARGS__)
#else
#define print(...)
#endif

const int mod = 998244353;
const int N = 200000;
int f[N + 1], fi[N + 1];

int poww(int a, int n) {
  if (n == 0)
    return 1;
  if (n % 2) {
    return a * poww(a, n - 1) % mod;
  }
  int a2 = poww(a, n / 2);
  return a2 * a2 % mod;
}

int choose(int n, int k) { return (f[n] * fi[k] % mod) * fi[n - k] % mod; }

signed main() {
  f[0] = fi[0] = 1;
  for (int i = 1; i <= N; ++i) {
    f[i] = f[i - 1] * i % mod;
    fi[i] = poww(f[i], mod - 2);
  }

  int n, m, K;
  cin >> n >> m >> K;

  int ans = 0;
  for (int k = 0; k <= K; ++k) {
    int part = m * poww(m - 1, n - k - 1) % mod;
    part = part * choose(n - 1, k) % mod;
    ans = (ans + part) % mod;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define x first
#define y second
#define int int long long // 2e9 & 9e18
using namespace std;
const int inf = numeric_limits<int>::max();
struct __io {
  __io() {
    ios_base::Init i;
    ios_base::sync_with_stdio(0);
    cin.tie(0);
  }
} __io;      // fast I/O
#ifdef LOCAL // setting up print debugging (yes lol)
template <class K, class V>
ostream &operator<<(ostream &s, const pair<K, V> &p) {
  s << '<' << p.x << ", " << p.y << '>';
  return s;
}
template <class T, class = typename T::value_type,
          class = typename enable_if<!is_same<T, string>::value>::type>
ostream &operator<<(ostream &s, const T &v) {
  s << '[';
  for (auto &x : v) {
    s << x << ", ";
  }
  if (!v.empty()) {
    s << "\\b\\b";
  }
  s << ']';
  return s;
}
void __prnt() { cerr << endl; }
template <class T, class... Ts> void __prnt(T &&a, Ts &&...etc) {
  cerr << a << ' ';
  __prnt(etc...);
}
#define print(...) __prnt(__VA_ARGS__)
#else
#define print(...)
#endif

const int mod = 998244353;

int poww(int a, int n) {
  if (n == 0)
    return 1;
  if (n % 2) {
    return a * poww(a, n - 1) % mod;
  }
  int a2 = poww(a, n / 2);
  return a2 * a2 % mod;
}

int choose(int n, int k) {
  int a = 1, b = 1;
  for (int i = 1; i <= k; ++i) {
    a = (a * (n - i + 1)) % mod;
    b = (b * i) % mod;
  }
  return (a * poww(b, mod - 2)) % mod;
}

signed main() {
  int n, m, K;
  cin >> n >> m >> K;

  int ans = 0;
  for (int k = 0; k <= K; ++k) {
    int part = m * poww(m - 1, n - k - 1) % mod;
    part = part * choose(n - 1, k) % mod;
    ans = (ans + part) % mod;
  }
  cout << ans << endl;
}