Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(c) begin(c), end(c)
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
/*
3 2 0 = 2 //121 212
3 2 1 = 6 // 2*(2+1)
3 2 2 = 8 // 2^3

2通りとれるばしょ
4 2 1だとして
1121
ペアは3か所。そのうち1か所まで
2 *

DP

DP[K]
DP[0] = M;
1回目のループ
DP[0] = DP[0] * (M-1)
DP[1] = DP[0];
DP(NK)になってまう

3 3 0
3 * 2 * 2
=12
3 3 1 = 3^2 3^(N-K)
3 3 2 = 3^1
330+
112
113
221
223
331
332
122
133
211
233
311
322
=9通り
3*3

3 3 2
=3通り

1=0+1
2=0+1+2

3 3 0 = 12
3 3 1 = 12 + 9同じペアをくっつけてカウント？
3 3 2 = 12 + 9 + 3

4 4 0 = 4*3*3*3
4 4 1 = 4 * 3 * 3 * 3C1 = (N-1)CK
4 4 2 = 4 * 3 * 2C2？
1122 1133
1112 1113とか
3通りある。3C2か
1123
1223
1233
1112
1222
1122
1111

4 4 0 = 4*3*3*3* 3C0//1234
4 4 1 = 4*3*3* 3C1//1123 1223 1233
4 4 2 = 4*3 * 3C2//1122 1112 1222
4 4 3 = 4 * 3C3 //1111
M * (M-1)^(N-1-K)*(N-1)CK

3 2 0 = 2 //121 212
2 * 1 * 1 * 2C0
*/

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

ll 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(0);
  COMinit();

  ll N, M, K;
  cin >> N >> M >> K;
  ll ans = 0;
  vector<ll> Z(N + 10, 1);
  rep(i, N) {
    Z[i + 1] = Z[i] * (M - 1);
    Z[i + 1] %= MOD;
  }
  for (ll k = K; k >= 0; k--) {
    // cout << "k;"<<k<<" ans:"<<ans<<endl;
    // M * (M-1)^(N-1-K)*(N-1)CK
    ll tmp = M * Z[N - 1 - k];
    tmp %= MOD;
    tmp *= COM(N - 1, k);
    tmp %= MOD;
    ans += tmp;
    ans %= MOD;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(c) begin(c), end(c)
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
/*
3 2 0 = 2 //121 212
3 2 1 = 6 // 2*(2+1)
3 2 2 = 8 // 2^3

2通りとれるばしょ
4 2 1だとして
1121
ペアは3か所。そのうち1か所まで
2 *

DP

DP[K]
DP[0] = M;
1回目のループ
DP[0] = DP[0] * (M-1)
DP[1] = DP[0];
DP(NK)になってまう

3 3 0
3 * 2 * 2
=12
3 3 1 = 3^2 3^(N-K)
3 3 2 = 3^1
330+
112
113
221
223
331
332
122
133
211
233
311
322
=9通り
3*3

3 3 2
=3通り

1=0+1
2=0+1+2

3 3 0 = 12
3 3 1 = 12 + 9同じペアをくっつけてカウント？
3 3 2 = 12 + 9 + 3

4 4 0 = 4*3*3*3
4 4 1 = 4 * 3 * 3 * 3C1 = (N-1)CK
4 4 2 = 4 * 3 * 2C2？
1122 1133
1112 1113とか
3通りある。3C2か
1123
1223
1233
1112
1222
1122
1111

4 4 0 = 4*3*3*3* 3C0//1234
4 4 1 = 4*3*3* 3C1//1123 1223 1233
4 4 2 = 4*3 * 3C2//1122 1112 1222
4 4 3 = 4 * 3C3 //1111
M * (M-1)^(N-1-K)*(N-1)CK

3 2 0 = 2 //121 212
2 * 1 * 1 * 2C0
*/

const int MAX = 210000;
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;
  }
}

ll 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(0);
  COMinit();

  ll N, M, K;
  cin >> N >> M >> K;
  ll ans = 0;
  vector<ll> Z(N, 1);
  rep(i, N) {
    Z[i + 1] = Z[i] * (M - 1);
    Z[i + 1] %= MOD;
  }
  for (ll k = K; k >= 0; k--) {
    // cout << "k;"<<k<<" ans:"<<ans<<endl;
    // M * (M-1)^(N-1-K)*(N-1)CK
    ll tmp = M * Z[N - 1 - k];
    tmp %= MOD;
    tmp *= COM(N - 1, k);
    tmp %= MOD;
    ans += tmp;
    ans %= MOD;
  }
  cout << ans << endl;
}