alexjercan/bugnet huggingface dataset:
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mkp make_pair
#define f(i, l, h) for (ll i = l; i < h; i++)
#define fr(i, h, l) for (ll i = h; i >= l; i--)

using namespace std;

typedef pair<ll, ll> pll;
const ll mod = (ll)(998244353);
typedef vector<ll> vll;
ll fact[(ll)(2e5 + 1)];

ll poww(ll a, ll b) {
  ll res = 1;
  a %= mod;

  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;

    a = (a * a) % mod;
    b >>= 1;
  }

  return res;
}

ll C(ll n, ll r) {
  if (n < r) {
    return 0;
  }

  return (((poww(fact[n - r], mod - 2) * poww(fact[r], mod - 2)) % mod) *
          fact[n]) %
         mod;
}

int main() {
  // cout << "Hello world!" << endl;
  ll n, m, k;
  cin >> n >> m >> k;

  fact[0] = 1;
  f(i, 1, 2e5 + 1) { fact[i] = (fact[i - 1] * i) % mod; }

  ll temp = 0;
  f(i, 0, k + 1) {
    temp = (temp + (((C(n - 1, i) * m) % mod) * poww(m - 1, n - 1 - i)) % mod) %
           mod;
  }
  cout << temp << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mkp make_pair
#define f(i, l, h) for (ll i = l; i < h; i++)
#define fr(i, h, l) for (ll i = h; i >= l; i--)

using namespace std;

typedef pair<ll, ll> pll;
const ll mod = (ll)(998244353);
typedef vector<ll> vll;
ll fact[(ll)(2e5 + 1)];

ll poww(ll a, ll b) {
  ll res = 1;
  a %= mod;

  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;

    a = (a * a) % mod;
    b >>= 1;
  }

  return res;
}

ll C(ll n, ll r) {
  if (n < r) {
    return 0;
  }

  ll lower = n - r, curr = 1;

  while (n > lower) {
    curr = (curr * n) % mod;

    n--;
  }

  return (curr * poww(fact[r], mod - 2)) % mod;
}

int main() {
  // cout << "Hello world!" << endl;
  ll n, m, k;
  cin >> n >> m >> k;

  fact[0] = 1;
  f(i, 1, 2e5 + 1) { fact[i] = (fact[i - 1] * i) % mod; }

  ll temp = 0;
  f(i, 0, k + 1) {
    temp = (temp + (((C(n - 1, i) * m) % mod) * poww(m - 1, n - 1 - i)) % mod) %
           mod;
  }
  cout << temp << endl;

  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 998244353;

ll pd[200005];
ll rd[200005];
ll Ep(int x, int y) {
  if (y == 0)
    return 1;
  if (y == 1)
    return x;
  ll r = Ep(x, y / 2);
  if (y % 2 == 1)
    return r * r % MOD * x % MOD;
  else
    return r * r % MOD;
}
ll Ftl(ll x) {
  if (x == 1 || x == 0)
    return 1;
  if (pd[x] != 0)
    return pd[x];
  return pd[x] = Ftl(x - 1) * x % MOD;
}
ll rFtl(ll x) {
  if (x == 1 || x == 0)
    return 1;
  if (rd[x] != 0)
    return rd[x];
  return rd[x] = rFtl(x - 1) * Ep(x, MOD - 2) % MOD;
}
ll comb(ll m, ll n) {
  if (m < n)
    return 0;
  return Ftl(m) * rFtl(n) % MOD * rFtl(m - n) % MOD;
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  for (int i = 0; i <= k; ++i) {
    ll nans = m * Ep(m - 1, n - i - 1) % MOD;
    nans *= comb(n - 1, i);
    nans %= MOD;
    ans += nans;
    ans %= MOD;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 998244353;

ll pd[200005];
ll rd[200005];
ll Ep(int x, int y) {
  if (y == 1)
    return x;
  ll r = Ep(x, y / 2);
  if (y % 2 == 1)
    return r * r % MOD * x % MOD;
  else
    return r * r % MOD;
}
ll Ftl(ll x) {
  if (x == 1 || x == 0)
    return 1;
  if (pd[x] != 0)
    return pd[x];
  return pd[x] = Ftl(x - 1) * x % MOD;
}
ll rFtl(ll x) {
  if (x == 1 || x == 0)
    return 1;
  if (rd[x] != 0)
    return rd[x];
  return rd[x] = rFtl(x - 1) * Ep(x, MOD - 2) % MOD;
}
ll comb(ll m, ll n) {
  if (m < n)
    return 0;
  return Ftl(m) * rFtl(n) % MOD * rFtl(m - n) % MOD;
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  for (int i = 0; i <= k; ++i) {
    ll nans = m * Ep(m - 1, n - i - 1) % MOD;
    nans *= comb(n - 1, i);
    nans %= MOD;
    ans += nans;
    ans %= MOD;
  }
  cout << ans << endl;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

// by szh
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define fi first
#define SZ(x) (int((x).size()))
#define se second
#define ll long long
#define pq priority_queue
#define MP make_pair
#define pii pair<int, int>
#define mod 998244353
#define inf 0x3f3f3f3f
#define debug(x) cerr << #x << "=" << x << '\\n'
#define rep(i, a, b) for (int i = a; i < (b); i++)

const int maxn = 3e5 + 10;

int n, m, K;
ll fact[maxn], invf[maxn];

ll p0w(ll base, int p) {
  ll res = 1;
  while (p) {
    if (p % 2 == 1)
      (res *= base) %= mod;
    (base *= base) %= mod;
    p /= 2;
  }
  return res;
}

ll C(int x, int y) {
  if (y == 0 || x == y)
    return 1;
  return fact[x] * invf[y] % mod * invf[x - y] % mod;
}

int main() {
  std::ios::sync_with_stdio(false);
  invf[0] = fact[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fact[i] = fact[i - 1] * (ll)i % mod;
    invf[i] = p0w(fact[i], mod - 2);
  }
  cin >> n >> m >> K;
  n--;
  int ans = 0;
  rep(i, 0, K + 1) { (ans += C(n, i) * p0w(m - 1, n - i) % mod) %= mod; }
  ans = (ll)ans * (ll)m % mod;
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

// by szh
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define fi first
#define SZ(x) (int((x).size()))
#define se second
#define ll long long
#define pq priority_queue
#define MP make_pair
#define pii pair<int, int>
#define mod 998244353
#define inf 0x3f3f3f3f
#define debug(x) cerr << #x << "=" << x << '\\n'
#define rep(i, a, b) for (int i = a; i < (b); i++)

const int maxn = 1e5 + 10;

int n, m, K;
ll fact[maxn], invf[maxn];

ll p0w(ll base, int p) {
  ll res = 1;
  while (p) {
    if (p % 2 == 1)
      (res *= base) %= mod;
    (base *= base) %= mod;
    p /= 2;
  }
  return res;
}

ll C(int x, int y) {
  if (y == 0 || x == y)
    return 1;
  return fact[x] * invf[y] % mod * invf[x - y] % mod;
}

int main() {
  std::ios::sync_with_stdio(false);
  invf[0] = fact[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fact[i] = fact[i - 1] * (ll)i % mod;
    invf[i] = p0w(fact[i], mod - 2);
  }
  cin >> n >> m >> K;
  n--;
  int ans = 0;
  rep(i, 0, K + 1) { (ans += C(n, i) * p0w(m - 1, n - i) % mod) %= mod; }
  ans = (ll)ans * (ll)m % mod;
  cout << ans;
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

const long MOD = 998244353;
using B = bitset<30>;
const B b_mod = MOD - 2;

long my_pow(int n, B b) {
  long m = 1;
  long c = n;
  for (int i = 0; i < b.size(); i++) {
    if (b.test(i)) {
      m *= c;
      m %= MOD;
    }
    c *= c;
    c %= MOD;
  }
  return m;
}

long inv(int n) { return my_pow(n, b_mod); }

int main() {
  /*
  k = 0, 1, ... Kにおける
  (N-1) C k * M * (M-1)^(N-1-k)
  の総和を求める
  */

  int N, M, K;
  cin >> N >> M >> K;
  // k = 0 のとき 1 * M * (M-1)^(N-1)
  B b_n = N - 1;
  long p = (my_pow(M - 1, b_n) * M) % MOD;
  long inv_m = inv(M - 1);
  long ans = p;
  long comb = 1;

  // k > 1のとき
  for (int i = 1; i <= K; i++) {
    comb = (((comb * (N - i)) % MOD) * inv(i)) % MOD;
    p = (p * inv_m) % MOD;
    ans += (p * comb) % MOD;
    ans %= MOD;
  }

  if (N - 1 == K && K > 0 && M == 1) {
    ans++;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

const long MOD = 998244353;

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  long DP[2][K + 1];
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < K + 1; j++) {
      DP[i][j] = 0;
    }
  }
  DP[0][0] = M;
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < K + 1; j++) {
      DP[1][j] += (DP[0][j] * (M - 1)) % MOD;
      if (j > 0) {
        DP[1][j] += DP[0][j - 1];
        DP[1][j] %= MOD;
      }
      // cout << DP[i][j] << ' ';
    }
    for (int j = 0; j < K + 1; j++) {
      DP[0][j] = DP[1][j];
      DP[1][j] = 0;
      // cout << DP[0][j] << ' ';
    }
    // cout << endl;
  }
  long ans = 0;
  for (int i = 0; i < K + 1; i++) {
    ans = (ans + DP[0][i]) % MOD;
  }
  cout << ans << endl;
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int64_t MOD = 998244353;
double PI = 3.141592653589793;
int64_t MAX = 500000;
vector<int64_t> fac(MAX), finv(MAX), inv(MAX);

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;
  }
}

int64_t COM(int64_t n, int64_t k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int64_t mypow(int64_t x, int64_t y) // x ^ y
{
  if (y == 0)
    return 1;
  if (y == 1)
    return x;
  else if (y % 2 == 1)
    return mypow(x, y - 1) * x % MOD;
  else {
    int64_t tmp = mypow(x, y / 2);
    return tmp * tmp % MOD;
  }
}

int main() {
  COMinit();
  int64_t N, M, K, tmp, ans = 0;
  cin >> N >> M >> K;
  for (int64_t i = 0; i <= K; i++) {
    tmp = M * mypow(M - 1, N - i - 1) % MOD;
    tmp = (tmp * COM(N - 1, i)) % MOD;
    ans = (ans + tmp) % MOD;
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int64_t MOD = 998244353;
double PI = 3.141592653589793;
int64_t MAX = 500000;
vector<int64_t> fac(MAX), finv(MAX), inv(MAX);

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;
  }
}

int64_t COM(int64_t n, int64_t k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int64_t mypow(int64_t x, int64_t y) // x ^ y
{
  if (y == 1)
    return x;
  else if (y % 2 == 1)
    return mypow(x, y - 1) * x % MOD;
  else {
    int64_t tmp = mypow(x, y / 2);
    return tmp * tmp % MOD;
  }
}

int main() {
  COMinit();
  int64_t N, M, K, tmp, ans = 0;
  cin >> N >> M >> K;
  for (int64_t i = 0; i <= K; i++) {
    tmp = M * mypow(M - 1, N - i - 1) % MOD;
    tmp = (tmp * COM(N - 1, i)) % MOD;
    ans = (ans + tmp) % MOD;
  }
  cout << ans;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

// constexpr int MOD = 1000000007;
constexpr int MOD = 998244353;
constexpr int MAX = 510000;
vector<long> fact(MAX), finv(MAX), inve(MAX);

void init() {
  fact.at(0) = fact.at(1) = 1;
  finv.at(0) = finv.at(1) = 1;
  inve.at(1) = 1;
  for (int i = 2; i < MAX; i++) {
    fact.at(i) = fact.at(i - 1) * i % MOD;
    inve.at(i) = MOD - inve.at(MOD % i) * (MOD / i) % MOD;
    finv.at(i) = finv.at(i - 1) * inve.at(i) % MOD;
  }
}

long com(long n, long r) {
  if (n < r || n < 0 || r < 0)
    return 0;
  return fact.at(n) * (finv.at(r) * finv.at(n - r) % MOD) % MOD;
}

long mpow(long a, long b) {
  long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

long pcom(long n, long r) {
  long x = 1, y = 1;
  for (int i = 1; i <= r; i++) {
    x = x * (n - i + 1) % MOD;
    y = y * i % MOD;
  }
  return x * mpow(y, MOD - 2) % MOD;
}

long inv(long a) {
  long b = MOD, u = 1, v = 0;
  while (b) {
    long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= MOD;
  if (u < 0)
    u += MOD;
  return u;
}

int main() {
  ios::sync_with_stdio(false);
  init();
  long N, M, K;
  cin >> N >> M >> K;
  long ans = 0;
  for (int i = 0; i <= K; i++) {
    long tmp = com(N - 1, i);
    tmp *= M;
    tmp %= MOD;
    tmp *= mpow(M - 1, N - i - 1);
    tmp %= MOD;
    ans += tmp;
    ans %= MOD;
  }
  cout << ans << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// constexpr int MOD = 1000000007;
constexpr int MOD = 998244353;
vector<long> fact, inv;

long mpow(long a, long b) {
  long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return res;
}

long com(long n, long r) {
  long x = 1, y = 1;
  for (int i = 1; i <= r; i++) {
    x = x * (n - i + 1) % MOD;
    y = y * i % MOD;
  }
  return x * mpow(y, MOD - 2) % MOD;
}

void init(int n) {
  fact.push_back(1);
  inv.push_back(1);
  for (int i = 0; i < n; i++) {
    fact.push_back(fact.at(i) * (i + 1) % MOD);
    inv.push_back(mpow(fact.at(i + 1), MOD - 2));
  }
}

long qcom(long n, long r) {
  if (n < r || n < 0 || r < 0)
    return 0;
  return fact.at(n) * inv.at(r) % MOD * inv.at(n - r) % MOD;
}

int main() {
  ios::sync_with_stdio(false);
  init(175000);
  long N, M, K;
  cin >> N >> M >> K;
  long ans = 0;
  for (int i = 0; i <= K; i++) {
    long tmp = qcom(N - 1, i);
    tmp *= M;
    tmp %= MOD;
    tmp *= mpow(M - 1, N - i - 1);
    tmp %= MOD;
    ans += tmp;
    ans %= MOD;
  }
  cout << ans << "\\n";
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

// constexpr long MOD = 1000000007;
constexpr long MOD = 998244353;
vector<long> fact, inv;

long mpow(long a, long b) {
  long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return res;
}

long com(long n, long r) {
  long x = 1, y = 1;
  for (int i = 1; i <= r; i++) {
    x = x * (n - i + 1) % MOD;
    y = y * i % MOD;
  }
  return x * mpow(y, MOD - 2) % MOD;
}

void init(int n) {
  fact.push_back(1);
  inv.push_back(1);
  for (int i = 0; i < n; i++) {
    fact.push_back(fact.at(i) * (i + 1) % MOD);
    inv.push_back(mpow(fact.at(i + 1), MOD - 2));
  }
}

long qcom(long n, long r) {
  if (n < r || n < 0 || r < 0)
    return 0;
  return fact.at(n) * inv.at(r) % MOD * inv.at(n - r) % MOD;
}

int main() {
  ios::sync_with_stdio(false);
  init(200000);
  long N, M, K;
  cin >> N >> M >> K;
  long ans = 0;
  for (int i = 0; i <= K; i++) {
    long tmp = qcom(N - 1, i);
    tmp *= M;
    tmp %= MOD;
    tmp *= mpow(M - 1, N - i - 1);
    tmp %= MOD;
    ans += tmp;
    ans %= MOD;
  }
  cout << ans << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// constexpr long MOD = 1000000007;
constexpr long MOD = 998244353;
vector<long> fact, inv;

long mpow(long a, long b) {
  long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return res;
}

long com(long n, long r) {
  long x = 1, y = 1;
  for (int i = 1; i <= r; i++) {
    x = x * (n - i + 1) % MOD;
    y = y * i % MOD;
  }
  return x * mpow(y, MOD - 2) % MOD;
}

void init(int n) {
  fact.push_back(1);
  inv.push_back(1);
  for (int i = 0; i < n; i++) {
    fact.push_back(fact.at(i) * (i + 1) % MOD);
    inv.push_back(mpow(fact.at(i + 1), MOD - 2));
  }
}

long qcom(long n, long r) {
  if (n < r || n < 0 || r < 0)
    return 0;
  return fact.at(n) * inv.at(r) % MOD * inv.at(n - r) % MOD;
}

int main() {
  ios::sync_with_stdio(false);
  init(100000);
  long N, M, K;
  cin >> N >> M >> K;
  long ans = 0;
  for (int i = 0; i <= K; i++) {
    long tmp = qcom(N - 1, i);
    tmp *= M;
    tmp %= MOD;
    tmp *= mpow(M - 1, N - i - 1);
    tmp %= MOD;
    ans += tmp;
    ans %= MOD;
  }
  cout << ans << "\\n";
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long llint;
const llint MOD = 998244353;

template <typename T> T modpow(T m, T n, const T &p) {
  if (n == 0)
    return 1;
  T ret = modpow(m * m % p, n / 2, p);
  if (n % 2)
    ret *= m;
  return ret % p;
}

int main() {
  llint n, m, k;
  cin >> n >> m >> k;
  vector<llint> mul(n, 0), div(n, 0);
  mul[0] = div[0] = 1;
  for (llint i = 0; i <= k; i++) {
    mul[i + 1] = mul[i] * (n - 1 - i);
    div[i + 1] = div[i] * (i + 1);
    mul[i + 1] %= MOD;
    div[i + 1] %= MOD;
  }
  llint ans = 0;
  for (llint i = 0; i <= k; i++) {
    llint v = m * mul[i] % MOD * modpow(div[i], MOD - 2, MOD) % MOD;
    v *= modpow(m - 1, n - 1 - i, MOD), v %= MOD;
    ans += v, ans %= MOD;
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long llint;
const llint MOD = 998244353;

template <typename T> T modpow(T m, T n, const T &p) {
  if (n == 0)
    return 1;
  T ret = modpow(m * m % p, n / 2, p);
  if (n % 2)
    ret *= m;
  return ret % p;
}

template <typename T> T binomial(T n, T r, const T &p) {
  if (r > n - r)
    return binomial(n, n - r, p);
  T ansMul = 1;
  T ansDiv = 1;
  for (T i = 0; i < r; i++) {
    ansMul *= (n - i);
    ansDiv *= (i + 1);
    ansMul %= p;
    ansDiv %= p;
  }

  T ans = (ansMul * modpow(ansDiv, p - 2, p)) % p;
  return ans;
}

int main() {
  llint n, m, k;
  cin >> n >> m >> k;
  llint ans = 0;
  for (llint i = 0; i <= k; i++) {
    ans += (m * binomial(n - 1, i, MOD)) % MOD * modpow(m - 1, n - 1 - i, MOD) %
           MOD;
    ans %= MOD;
  }
  cout << ans << endl;

  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define repr(e, x) for (auto &e : x)
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef pair<int,int> P;
int const INF = 1001001001;
ll const LINF = 1001001001001001001;
ll const MOD = 998244353;

ll mpow(ll x, ll n) {
  if (n == 0)
    return 1;
  else if (n % 2)
    return x * mpow(x, n - 1) % MOD;
  return mpow(x * x % MOD, n / 2) % MOD;
}

ll N, M, K;

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

  ll ans = 0;
  ll comb = 1;
  rep(i, K + 1) {
    ans += M * mpow(M - 1, N - 1 - i) % MOD * comb % MOD;
    ans %= MOD;
    comb *= (N - 1 - i) * mpow(i + 1, MOD - 2) % MOD;
    comb %= MOD;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define repr(e, x) for (auto &e : x)
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef pair<int,int> P;
int const INF = 1001001001;
ll const LINF = 1001001001001001001;
ll const MOD = 998244353;

ll mpow(ll x, ll n) {
  if (n == 0)
    return 1;
  else if (n % 2)
    return x * mpow(x, n - 1) % MOD;
  return mpow(x * x % MOD, n / 2) % MOD;
}

ll mfrac(ll n) {
  if (n == 0)
    return 1;
  else
    return n * mfrac(n - 1) % MOD;
}

ll mperm(ll n, ll r) {
  ll res = 1;
  for (ll i = 0; i < r; i++)
    res = res * (n - i) % MOD;
  return res;
}

ll mcomb(ll n, ll r) {
  if (r > n - r)
    r = n - r;
  ll res = mperm(n, r);
  return res * mpow(mfrac(r), MOD - 2) % MOD;
}

ll N, M, K;

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

  ll ans = 0;
  rep(i, K + 1) {
    ans += M * mpow(M - 1, N - 1 - i) % MOD * mcomb(N - 1, i) % MOD;
    ans %= MOD;
  }

  cout << ans << endl;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<ll, ll> P;

#define MOD 998244353
// a^n%m
ll modpow(ll a, ll n, ll m = MOD) {
  ll r = 1;
  while (n > 0) {
    if (n & 1)
      r = r * a % m;
    a = a * a % m;
    n >>= 1;
  }
  return r;
}
const int COMMAX = 200002;
ll fac[COMMAX], finv[COMMAX], inv[COMMAX];
void COMinit(int m = MOD) {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < COMMAX; i++) {
    fac[i] = fac[i - 1] * i % m;
    inv[i] = m - inv[m % i] * (m / i) % m;
    finv[i] = finv[i - 1] * inv[i] % m;
  }
}
ll COM(int n, int k, int m = MOD) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % m) % m;
}

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll res = 0;
  COMinit();
  for (int i = 0; i <= k; i++) {
    ll tmp = (COM(n - 1, i) * (ll)((m * modpow(m - 1, n - 1 - i)) % MOD)) % MOD;
    res = (res + tmp) % MOD;
  }
  cout << res << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<ll, ll> P;

#define MOD 998244353
// a^n%m
ll modpow(ll a, ll n, ll m = MOD) {
  ll r = 1;
  while (n > 0) {
    if (n & 1)
      r = r * a % m;
    a = a * a % m;
    n >>= 1;
  }
  return r;
}
ll COM(ll n, ll k, ll m = MOD) {
  ll r = 1; // 分母
  ll a = 1; // 分子
  for (int i = 0; i < k; i++) {
    r = (r * (n - i)) % m;
    a = (a * (i + 1)) % m;
  }

  return (r * modpow(a, m - 2, m)) % m;
}

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll res = 0;
  for (int i = 0; i <= k; i++) {
    ll tmp = (COM(n - 1, i) * (ll)((m * modpow(m - 1, n - 1 - i)) % MOD)) % MOD;
    res = (res + tmp) % MOD;
  }
  cout << res << endl;

  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const ll MOD = 998244353;
const int MAX = 200002;

vector<ll> fac(MAX);

void COMinit() {
  fac[0] = 1;
  for (ll i = 1; i <= MAX; i++) {
    fac[i] = (fac[i - 1] * i) % MOD;
  }
}

ll fast(ll a, ll n) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1) {
      ret = (ret * a) % MOD;
    }
    n = (n >> 1);
    a = a * a % MOD;
  }
  return (ret);
}

ll comb(ll n, ll r) {
  ll top = 1;
  ll bot = 1;
  ll ans;

  if (n < r || n < 0 || r < 0)
    return (0);

  top = fac[n];
  bot = fac[r] * fac[n - r] % MOD;
  ans = top * fast(bot, MOD - 2) % MOD;
  return (ans);
}

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

  COMinit();

  ll ans = 0;
  for (int i = 0; i <= K; i++) {
    ll sum = M * fast(M - 1, N - i - 1) % MOD;
    sum *= comb(N - 1, i);
    sum %= MOD;
    ans += sum;
    ans %= MOD;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const ll MOD = 998244353;
const int MAX = 200002;

vector<ll> fac(MAX);

void COMinit() {
  fac[0] = 1;
  for (ll i = 1; i <= MAX; i++) {
    fac[i] = (fac[i - 1] * i) % MOD;
  }
}

ll fast(ll a, ll n) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1) {
      ret = (ret * a) % MOD;
    }
    n = (n >> 1);
    a = a * a % MOD;
  }
  return (ret);
}

ll comb(ll n, ll r) {
  ll top = 1;
  ll bot = 1;
  ll ans;

  top = fac[n];
  bot = fac[r] * fac[n - r] % MOD;
  ans = top * fast(bot, MOD - 2) % MOD;
  return (ans);
}

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

  COMinit();

  ll ans = 0;
  for (int i = 0; i <= K; i++) {
    ll sum = M * fast(M - 1, N - i - 1) % MOD;
    sum *= comb(N - i, i);
    sum %= MOD;
    ans += sum;
    ans %= MOD;
  }
  cout << ans << endl;
}