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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define Graph vector<vector<int>>
typedef long long ll;
typedef pair<int, int> P;
const int INF = 998244353; // INFはそのまま使用

struct Combination {
  vector<ll> F, R;

  Combination(int N) { // 最大の値を代入
    fac(N);
  }

  ll power(ll m, ll n) {
    if (n == 0)
      return 1;
    if (n % 2 == 1)
      return m * power(m, n - 1) % INF;
    ll tmp = power(m, n / 2);
    return tmp * tmp % INF;
  }

  void fac(int N) {
    F.assign(N + 1, 1);
    R.assign(N + 1, 1);
    for (ll i = 1; i <= N; i++) {
      F[i] = F[i - 1] * i % INF;
      R[i] = power(F[i], INF - 2);
    }
  }

  ll com(ll m, ll n) {
    if (m < 0 || n < 0 || m < n)
      return 0;
    ll tmp = F[m];
    tmp *= R[n];
    tmp %= INF;
    tmp *= R[m - n];
    tmp %= INF;
    return tmp;
  }
};

int main() {
  int N, M, K;
  cin >> N >> M >> K;

  Combination bc(200000);

  ll sum = 0, c = M;
  rep(i, N - K - 1) {
    c *= M - 1;
    c %= INF;
  }

  for (ll k = K; k >= 0; k--) {
    ll tmp = c * bc.com(N - 1, k);
    tmp %= INF;

    sum += tmp;
    sum %= INF;

    c *= M - 1;
    c %= INF;
  }

  cout << sum << endl;
}

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define Graph vector<vector<int>>
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 998244353;

ll power(ll m, ll n) {
  if (n == 0)
    return 1;
  if (n % 2 == 1)
    return m * power(m, n - 1) % INF;
  ll tmp = power(m, n / 2);
  return tmp * tmp % INF;
}

ll combination(ll n, ll k) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  ll ue = 1, shita = 1;
  for (ll i = 1; i <= k; i++) {
    ue *= (n - i + 1);
    ue %= INF;
    shita *= i;
    shita %= INF;
  }
  ue *= power(shita, INF - 2);
  ue %= INF;
  return ue;
}

int main() {
  // ll N, M, K;
  // cin >> N >> M >> K;

  // ll M_div = power(M - 1, INF - 2);

  // ll ans = M, sum = 0;

  // rep(i, N - 1){
  //     ans = ans * (M - 1) % INF;
  // }

  // sum = (sum + ans) % INF;

  // for(ll k = 1; k <= K; k++){
  //     ans = ans * M_div % INF;
  //     ans = ans * (N - k) % INF;
  //     ans = ans * power(k, INF - 2) % INF;
  //     sum = (sum + ans) % INF;
  // }

  // cout << sum << endl;

  ll N, M, K;
  cin >> N >> M >> K;

  ll M_div = power(M - 1, INF - 2);

  ll sum = 0;

  for (ll k = 0; k <= K; k++) {
    ll tmp = M;
    tmp *= power(M - 1, N - k - 1);
    tmp %= INF;
    tmp *= combination(N - 1, k);
    tmp %= INF;
    sum += tmp;
    sum %= INF;
  }

  cout << sum << endl;

  // int64_t col = M;
  // Bint ans = 0, b = 1, b_div = 1;

  // for(int64_t i = 1; i <= K; i++){
  //     b = b *  (N - i) / i;
  // }
  // b_div = b % INF;

  // for(int64_t i = 1; i <= N - 1 -K; i++){
  //     col = col * (M - 1) % INF;
  // }

  // for(int64_t i = K; i >= 0; i--){
  //     ans = (ans + b_div * col) % INF;
  //     col = col * (M - 1) % INF;
  //     b = b * i / (N - i);
  //     b_div = b % INF;
  // }

  // cout << ans << endl;
}