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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 10000000000000000
#define mod 998244353

vector<ll> f(200005);
ll modpow(ll a, ll n) { // 繰り返し二乗法
  if (n == 0)
    return 1;
  if (n == 1)
    return a % mod;
  if (n % 2 == 1)
    return (a * modpow(a, n - 1)) % mod;
  ll t = modpow(a, n / 2);
  return (t * t) % mod;
}

ll combi(ll n, ll r) {
  return f[n] * modpow(f[n - r], mod - 2) % mod * modpow(f[r], mod - 2) % mod;
}
/*mainに

f[0]=1;
for(ll i=1;i<=200000;i++){f[i]=(f[i-1]*i)%mod;}を入れる

*/
int main() {
  ll n, m, k, i; // m:色の種類 k:同色で隣接可能な数
  cin >> n >> m >> k;

  f[0] = 1;
  for (ll i = 1; i <= 200000; i++) {
    f[i] = (f[i - 1] * i) % mod;
  }

  ll ans = 0;
  for (i = 0; i <= k; i++) {
    ans += ((combi(n - 1, i) * m) % mod) * modpow(m - 1, n - 1 - i) % mod;
    ans %= mod;
  }
  // combi(n-1,i):分け方
  // m*modpow(m-1,n):色の割り当て方

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <chrono>

using namespace std;
using namespace std::chrono;

/// ----------------------------- (Debug) ------------------------------
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
vector<char *> tokenizer(const char *args) {
  char *token = new char[111];
  strcpy(token, args);
  token = strtok(token, ", ");
  vector<char *> v({token});
  while (token = strtok(NULL, ", "))
    v.push_back(token);
  return reverse(v.begin(), v.end()), v;
}
void debugg(vector<char *> args) { cerr << "\\b\\b "; }
template <typename Head, typename... Tail>
void debugg(vector<char *> args, Head H, Tail... T) {
  debug() << " [" << args.back() << ": " << H << "] ";
  args.pop_back();
  debugg(args, T...);
}
#define harg(...) #__VA_ARGS__
#ifndef ONLINE_JUDGE
#define dbg(...)                                                               \\
  {                                                                            \\
    debugg(tokenizer(harg(__VA_ARGS__, \\b\\t-- > Line)), __VA_ARGS__,           \\
           __LINE__);                                                          \\
    cerr << endl;                                                              \\
  }
#else
#define dbg(...)                                                               \\
  {}
#endif
/// -----------------------------------------------------------------------

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define print(x) cout << x << "\\n";
#define REP(i, a, b) for (i = a; i <= b; i++)
#define RAP(i, a, b) for (i = a; i >= b; i--)
#define spa << " " <<
#define all(x) (x).begin(), (x).end()
int mod = 998244353;
const int MX = 0x3f3f3f3f;

int main() {
  IOS;
  int n, m, k, i, j;
  cin >> n >> m >> k;
  vector<ll> dp(k + 2);
  dp[0] = m;
  // dbg(dp[0],dp[k]);
  REP(i, 2, n) {
    // dbg(k);
    RAP(j, k, 0) {
      (dp[j + 1] += dp[j]) %= mod;
      (dp[j] = (m - 1) * dp[j]) %= mod;
      // dbg(j);
    }
  }
  ll ans = 0;
  REP(i, 0, k) ans += dp[i], ans %= mod;
  cout << ans;
  return 0;
}