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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define Rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = b - 1; i >= a; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
const double pi = acos(-1.0);
// const ll MOD = 1e9 + 7;
// const ll INF = 1LL << 60;
#define pp pair<ll, pair<ll, ll>>
#define fi first
#define se second

////////////////////////////////////////////////////////

const ll MOD = 998244353;

const int MAX = 510000;

long long fac[MAX], finv[MAX], inv[MAX];

ll n, m, k;
ll d[200010];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

void init() {
  ll a = 1;
  rep(i, 0, n) {
    d[i] = a;
    a *= m - 1;
    a %= MOD;
  }
}

ll f(ll k) {
  ll ret = m * COM(n - 1, k);
  ret %= MOD;
  ret *= d[n - 1 - k];
  ret %= MOD;
  return ret;
}

int main() {
  COMinit();

  cin >> n >> m >> k;

  init();

  ll ans = 0;
  Rep(i, 0, k) {
    ans += f(i);
    ans %= MOD;
  }

  co(ans);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define Rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = b - 1; i >= a; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
const double pi = acos(-1.0);
// const ll MOD = 1e9 + 7;
// const ll INF = 1LL << 60;
#define pp pair<ll, pair<ll, ll>>
#define fi first
#define se second

////////////////////////////////////////////////////////

const ll MOD = 998244353;

const int MAX = 510000;

long long fac[MAX], finv[MAX], inv[MAX];

ll n, m, k;

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll f(ll k) {
  ll ret = m * COM(n - 1, k);
  ret %= MOD;
  ll d = 1;
  rep(i, 0, n - 1 - k) {
    d *= m - 1;
    d %= MOD;
  }
  ret *= d;
  ret %= MOD;
  return ret;
}

int main() {
  COMinit();

  cin >> n >> m >> k;

  ll ans = 0;
  Rep(i, 0, k) {
    ans += f(i);
    ans %= MOD;
  }

  co(ans);

  return 0;
}