alexjercan/bugnet huggingface dataset:
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
static const ll MOD = 998244353;
static const int MAX = 500000 + 100;

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

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

ll ModPow(ll a, ll cur = MOD - 2) {
  ll r = 1;
  while (cur)
    r = r * ((cur % 2) ? a : 1) % MOD, a = a * a % MOD, cur >>= 1;
  return r;
}

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  /*
  先頭の選び方がM
  iが[0,K]で、
  先頭と同じ色のブロックの選び方はcomb(N-1,i)
  残りのブロックの色の選び方はM^(N-1-i);
  */

  ll ans = 0;
  COMinit();

  for (int i = 0; i <= K; i++) {
    ans += M * COM(N - 1, i) % MOD * ModPow(M - 1, N - 1 - i) % MOD;
  }

  cout << ans % MOD << endl;

  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;
static const ll MOD = 998244353;
static const int MAX = 100000;

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

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

ll ModPow(ll a, ll cur = MOD - 2) {
  ll r = 1;
  while (cur)
    r = r * ((cur % 2) ? a : 1) % MOD, a = a * a % MOD, cur >>= 1;
  return r;
}

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  /*
  先頭の選び方がM
  iが[0,K]で、
  先頭と同じ色のブロックの選び方はcomb(N-1,i)
  残りのブロックの色の選び方はM^(N-1-i);
  */

  ll ans = 0;
  COMinit();

  for (int i = 0; i <= K; i++) {
    ans += M * COM(N - 1, i) % MOD * ModPow(M - 1, N - 1 - i) % MOD;
  }

  cout << ans % MOD << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define so(v) sort(v.begin(), v.end())
using ll = long long;
using vi = vector<int>;
using vii = vector<vi>;

const int MAX = 510000;
const int MOD = 998244353;

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

// 二項係数計算
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 n, ll m) {
  // k=0の時の答え
  ll ans = m;
  rep(i, n - 1) {
    ans *= m - 1;
    ans %= MOD;
  }
  return ans;
}
int main() {
  // 前処理
  COMinit();
  ll n, m, k;
  cin >> n >> m >> k;
  ll answer = 0;
  ll ff = f(n - k, m);
  for (int i = k; i >= 0; i--) {
    answer += (COM(n - 1, i) % MOD) * (ff % MOD) % MOD;
    ff = (ff * (m - 1)) % MOD;
    answer %= MOD;
  }
  cout << answer << endl;
  // cout<<COM(n-1,1)%MOD<<endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define so(v) sort(v.begin(), v.end())
using ll = long long;
using vi = vector<int>;
using vii = vector<vi>;

const int MAX = 510000;
const int MOD = 998244353;

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

// 二項係数計算
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 n, ll m) {
  // k=0の時の答え
  ll ans = m;
  rep(i, n - 1) {
    ans *= m - 1;
    ans %= MOD;
  }
  return ans;
}
int main() {
  // 前処理
  COMinit();
  ll n, m, k;
  cin >> n >> m >> k;
  ll answer = 0;
  rep(i, k + 1) {
    answer += (COM(n - 1, i) % MOD) * (f(n - i, m) % MOD) % MOD;
    answer %= MOD;
  }
  cout << answer << endl;
  // cout<<COM(n-1,1)%MOD<<endl;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

long long n, m, k, t = 0;
long long p[200001], q[200001], r[200001];
const long long M = 998244353;

long long pow(long long a, long long b) {
  long long w = 1, x = a;

  while (b) {
    if (b & 1)
      w = w * x % M;

    x = x * x % M;
    b >>= 1;
  }

  return w;
}

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

  p[0] = 1;
  q[0] = 1;
  r[0] = 1;

  for (long long i = 1; i < n; i++) {
    p[i] = p[i - 1] * i % M;
    q[i] = pow(p[i], M - 2);
    r[i] = r[i - 1] * (m - 1) % M;
  }

  for (long long j = 0; j <= k; j++) {
    long long s = m;

    s = (((s * p[n - 1] % M) * q[j] % M) * q[n - j - 1] % M) * r[n - j - 1] % M;
    t = (t + s) % M;
  }

  cout << t << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

long long n, m, k, t = 0;
long long p[200001], q[200001];
const long long M = 998244353;

long long pow(long long a, long long b) {
  long long w = 1, x = a;

  while (b) {
    if (b & 1)
      w = w * x % M;

    x = x * x % M;
    b >>= 1;
  }

  return w;
}

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

  p[0] = 1;
  q[0] = 1;

  for (long long i = 1; i < n; i++) {
    p[i] = p[i - 1] * i % M;
    q[i] = pow(p[i], M - 2);
  }

  for (long long j = 0; j <= k; j++) {
    long long s = m;

    s = ((s * p[n - 1] % M) * q[j] % M) * q[n - j - 1] % M;

    for (long long i = 0; i < n - j - 1; i++)
      s = s * (m - 1) % M;

    t = (t + s) % M;
  }

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

#include <bits/stdc++.h>

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n, s) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define rreps(i, n, s) for (int i = s; i >= n; i--)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using namespace std;
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr ll MOD = 998244353;

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

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

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  COMinit();
  int n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  rep(i, k + 1) {
    ans += ((m * COM(n - 1, i) % MOD) * modpow(m - 1, n - i - 1, MOD)) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n, s) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define rreps(i, n, s) for (int i = s; i >= n; i--)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using namespace std;
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr ll MOD = 998244353;

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

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  COMinit();
  int n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  rep(i, k + 1) {
    ll tmp = m;
    reps(j, n, 1) {
      if (j <= i) {
        tmp *= 1;
      } else {
        tmp *= (m - 1);
      }
      tmp %= MOD;
    }
    tmp *= COM(n - 1, i);
    tmp %= MOD;
    ans += tmp;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
#define rep(i, j, n) for (int i = j; i < (int)(n); i++)
const ll MOD = 998244353;
const ll INFL = (1LL << 60);
const int INFI = (1 << 30);

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

const int MAX = 200002;

vector<ll> fac(MAX), finv(MAX), inv(MAX);

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

ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac.at(n) * (finv.at(k) * finv.at(n - k) % MOD) % MOD;
}

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  ll ans = 0;

  COMinit();

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
#define rep(i, j, n) for (int i = j; i < (int)(n); i++)
const ll MOD = 998244353;
const ll INFL = (1LL << 60);
const int INFI = (1 << 30);

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

ll comb(ll l, ll m) {
  ll t1 = 1, t2 = 1;
  for (ll i = l; i >= l - m + 1; i--)
    t1 = t1 * i % MOD;
  for (ll i = 1; i <= m; i++)
    t2 = t2 * i % MOD;
  t2 = pow_mod(t2, MOD - 2);
  return t1 * t2 % MOD;
}

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

  ll ans = 0;

  for (ll i = 0; i <= K; i++) {
    ans += comb(N - 1, i) * pow_mod(M - 1, N - i - 1) % MOD * M % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

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

const long Beneath = 250000;
// 配列の大きさはBeneath+1で定義する。添字はBeneathまで。1.72*10^8で大体2s

const long mod = 998244353;

vector<long> fac(Beneath + 1), ifac(Beneath + 1);

long mpow(long x, long n) {
  long ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    n = n >> 1;
  }
  return ans;
}

long comb(long a, long b) {
  // long tmp;
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  long tmp = ifac.at(a - b) * ifac.at(b) % mod;
  return tmp * fac.at(a) % mod;
}

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

  vector<long> pow_(Beneath + 1);

  fac.at(0) = 1;
  ifac.at(0) = 1;
  for (long i = 1; i < Beneath + 1; i++) {
    fac.at(i) = fac.at(i - 1) * i % mod;
    ifac.at(i) = ifac.at(i - 1) * mpow(i, mod - 2) % mod;
  }

  pow_.at(0) = 1;
  for (long i = 1; i < Beneath; i++)
    pow_.at(i) = pow_.at(i - 1) * (M - 1) % mod;

  long ans = 0;

  for (long i = 0; i <= K; i++) {
    long A = M;
    A = A * comb(N - 1, i) % mod;
    A = A * pow_.at(N - 1 - i) % mod;
    ans += A;
    ans %= mod;
  }
  cout << ans << endl;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 250001) >= this->size() (which is 250001)


Fixed submission:

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

const long Beneath = 250000;

const long mod = 998244353;

vector<long> fac(Beneath + 1), ifac(Beneath + 1);

long mpow(long x, long n) {
  long ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    n = n >> 1;
  }
  return ans;
}

long comb(long a, long b) {
  if (a == 0 && b == 0)
    return 1;
  else if (a < b || a < 0)
    return 0;
  else {
    long tmp = ifac.at(a - b) * ifac.at(b) % mod;
    return tmp * fac.at(a) % mod;
  }
}

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

  vector<long> pow_(Beneath + 1);

  fac.at(0) = 1;
  ifac.at(0) = 1;
  for (long i = 0; i < Beneath + 1; i++) {
    fac.at(i + 1) = fac.at(i) * (i + 1) % mod;
    ifac.at(i + 1) = ifac.at(i) * mpow(i + 1, mod - 2) % mod;
  }

  pow_.at(0) = 1;
  for (long i = 1; i < Beneath + 1; i++)
    pow_.at(i) = pow_.at(i - 1) * (M - 1) * mod;

  long ans = 0;

  for (long i = 0; i <= K; i++) {
    long A = M;
    A = A * comb(N - 1, i) % mod;
    A = A * pow_.at(N - 1 - i) % mod;
    ans += A;
    ans %= mod;
  }
  cout << ans << endl;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;

const ll M = 998244353;

vector<ll> fac(300001);
vector<ll> ifac(300001);

ll mpow(ll x, ll n) {
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % M;
    x = x * x % M;
    n = n >> 1;
  }
  return ans;
}

ll comb(ll a, ll b) { // aCbをmod計算
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] * ifac[b] % M;
  return tmp * fac[a] % M;
}

vector<ll> pow_(300001);

int main() {
  ll N, X, K;
  cin >> N >> X >> K;

  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 0; i < 300000; i++) {
    fac[i + 1] = fac[i] * (i + 1) % M; // n!(mod M)
    ifac[i + 1] = ifac[i] * mpow(i + 1, M - 2) % M;
  }

  pow_[0] = 1;
  for (ll i = 1; i < 300000; i++)
    pow_[i] = pow_[i - 1] * (X - 1) % M;

  ll ans = 0;
  for (ll i = 0; i <= K; i++) {
    ll Z = X;
    Z *= comb(N - 1, i);
    Z %= M;
    Z *= pow_[N - 1 - i];
    Z %= M;
    ans += Z;
    ans %= M;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;

const ll M = 998244353;

vector<ll> fac(300001);
vector<ll> ifac(300001);

ll mpow(ll x, ll n) {
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % M;
    x = x * x % M;
    n = n >> 1;
  }
  return ans;
}

ll comb(ll a, ll b) { // aCbをmod計算
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] * ifac[b] % M;
  return tmp * fac[a] % M;
}

int main() {
  ll N, X, K;
  cin >> N >> X >> K;

  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 0; i < 300000; i++) {
    fac[i + 1] = fac[i] * (i + 1) % M; // n!(mod M)
    ifac[i + 1] = ifac[i] * mpow(i + 1, M - 2) % M;
  }

  ll ans = 0;
  for (long i = 0; i <= K; i++) {
    long Z = X;
    Z *= comb(N - 1, i);
    Z %= M;
    for (long j = 0; j < N - 1 - i; j++) {
      Z *= X - 1;
      Z %= M;
    }
    ans += Z;
    ans %= M;
  }
  cout << ans << endl;
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

const long long MOD = 998244353;
const long long MAX = 1e6;

long long fact[MAX], fact_inv[MAX];

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

void init_nCr(long long n) {
  fact[0] = 1;
  for (int i = 0; i < n; i++) {
    fact[i + 1] = (fact[i] * (i + 1)) % MOD;
  }
  fact_inv[n] = power(fact[n], MOD - 2);
  for (int i = n - 1; i >= 0; i--) {
    fact_inv[i] = (fact_inv[i + 1] * (i + 1)) % MOD;
  }
}

long long nCr(long long n, long long r) {
  return (((fact[n] * fact_inv[r]) % MOD) * fact_inv[n - r]) % MOD;
}

long long pow(long long n, long long c) {
  long long ret = 1;
  while (n != 0) {
    if (n & 1) {
      ret = ret * c % MOD;
    }
    c = c * c % MOD;
    n /= 2;
  }
  return ret;
}

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

  init_nCr(2 * 1e5);

  long long ans = 0;
  for (int i = 0; i <= K; i++) {
    long long add = (M * pow(N - i - 1, M - 1) % MOD) * nCr(N - 1, i) % MOD;
    ans = (ans + add) % MOD;
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

const long long MOD = 998244353;
const long long MAX = 1e6;

long long fact[MAX], fact_inv[MAX];

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

void init_nCr(long long n) {
  fact[0] = 1;
  for (int i = 0; i < n; i++) {
    fact[i + 1] = (fact[i] * (i + 1)) % MOD;
  }
  fact_inv[n] = power(fact[n], MOD - 2);
  for (int i = n - 1; i >= 0; i--) {
    fact_inv[i] = (fact_inv[i + 1] * (i + 1)) % MOD;
  }
}

long long nCr(long long n, long long r) {
  return (((fact[n] * fact_inv[r]) % MOD) * fact_inv[n - r]) % MOD;
}

long long calc(long long n, long long c) {
  long long ret = c;
  for (long long i = 1; i < n; i++) {
    ret *= c - 1;
    ret %= MOD;
  }
  return ret;
}

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

  init_nCr(2 * 1e5);

  long long ans = 0;
  for (int i = 0; i <= K; i++) {
    long long add = calc(N - i, M) * nCr(N - 1, i);
    ans += add;
    ans %= MOD;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define INCANT                                                                 \\
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(0),                            \\
      cout << fixed << setprecision(20);
#define int long long
#define double long double
const int INF = 1e18, MOD = 998244353;

vector<int> fact;

void modinit(int n) {
  fact.push_back(1);
  int now = 1;
  for (int i = 1; i <= n; i++) {
    now *= i;
    now %= MOD;
    fact.push_back(now);
  }
}

int modpow(int x, int n) {
  return n == 0 ? 1 : modpow(x * x % MOD, n / 2) * (n % 2 ? x : 1) % MOD;
}

int modcomb(int n, int r) {
  if (n < r)
    return 0;
  return fact[n] * modpow(fact[n - r], MOD - 2) % MOD *
         modpow(fact[r], MOD - 2) % MOD;
}

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INCANT                                                                 \\
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(0),                            \\
      cout << fixed << setprecision(20);
#define int long long
#define double long double
const int INF = 1e18, MOD = 998244353;

vector<int> fact;

int modinit(int n) {
  fact.push_back(1);
  int now = 1;
  for (int i = 1; i <= n; i++) {
    now *= i;
    now %= MOD;
    fact.push_back(now);
  }
}

int modpow(int x, int n) {
  return n == 0 ? 1 : modpow(x * x % MOD, n / 2) * (n % 2 ? x : 1) % MOD;
}

int modcomb(int n, int r) {
  if (n < r)
    return 0;
  return fact[n] * modpow(fact[n - r], MOD - 2) % MOD *
         modpow(fact[r], MOD - 2) % MOD;
}

signed main() {
  int n, m, k;
  cin >> n >> m >> k;
  int res = 0;
  modinit(222222);
  for (int i = 0; i <= k; i++) {
    res += m * modcomb(n - 1, i) % MOD * modpow(m - 1, n - i - 1) % MOD;
    res %= MOD;
  }
  cout << res << endl;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const long long MAX = 200005;
const long long M = 998244353;

long long x_y(long long x, long long y) {
  if (y == 0)
    return 1;
  if (y == 1)
    return x;
  if (y % 2 == 0)
    return x_y(x, y / 2) * x_y(x, y / 2) % M;
  if (y % 2 == 1)
    return x * x_y(x, y - 1) % M;
}
long long A(long long n, long long m, long long k) {
  long long ans = m;
  ans *= x_y(m - 1, n - k - 1);
  ans %= M;
  return ans;
}

long long a[MAX], b[MAX];
void kai() {
  a[0] = a[1] = 1;
  b[0] = b[1] = 1;
  for (long long i = 2; i < MAX; i++) {
    a[i] = a[i - 1] * i % M;
    b[i] = x_y(a[i], M - 2);
  }
}
long long nCr(long long n, long long r) {
  return a[n] * b[r] % M * b[n - r] % M;
}

int main() {
  long long n, m, k;
  cin >> n >> m >> k;
  long long sum = 0;
  long long Q = A(n, m, k);
  kai();
  for (long long i = k; i >= 0; i--) {
    long long x = Q * nCr(n - 1, i) % M;
    Q = Q * (m - 1) % M;
    sum += x;
    sum %= M;
  }
  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long M = 998244353;

long long x_y(long long x, long long y) {
  if (y == 0)
    return 1;
  if (y == 1)
    return x;
  if (y % 2 == 0)
    return x_y(x, y / 2) * x_y(x, y / 2) % M;
  if (y % 2 == 1)
    return x * x_y(x, y - 1) % M;
}
long long a(long long n, long long m, long long k) {
  long long ans = m;
  ans *= x_y(m - 1, n - k - 1);
  ans %= M;
  return ans;
}
long long nCr_bunsi(long long n, long long r) {
  if (r == 0)
    return 1;
  return (n - r + 1) * nCr_bunsi(n, r - 1) % M;
}

long long nCr_bunbo(long long r) {
  long long x = nCr_bunsi(r, r);
  return x_y(x, M - 2);
}
long long b(long long n, long long k) {
  return nCr_bunsi(n - 1, k) * nCr_bunbo(k) % M;
}

int main() {
  long long n, m, k;
  cin >> n >> m >> k;
  long long sum = 0;
  long long Q = a(n, m, k);
  for (long long i = k; i >= 0; i--) { // aは計算量抑えるため一回だけ使う
    long long x = Q * b(n, i) % M;
    Q = Q * (m - 1) % M;
    sum += x;
    sum %= M;
  }
  cout << sum << endl;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

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

const int M = 998244353;
const int MAX = 1e5 * 2 + 5;

long long kai[MAX], kair[MAX], rev[MAX];

void com_p() {
  kai[0] = kai[1] = 1;
  kair[0] = kair[1] = 1;
  rev[1] = 1;
  for (int i = 2; i < MAX; i++) {
    kai[i] = kai[i - 1] * i % M;
    rev[i] = M - rev[M % i] * (M / i) % M;
    kair[i] = kair[i - 1] * rev[i] % M;
  }
}
long long ncr(int n, int r) {
  if (n < 0 || r < 0)
    return 0;
  if (n < r)
    return 0;
  return kai[n] * (kair[r] * kair[n - r] % M) % M;
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  com_p();
  long long sum = 0, x = m;
  for (int i = 0; i < n - k - 1; i++) {
    x = x * (m - 1) % M;
  }
  for (int i = k; i >= 0; i--) {
    sum += x * ncr(n - 1, i) % M;
    sum %= M;
    x = x * (m - 1) % M;
  }
  cout << sum << endl;
}


Error: 0

Fixed submission:

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

const int M = 998244353;
const int MAX = 1e5 + 10;

long long kai[MAX], kair[MAX], rev[MAX];

void com_p() {
  kai[0] = kai[1] = 1;
  kair[0] = kair[1] = 1;
  rev[1] = 1;
  for (int i = 2; i < MAX; i++) {
    kai[i] = kai[i - 1] * i % M;
    rev[i] = M - rev[M % i] * (M / i) % M;
    kair[i] = kair[i - 1] * rev[i] % M;
  }
}
long long ncr(int n, int r) {
  if (n < 0 || r < 0)
    return 0;
  if (n < r)
    return 0;
  return kai[n] * (kair[r] * kair[n - r] % M) % M;
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  com_p();
  long long sum = 0, x = m;
  for (int i = 0; i < n - k - 1; i++) {
    x = x * (m - 1) % M;
  }
  for (int i = k; i >= 0; i--) {
    sum += x * ncr(n - 1, i) % M;
    sum %= M;
    x = x * (m - 1) % M;
  }
  cout << sum << endl;
}