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

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
} c(200005);

int main() {
  int n, m, k;
  cin >> n >> m >> k;

  vector<int> bi(35, 0), b2(35, 0), b0(35, 0);

  mint ans;
  ll x;
  ll xb; // xb^(2^0)*xb^(2^1)*..
  xb = m;
  ll p = 998244353;
  ll xans = 1;
  ans = 0;

  xb = m - 1;
  rep(i, 19) {
    b2[i] = xb;
    xb = (xb * xb) % p;
  }

  x = n - k - 1;
  rep(i, 19) {
    bi[i] = (x >> i) & 1;
    if (bi[i] == 1) {
      xans *= b2[i];
      xans = xans % p;
    }
  }

  mint xans2, ml;
  mint cc;
  for (int i = k; i > -1; --i) {
    if (i != k) {
      xans *= (m - 1);
      xans = xans % p;
    }
    cc = c(n - 1, i);
    xans2 = cc * xans;
    ans += xans2 * m;
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

ll nck(ll n, ll k) {

  ll p = 998244353;
  ll p2;
  p2 = p - 2;
  vector<int> r(35);
  vector<ll> r2(36);

  rep(j, 35) { r[j] = (p2 >> j) & 1; }

  ll ans;
  ans = 1;

  if (k != 0) {
    rep(i, k - 1) {
      r2[0] = i + 2;
      rep(j, 35) {

        r2[j + 1] = (r2[j] * r2[j]) % p;
        if (r[j] == 1) {
          ans *= r2[j];
          ans = ans % p;
        }
      }
    }
    rep(i, k) {
      ans *= n - i;
      ans = ans % p;
    }
  }
  return ans;
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;

  vector<int> bi(35, 0), b2(35, 0), b0(35, 0);

  ll ans, x;
  ll xb; // xb^(2^0)*xb^(2^1)*..
  xb = m;
  ll p = 998244353;
  ll xans = 1;
  ans = 0;

  xb = m - 1;
  rep(i, 19) {
    b2[i] = xb;
    xb = (xb * xb) % p;
  }

  x = n - k - 1;
  rep(i, 19) {
    bi[i] = (x >> i) & 1;
    if (bi[i] == 1) {
      xans *= b2[i];
      xans = xans % p;
    }
  }
  ll nck2 = nck(n - 1, k);

  ll xans2;
  for (int i = k; i > -1; --i) {
    // cout << i << endl;
    if (i != k) {
      xans *= (m - 1);
      nck2 = nck2 * (i + 1);
      while (nck2 % (n - i - 1) != 0) {
        nck2 += p;
      }
      nck2 /= n - i - 1;
      nck2 = nck2 % p;
    }

    xans = xans % p;
    xans2 = xans * nck2;
    // cout << xans << endl;
    xans2 = xans2 % p;
    ans += xans2 * m;
    ans = ans % p;
  }

  cout << ans << endl;

  return 0;
}