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

#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define MP(A, B) make_pair(A, B)
#define fi first
#define se second

#define gcd(a, b) __gcd(a, b)
#define pi acos(-1.0)
#define pii pair<int, int>
#define ll long long
#define MAX 1000005
#define MOD 998244353
#define M 998244353
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x & -x)

long long qpow(long long a, long long n) {
  long long ret = 1;
  a = (a % M + M) % M;
  for (; n; n >>= 1) {
    if (n & 1)
      ret = (ret * a) % M;
    a = (a * a) % M;
  }
  return ret;
}

vector<int64_t> fact, seq_inv, fact_inv;

void create_fact_mod(int num) {
  fact[0] = fact[1] = 1;
  for (int i = 2; i <= num; i++)
    fact[i] = fact[i - 1] * i % MOD;
}

void create_seq_inv_mod(int num) {
  seq_inv[0] = seq_inv[1] = 1;
  for (int i = 2; i <= num; i++)
    seq_inv[i] = (MOD - MOD / i) * seq_inv[MOD % i] % MOD;
}

void create_fact_inv_mod(int num) {
  fact_inv[0] = fact_inv[1] = 1;
  for (int i = 2; i <= num; i++)
    fact_inv[i] = fact_inv[i - 1] * seq_inv[i] % MOD;
}

void create_mod_tables(int num) {
  fact.resize(num + 1);
  seq_inv.resize(num + 1);
  fact_inv.resize(num + 1);
  create_fact_mod(num);
  create_seq_inv_mod(num);
  create_fact_inv_mod(num);
}

int64_t comb_mod(int n, int k) {
  return fact[n] * fact_inv[n - k] % MOD * fact_inv[k] % MOD;
}

int64_t perm_mod(int n, int k) { return fact[n] * fact_inv[n - k] % MOD; }

int64_t power_mod(int64_t num, int64_t power) {
  int64_t prod = 1;
  num %= MOD;
  while (power > 0) {
    if (power & 1)
      prod = prod * num % MOD;
    num = num * num % MOD;
    power >>= 1;
  }
  return prod;
}

int64_t extgcd(int64_t a, int64_t b, int64_t &x, int64_t &y) {
  int64_t d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

int64_t inv_mod(int64_t a) {
  int64_t x, y;
  extgcd(a, MOD, x, y);
  return (MOD + x % MOD) % MOD;
}

struct hash_pair {
  template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};

long long n, m, k;
long long dp[200005] = {0};
int main() {
  cin >> n >> m >> k;
  long long ans = 0;
  create_mod_tables(200005);
  // for (int i = 1; i <= n; ++i) {
  //     int t = min((int)k, i-1);
  //     for (int j = t; j >= 0; --j) {
  //         if (i == 1) {dp[j] = m; continue;}
  //         dp[j] = (dp[j-1] + (m-1)*dp[j]) % MOD;
  //     }
  // }
  long long c = 1;
  for (int i = 0; i <= k; ++i) {
    dp[i] = (m * power_mod(m - 1, n - i - 1) % MOD * c) % MOD;
    // dp[i] = (m * qpow(m-1, n-i-1) %MOD * comb_mod(n-i, i)) %MOD;
    c = c * (n - i - 1) % MOD * power_mod(i + 1, MOD - 2) % MOD;
    ans = (ans + dp[i]) % MOD;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define MP(A, B) make_pair(A, B)
#define fi first
#define se second

#define gcd(a, b) __gcd(a, b)
#define pi acos(-1.0)
#define pii pair<int, int>
#define ll long long
#define MAX 1000005
#define MOD 998244353
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x & -x)
ll qpow(ll p, ll q) {
  ll f = 1;
  while (q) {
    if (q & 1)
      f = f * p;
    p = p * p;
    q >>= 1;
  }
  return f;
}

struct hash_pair {
  template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};

long long n, m, k;
int dp[200005] = {0};
int main() {
  cin >> n >> m >> k;
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    int t = min((int)k, i - 1);
    for (int j = t; j >= 0; --j) {
      if (i == 1) {
        dp[j] = m;
        continue;
      }
      dp[j] = (dp[j - 1] + (m - 1) * dp[j]) % MOD;
    }
  }
  for (int i = 0; i <= k; ++i) {
    ans = (ans + dp[i]) % MOD;
  }
  cout << ans << endl;
  return 0;
}