prithivMLmods/Coder-Stat huggingface dataset:
Problem p02990 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

long long factorial(long long i) { // 階乗
  if (i > 0)
    return i * factorial(i - 1);
  else
    return 1;
}
//// a^n 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;
}

//// a^(-1) mod
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }

int main() {
  const ll mod = 1000000000 + 7;
  ll N, K;
  cin >> N >> K;

  vector<ll> fact(max(K + 2, N - K) + 3);
  fact[0] = 1;
  rep(i, max(K + 2, N - K) + 3) { fact[i + 1] = fact[i] * (i + 1) % mod; }

  vector<ll> cnt(K);
  ll tmp = 0;

  for (int i = 1; i <= K; i++) {
    // r の位置 * rの個数
    tmp = fact[K - 1] * modinv(fact[i - 1], mod) % mod *
          modinv(fact[K - i], mod) % mod;
    // cout << "1:" << tmp << endl;
    if (N - K - i + 1 < 0) {
      tmp = 0;
    } else
      tmp *= fact[N - K + 1] * modinv(fact[i], mod) % mod *
             modinv(fact[N - K - i + 1], mod) % mod;
    // cout << "2:" << fact[N-K+1] << ' ' << modinv(fact[i],mod) << ' ' <<
    // modinv(fact[N-K-i+1],mod) << endl;
    tmp = tmp % mod;

    cnt[i - 1] = tmp;
  }

  rep(i, K) { cout << cnt[i] << endl; }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

long long factorial(long long i) { // 階乗
  if (i > 0)
    return i * factorial(i - 1);
  else
    return 1;
}
//// a^n 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;
}

//// a^(-1) mod
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }

int main() {
  const ll mod = 1000000000 + 7;
  ll N, K;
  cin >> N >> K;

  vector<ll> fact(max(K + 2, N - K) + 4);
  fact[0] = 1;
  rep(i, max(K + 2, N - K) + 3) { fact[i + 1] = fact[i] * (i + 1) % mod; }

  vector<ll> cnt(K);
  ll tmp = 0;

  for (int i = 1; i <= K; i++) {
    // r の位置 * rの個数
    tmp = fact[K - 1] * modinv(fact[i - 1], mod) % mod *
          modinv(fact[K - i], mod) % mod;
    // cout << "1:" << tmp << endl;
    if (N - K - i + 1 < 0) {
      tmp = 0;
    } else
      tmp *= fact[N - K + 1] * modinv(fact[i], mod) % mod *
             modinv(fact[N - K - i + 1], mod) % mod;
    // cout << "2:" << fact[N-K+1] << ' ' << modinv(fact[i],mod) << ' ' <<
    // modinv(fact[N-K-i+1],mod) << endl;
    tmp = tmp % mod;

    cnt[i - 1] = tmp;
  }

  rep(i, K) { cout << cnt[i] << endl; }

  return 0;
}
Problem p02990 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
const int N = 2002, mod = 1e9 + 7;
int fact[N * 2];

int fastpower(int b, int p) {
  int res = 1;
  int v = b;
  while (p) {
    if (p & 1)
      res = (1ll * res * v) % mod;
    p >>= 1;
    v = (1ll * v * v) % mod;
  }
  return res;
}

void pre() {
  fact[0] = fact[1] = 1;
  for (int i = 2; i < N * 2; ++i) {
    fact[i] = (1ll * i * fact[i - 1]) % mod;
  }
}

int ncr(int n, int r) {
  if (n == r || r == 0)
    return 1;
  int res = fact[n];
  res = (1ll * res * fastpower(fact[r], mod - 2)) % mod;
  res = (1ll * res * fastpower(fact[n - r], mod - 2)) % mod;
  return res;
}

int main() {
  // freopen("in.txt", "rt", stdin);
  pre();
  int n, k;
  scanf("%d %d", &n, &k);
  long long v;
  for (int i = 1; i <= k; ++i) {
    int l = k - i;
    v = ncr(l + i - 1, i - 1);
    v = (1ll * v * ncr(n - k + i - (i - 1), i)) % mod;
    printf("%lld\n", v);
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
const int N = 2002, mod = 1e9 + 7;
int fact[N * 2];

int fastpower(int b, int p) {
  int res = 1;
  int v = b;
  while (p) {
    if (p & 1)
      res = (1ll * res * v) % mod;
    p >>= 1;
    v = (1ll * v * v) % mod;
  }
  return res;
}

void pre() {
  fact[0] = fact[1] = 1;
  for (int i = 2; i < N * 2; ++i) {
    fact[i] = (1ll * i * fact[i - 1]) % mod;
  }
}

int ncr(int n, int r) {
  if (r > n || n < 0 || r < 0)
    return 0;
  if (n == r || r == 0)
    return 1;
  int res = fact[n];
  res = (1ll * res * fastpower(fact[r], mod - 2)) % mod;
  res = (1ll * res * fastpower(fact[n - r], mod - 2)) % mod;
  return res;
}

int main() {
  // freopen("in.txt", "rt", stdin);
  pre();
  int n, k;
  scanf("%d %d", &n, &k);
  long long v;
  for (int i = 1; i <= k; ++i) {
    int l = k - i;
    v = ncr(l + i - 1, i - 1);
    v = (1ll * v * ncr(n - k + i - (i - 1), i)) % mod;
    printf("%lld\n", v);
  }
}
Problem p02990 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

const int mod = 1000000007;

long long power(long long b, int p) {
  long long ans = 1;
  while (p) {
    if (p % 2)
      ans = (ans * b) % mod;
    b = (b * b) % mod;
    p /= 2;
  }
  return ans;
}

const int size = 20005;

int fact[size]{1, 1};

int df[size]{1, 1};

void Compute() {
  for (int i = 2; i < size; ++i) {
    fact[i] = (fact[i - 1] * static_cast<long long>(i)) % mod;
    df[i] = power(fact[i], mod - 2);
  }
}

long long ncr(int n, int r) {
  return (((static_cast<long long>(fact[n]) * df[n - r]) % mod) * df[r]) % mod;
}

int main() {
  Compute();
  int n, k;
  std::cin >> n >> k;
  for (int i = 1; i <= k; ++i)
    std::cout << (ncr(k - 1, i - 1) * ncr(n - k + 1, i)) % mod << '\n';
  return 0;
}

Fixed submission:
#include <iostream>

const int mod = 1000000007;

long long power(long long b, int p) {
  long long ans = 1;
  while (p) {
    if (p % 2)
      ans = (ans * b) % mod;
    b = (b * b) % mod;
    p /= 2;
  }
  return ans;
}

const int size = 20005;

int fact[size]{1, 1};

int df[size]{1, 1};

void Compute() {
  for (int i = 2; i < size; ++i) {
    fact[i] = (fact[i - 1] * static_cast<long long>(i)) % mod;
    df[i] = power(fact[i], mod - 2);
  }
}

long long ncr(int n, int r) {
  if (r > n)
    return 0;
  return (((static_cast<long long>(fact[n]) * df[n - r]) % mod) * df[r]) % mod;
}

int main() {
  Compute();
  int n, k;
  std::cin >> n >> k;
  for (int i = 1; i <= k; ++i)
    std::cout << (ncr(k - 1, i - 1) * ncr(n - k + 1, i)) % mod << '\n';
  return 0;
}
Problem p02990 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
void comb(vector<vector<long long>> &v) {
  for (int i = 0; i < v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (int k = 1; k < v.size(); k++) {
    for (int j = 1; j < k; j++) {
      v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]) % mod;
    }
  }
}

// int main(){
//   int N,K;
//   cin >> N >>K;
//   vector<vector<long long int> > v(N+1,vector<long long int>(N+1,0));
//   comb(v);
//   cout << v[N][K];
//   return 0;
// }

int main() {
  int N, K;
  cin >> N >> K;
  vector<vector<long long>> v(2000, vector<long long>(2000, 0));
  vector<int> ans(K);
  int left = N - K + 1, right;
  comb(v);

  for (int i = 1; i < K + 1; i++) {
    ans[i] = (v[left][i] * v[K - 1][i - 1]) % mod;
  }

  for (int i = 1; i < K + 1; i++) {
    cout << ans[i] << endl;
  }

  //     cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
void comb(vector<vector<long long>> &v) {
  for (int i = 0; i < v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (int k = 1; k < v.size(); k++) {
    for (int j = 1; j < k; j++) {
      v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]) % mod;
    }
  }
}

// int main(){
//   int N,K;
//   cin >> N >>K;
//   vector<vector<long long int> > v(N+1,vector<long long int>(N+1,0));
//   comb(v);
//   cout << v[N][K];
//   return 0;
// }

int main() {
  int N, K;
  cin >> N >> K;
  vector<vector<long long>> v(2000, vector<long long>(2000, 0));
  vector<int> ans(4000);
  int left = N - K + 1, right;
  comb(v);

  for (int i = 1; i < K + 1; i++) {
    ans[i] = (v[left][i] * v[K - 1][i - 1]) % mod;
  }

  for (int i = 1; i < K + 1; i++) {
    cout << ans[i] << endl;
  }

  //     cout << ans << endl;
}
Problem p02990 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
typedef long long ll;
using namespace std;

ll modfact[2001];

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

ll modinv(ll n) { return modpow(modfact[n], MOD - 2); }

ll nCr(ll n, ll r) {
  return (modfact[n] * modinv(n - r) % MOD) * modinv(r) % MOD;
}

int main() {
  int n, k;
  cin >> n >> k;
  modfact[0] = 1;
  for (int i = 1; i <= 2000; i++) {
    modfact[i] = modfact[i - 1] * i % MOD;
  }
  for (int i = 1; i <= k; i++) {
    // if (n-k < i-1) cout << 0 << endl;
    cout << nCr(n - k + 1, i) * nCr(k - 1, i - 1) % MOD << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
typedef long long ll;
using namespace std;

ll modfact[2001];

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

ll modinv(ll n) { return modpow(modfact[n], MOD - 2); }

ll nCr(ll n, ll r) {
  return (modfact[n] * modinv(n - r) % MOD) * modinv(r) % MOD;
}

int main() {
  int n, k;
  cin >> n >> k;
  modfact[0] = 1;
  for (int i = 1; i <= 2000; i++) {
    modfact[i] = modfact[i - 1] * i % MOD;
  }
  for (int i = 1; i <= k; i++) {
    if (n - k < i - 1)
      cout << 0 << endl;
    else
      cout << nCr(n - k + 1, i) * nCr(k - 1, i - 1) % MOD << endl;
  }
  return 0;
}
Problem p02990 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const int N = 5005, M = 1e9 + 7;
ll fact[N];
ll mul(ll a, ll b) {
  a %= M;
  b %= M;
  return (a * b) % M;
}
void pre() {
  fact[0] = 1;
  for (int i = 1; i < N; ++i)
    fact[i] = mul(fact[i - 1], i);
}
ll fp(ll b, ll p) {
  if (!p)
    return 1;
  ll res = fp(b, p >> 1);
  res = mul(res, res);
  if (p & 1)
    res = mul(res, b);
  return res;
}
ll inv(ll n) { return fp(n, M - 2); }
ll ncr(ll n, ll r) { return mul(fact[n], mul(inv(fact[r]), inv(fact[n - r]))); }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  pre();
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n - k + 1; ++i)
    cout << mul(fp(i, k - i), ncr(n - k + 1, i)) << '\n';
  for (int i = n - k + 2; i <= k; ++i)
    cout << "0\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const int N = 5005, M = 1e9 + 7;
ll fact[N];
ll mul(ll a, ll b) {
  a %= M;
  b %= M;
  return (a * b) % M;
}
void pre() {
  fact[0] = 1;
  for (int i = 1; i < N; ++i)
    fact[i] = mul(fact[i - 1], i);
}
ll fp(ll b, ll p) {
  if (!p)
    return 1;
  ll res = fp(b, p >> 1);
  res = mul(res, res);
  if (p & 1)
    res = mul(res, b);
  return res;
}
ll inv(ll n) { return fp(n, M - 2); }
ll ncr(ll n, ll r) { return mul(fact[n], mul(inv(fact[r]), inv(fact[n - r]))); }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  pre();
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; ++i) {
    if (i <= n - k + 1)
      cout << mul(ncr(k - 1, i - 1), ncr(n - k + 1, i));
    else
      cout << 0;
    cout << '\n';
  }
  return 0;
}
Problem p02990 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define reg register
#define debug(x) cerr << #x << " = " << x << endl;
#define rep(a, b, c) for (reg int a = (b), a##_end_ = (c); a <= a##_end_; ++a)
#define ret(a, b, c) for (reg int a = (b), a##_end_ = (c); a < a##_end_; ++a)
#define drep(a, b, c) for (reg int a = (b), a##_end_ = (c); a >= a##_end_; --a)

inline int Read() {
  int res = 0, f = 1;
  char c;
  while (c = getchar(), c < 48 || c > 57)
    if (c == '-')
      f = 0;
  do
    res = (res << 3) + (res << 1) + (c ^ 48);
  while (c = getchar(), c >= 48 && c <= 57);
  return f ? res : -res;
}

template <class T> inline bool Min(T &a, T const &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T> inline bool Max(T &a, T const &b) {
  return a < b ? a = b, 1 : 0;
}

const int N = 2e3 + 5, mod = 1e9 + 7ll;

int n, k, m, C[N][N];

signed main() {
  n = Read(), k = Read(), m = n - k;
  C[0][0] = 1ll;
  rep(i, 1, n) {
    C[i][0] = 1ll;
    rep(j, 1, i) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
  } // debug(C[7][7]);
  printf("%lld\n", (C[m - 1][1] + 2ll) * 1 % mod);
  rep(i, 2, k) {
    int N1 = C[m - 1][i], N2 = (2 * C[m - 1][i - 1]) % mod,
        N3 = C[m - 1][i - 2], K1 = C[k - 1][i - 1];
    int Ans = ((N1 + N2 + N3) % mod * K1) % mod;
    Ans = (Ans + mod) % mod;
    printf("%lld\n", Ans);
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define reg register
#define debug(x) cerr << #x << " = " << x << endl;
#define rep(a, b, c) for (reg int a = (b), a##_end_ = (c); a <= a##_end_; ++a)
#define ret(a, b, c) for (reg int a = (b), a##_end_ = (c); a < a##_end_; ++a)
#define drep(a, b, c) for (reg int a = (b), a##_end_ = (c); a >= a##_end_; --a)

inline int Read() {
  int res = 0, f = 1;
  char c;
  while (c = getchar(), c < 48 || c > 57)
    if (c == '-')
      f = 0;
  do
    res = (res << 3) + (res << 1) + (c ^ 48);
  while (c = getchar(), c >= 48 && c <= 57);
  return f ? res : -res;
}

template <class T> inline bool Min(T &a, T const &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T> inline bool Max(T &a, T const &b) {
  return a < b ? a = b, 1 : 0;
}

const int N = 2e3 + 5, mod = 1e9 + 7ll;

int n, k, m, C[N][N];

signed main() {
  n = Read(), k = Read(), m = n - k;
  C[0][0] = 1ll;
  rep(i, 1, n) {
    C[i][0] = 1ll;
    rep(j, 1, i) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
  } // debug(C[7][7]);
  if (n == k) {
    puts("1");
    rep(i, 2, n) printf("%d\n", 0);
    return 0;
  }
  printf("%lld\n", (C[m - 1][1] + 2ll) * 1 % mod);
  rep(i, 2, k) {
    int N1 = C[m - 1][i], N2 = (2 * C[m - 1][i - 1]) % mod,
        N3 = C[m - 1][i - 2], K1 = C[k - 1][i - 1];
    int Ans = ((N1 + N2 + N3) % mod * K1) % mod;
    Ans = (Ans + mod) % mod;
    printf("%lld\n", Ans);
  }
}
Problem p02990 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int N = 100 * 1000 + 17;
const int MOD = 1000 * 1000 * 1000 + 7;

int n, k;
long long F[N];

bool read() {
  if (!(cin >> n >> k))
    return false;

  return true;
}

long long binpow(long long a, int n) {
  auto res = 1ll;

  for (; n > 0; n /= 2, a = a * a % MOD)
    if (n & 1)
      res = res * a % MOD;

  return res;
}

long long rev(long long x) { return binpow(x, MOD - 2); }

long long binomial(int n, int k) { return F[n] * rev(F[k] * F[n - k]) % MOD; }

void solve() {
  F[0] = 1;

  for (int i = 1; i <= n; ++i)
    F[i] = i * F[i - 1] % MOD;

  for (int i = 1; i <= k; ++i)
    cout << binomial(k - 1, i - 1) * binomial(n - k + 1, i) % MOD << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  while (read())
    solve();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int N = 100 * 1000 + 17;
const int MOD = 1000 * 1000 * 1000 + 7;

int n, k;
long long F[N];

bool read() {
  if (!(cin >> n >> k))
    return false;

  return true;
}

long long binpow(long long a, int n) {
  auto res = 1ll;

  for (; n > 0; n /= 2, a = a * a % MOD)
    if (n & 1)
      res = res * a % MOD;

  return res;
}

long long rev(long long x) { return binpow(x, MOD - 2); }

long long binomial(int n, int k) {
  return n < k ? 0ll : F[n] * rev(F[k] * F[n - k] % MOD) % MOD;
}

void solve() {
  F[0] = 1;

  for (int i = 1; i <= n; ++i)
    F[i] = i * F[i - 1] % MOD;

  for (int i = 1; i <= k; ++i)
    cout << binomial(k - 1, i - 1) * binomial(n - k + 1, i) % MOD << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  while (read())
    solve();

  return 0;
}
Problem p02990 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <chrono>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

const int MAX = 2001; // change here as problems
const int MOD = 1000000007;

int64_t fac[MAX], inv[MAX], finv[MAX]; // arrays for factorial, inverse (mod p),
                                       // and inverse of factorial, respectively

void table() {
  fac[0] = fac[1] = 1;
  inv[1] = 1;
  finv[0] = finv[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 comb(int n, int k) {
  return fac[n] * finv[k] % MOD * finv[n - k] % MOD;
}

int main(int argc, const char *argv[]) {

  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k;
  cin >> n >> k;

  table();

  for (int i = 1; i <= k; i++) {
    cout << comb(n - k + 1, i) * comb(k - 1, k - i) % MOD << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <chrono>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

const int MAX = 2001; // change here as problems
const int MOD = 1000000007;

int64_t fac[MAX], inv[MAX], finv[MAX]; // arrays for factorial, inverse (mod p),
                                       // and inverse of factorial, respectively

void table() {
  fac[0] = fac[1] = 1;
  inv[1] = 1;
  finv[0] = finv[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 comb(int n, int k) {
  return fac[n] * finv[k] % MOD * finv[n - k] % MOD;
}

int main(int argc, const char *argv[]) {

  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k;
  cin >> n >> k;

  table();

  for (int i = 1; i <= k; i++) {
    if (k - i >= 0 && n - k + 1 >= i)
      cout << comb(n - k + 1, i) * comb(k - 1, k - i) % MOD << endl;
    else
      cout << 0 << endl;
  }

  return 0;
}
Problem p02990 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int mod = 1e9 + 7;

vector<vector<int>> dp(2009, vector<int>(2009, -1));

int comb(int k, int n) {
  if (k > n)
    return 0;
  if (k == 0 || (n == k))
    return 1;
  if (dp[k][n] != -1)
    return dp[k][n];
  return dp[k][n] = (comb(k - 1, n - 1) + comb(k, n - 1)) % mod;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k;
  cin >> n >> k;

  for (int i = 1; i <= k; i++) {
    cout << comb(i - 1, k - 1) * comb(n - k - i + 1, n - k + 1) % mod << endl;
  }
}


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

#define int long long

const int mod = 1e9 + 7;

vector<vector<int>> dp(2009, vector<int>(2009, -1));

int comb(int k, int n) {
  if (k < 0 || n < 0 || k > n)
    return 0;
  if (k == 0 || (n == k))
    return 1;
  if (dp[k][n] != -1)
    return dp[k][n];
  return dp[k][n] = (comb(k - 1, n - 1) + comb(k, n - 1)) % mod;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k;
  cin >> n >> k;

  for (int i = 1; i <= k; i++) {
    cout << comb(i - 1, k - 1) * comb(n - k - i + 1, n - k + 1) % mod << endl;
  }
}
Problem p02990 (C++)

Buggy submission (status: Runtime Error):
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int acs(const void *a, const void *b) {
  return *(int *)a - *(int *)b;
} /* 1,2,3,4.. */
int des(const void *a, const void *b) {
  return *(int *)b - *(int *)a;
} /* 8,7,6,5.. */
int cmp_char(const void *a, const void *b) {
  return *(char *)a - *(char *)b;
} /* a,b,c,d.. */
int cmp_str(const void *a, const void *b) {
  return strcmp(*(const char **)a, *(const char **)b);
} /* aaa,aab.. */
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
#define rep(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007

int main(void) {
  int n, k;
  scanf("%d %d", &n, &k);

  long long int ans;
  int i, j;
  long long int combi[2001][2001];
  for (i = 0; i <= 2001; i++) {
    combi[i][0] = 1;
    for (j = 1; j < i; j++) {
      combi[i][j] = (combi[i - 1][j - 1] + combi[i - 1][j]) % mod;
    }
    combi[i][i] = 1;
  }

  for (i = 1; i <= k; i++) {
    if (n - k + 1 < i)
      ans = 0;
    else
      ans = (combi[k - 1][i - 1] * combi[n - k + 1][i]) % mod;
    printf("%lld\n", ans);
  }
  return 0;
}


Fixed submission:
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int acs(const void *a, const void *b) {
  return *(int *)a - *(int *)b;
} /* 1,2,3,4.. */
int des(const void *a, const void *b) {
  return *(int *)b - *(int *)a;
} /* 8,7,6,5.. */
int cmp_char(const void *a, const void *b) {
  return *(char *)a - *(char *)b;
} /* a,b,c,d.. */
int cmp_str(const void *a, const void *b) {
  return strcmp(*(const char **)a, *(const char **)b);
} /* aaa,aab.. */
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
#define rep(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007

int main(void) {
  int n, k;
  scanf("%d %d", &n, &k);

  long long int ans;
  int i, j;
  long long int combi[2001][2001];
  for (i = 0; i <= n; i++) {
    combi[i][0] = 1;
    for (j = 1; j < i; j++) {
      combi[i][j] = (combi[i - 1][j - 1] + combi[i - 1][j]) % mod;
    }
    combi[i][i] = 1;
  }

  for (i = 1; i <= k; i++) {
    if (n - k + 1 < i)
      ans = 0;
    else
      ans = (combi[k - 1][i - 1] * combi[n - k + 1][i]) % mod;
    printf("%lld\n", ans);
  }
  return 0;
}
Problem p02990 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

ll com[2005][2005];

void solve(ll n, ll k) {
  ll i, j;
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= min(i, k); j++) {
      if (j == 0 || j == i)
        com[i][j] = 1;
      else
        com[i][j] = (com[i - 1][j - 1] + com[i - 1][j]) % mod;
    }
  }
}

// D
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n;
  cin >> n;
  ll k, i;
  cin >> k;
  solve(n, k);
  for (i = 1; i <= k; i++) {
    cout << (com[k - 1][i - 1] * com[n - k + 1][i]) % mod << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

ll com[2005][2005];

void solve(ll n, ll k) {
  ll i, j;
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= min(i, k); j++) {
      if (j == 0 || j == i)
        com[i][j] = 1;
      else
        com[i][j] = (com[i - 1][j - 1] + com[i - 1][j]) % mod;
    }
  }
}

// D
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n;
  cin >> n;
  ll k, i;
  cin >> k;
  solve(n, k);
  for (i = 1; i <= k; i++) {
    cout << (com[k - 1][i - 1] * com[n - k + 1][i]) % mod << endl;
  }
}
Problem p02990 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>

using namespace std;

#define REP(a, i, n) for (int i = a; i < n; ++i)

const int limitation = 2005;
long C[limitation][limitation]; // C[n][k] = n! / (k! * (n - k)!)

long calc(int i, int j) {
  if (C[i][j] != 0)
    return C[i][j];

  if (i == j) {
    C[i][j] = 1;
    return 1;
  } else if (j == 0) {
    C[i][j] = 1;
    return 1;
  } else {
    C[i][j] = (calc(i - 1, j - 1) + calc(i - 1, j)) % 1000000007;
    return C[i][j];
  }
}

int main() {
  // 1 <= K <= N <= 2000
  int N; // ボール数
  int K; // 青いボール数
  cin >> N >> K;

  REP(0, i, limitation) REP(0, j, limitation) C[i][j] = 0;

  REP(1, i, K + 1) {
    cout << (calc(N - K + 1, i) * calc(K - 1, i - 1)) % 1000000007 << endl;
  }

  return 0;
}


Fixed submission:
#include <iostream>
#include <stdio.h>

using namespace std;

#define REP(a, i, n) for (int i = a; i < n; ++i)

const int limitation = 2005;
long C[limitation][limitation]; // C[n][k] = n! / (k! * (n - k)!)

long calc(int i, int j) {
  if (C[i][j] != 0)
    return C[i][j];

  if (i < j) {
    return 0;
  } else if (i == j) {
    C[i][j] = 1;
    return 1;
  } else if (j == 0) {
    C[i][j] = 1;
    return 1;
  } else {
    C[i][j] = (calc(i - 1, j - 1) + calc(i - 1, j)) % 1000000007;
    return C[i][j];
  }
}

int main() {
  // 1 <= K <= N <= 2000
  int N; // ボール数
  int K; // 青いボール数
  cin >> N >> K;

  REP(0, i, limitation) REP(0, j, limitation) C[i][j] = 0;

  REP(1, i, K + 1) {
    cout << (calc(N - K + 1, i) * calc(K - 1, i - 1)) % 1000000007 << endl;
  }

  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define modulo 100000
#define mod(mod_x) ((((long long)mod_x) + modulo) % modulo)
#define Inf 1000000000

int dijkstra(vector<vector<pair<int, int>>> &E, int start, int goal) {
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      P;
  vector<int> dis(100, Inf);
  dis[start] = 0;
  for (int i = 0; i < E[start].size(); i++) {
    P.push(make_pair(E[start][i].first, make_pair(start, E[start][i].second)));
  }

  while (P.size() != 0) {
    pair<int, pair<int, int>> X;
    X = P.top();
    P.pop();
    int cost = X.first;
    int from = X.second.first;
    int to = X.second.second;
    if (dis[to] == Inf) {
      dis[to] = cost;
      for (int i = 0; i < E[to].size(); i++) {
        P.push(make_pair(E[to][i].first + dis[to],
                         make_pair(to, E[to][i].second)));
      }
    }
  }

  return dis[goal];
}

int main() {

  int N, M;
  cin >> N >> M;

  vector<vector<pair<int, int>>> E(3 * N, vector<pair<int, int>>(0));

  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].push_back(make_pair(1, b + N));
    E[a + N].push_back(make_pair(1, b + 2 * N));
    E[a + 2 * N].push_back(make_pair(1, b));
  }

  int S, T;
  cin >> S >> T;
  S--;
  T--;

  int a = dijkstra(E, S, T);
  if (a == Inf) {
    cout << -1 << endl;
  } else {
    cout << a / 3 << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define modulo 100000
#define mod(mod_x) ((((long long)mod_x) + modulo) % modulo)
#define Inf 1000000000

int dijkstra(vector<vector<pair<int, int>>> &E, int start, int goal) {
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      P;
  vector<int> dis(E.size(), Inf);
  dis[start] = 0;
  for (int i = 0; i < E[start].size(); i++) {
    P.push(make_pair(E[start][i].first, make_pair(start, E[start][i].second)));
  }

  while (P.size() != 0) {
    pair<int, pair<int, int>> X;
    X = P.top();
    P.pop();
    int cost = X.first;
    int from = X.second.first;
    int to = X.second.second;
    if (dis[to] == Inf) {
      dis[to] = cost;
      for (int i = 0; i < E[to].size(); i++) {
        P.push(make_pair(E[to][i].first + dis[to],
                         make_pair(to, E[to][i].second)));
      }
    }
  }

  return dis[goal];
}

int main() {

  int N, M;
  cin >> N >> M;

  vector<vector<pair<int, int>>> E(3 * N, vector<pair<int, int>>(0));

  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].push_back(make_pair(1, b + N));
    E[a + N].push_back(make_pair(1, b + 2 * N));
    E[a + 2 * N].push_back(make_pair(1, b));
  }

  int S, T;
  cin >> S >> T;
  S--;
  T--;

  int a = dijkstra(E, S, T);
  if (a == Inf) {
    cout << -1 << endl;
  } else {
    cout << a / 3 << endl;
  }

  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

struct Path {
  int node, times;
  Path(int n, int t) {
    node = n;
    times = t;
  }
};

struct State {
  int node, times, depth;
  State(int n, int t, int d) {
    node = n;
    times = t;
    depth = d;
  }
};

int main(void) {
  cout << std::fixed << std::setprecision(10);
  cin.tie(0);
  std::ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  std::vector<Path> node[100001][3];
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    node[u][0].push_back(Path(v, 1));
    node[u][1].push_back(Path(v, 2));
    node[u][2].push_back(Path(v, 0));
  }
  int s, t;
  cin >> s >> t;

  bool visited[100001][3] = {};
  visited[s][0] = true;
  std::queue<State> bfs;
  bfs.push(State(s, 0, 0));
  while (!bfs.empty()) {
    State curr = bfs.front();
    bfs.pop();

    if (visited[t][0]) {
      assert(curr.depth % 3 == 0);
      cout << curr.depth / 3 << endl;
      return 0;
    }
    for (auto x : node[curr.node][curr.times]) {
      if (!visited[x.node][x.times]) {
        visited[x.node][x.times] = true;
        bfs.push(State(x.node, x.times, curr.depth + 1));
      }
    }
  }

  cout << -1 << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

struct Path {
  int node, times;
  Path(int n, int t) {
    node = n;
    times = t;
  }
};

struct State {
  int node, times, depth;
  State(int n, int t, int d) {
    node = n;
    times = t;
    depth = d;
  }
};

int main(void) {
  cout << std::fixed << std::setprecision(10);
  cin.tie(0);
  std::ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  std::vector<Path> node[100001][3];
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    node[u][0].push_back(Path(v, 1));
    node[u][1].push_back(Path(v, 2));
    node[u][2].push_back(Path(v, 0));
  }
  int s, t;
  cin >> s >> t;

  bool visited[100001][3] = {};
  visited[s][0] = true;
  std::queue<State> bfs;
  bfs.push(State(s, 0, 0));
  while (!bfs.empty()) {
    State curr = bfs.front();
    bfs.pop();

    if (curr.node == t && curr.times == 0) {
      assert(curr.depth % 3 == 0);
      cout << curr.depth / 3 << endl;
      return 0;
    }
    for (auto x : node[curr.node][curr.times]) {
      if (!visited[x.node][x.times]) {
        visited[x.node][x.times] = true;
        bfs.push(State(x.node, x.times, curr.depth + 1));
      }
    }
  }

  cout << -1 << endl;

  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()

const int INF = 1001001001;
int N, M, S, T;
int dist[10005][3];

signed main() {
  cin >> N >> M;
  vector<vector<int>> edge(N);
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    edge[x].push_back(y);
  }
  cin >> S >> T;
  --S;
  --T;

  rep(i, N) rep(j, 3) dist[i][j] = INF;
  queue<pair<int, int>> Q;
  Q.push(make_pair(S, 0));
  dist[S][0] = 0;

  while (!Q.empty()) {
    int v = Q.front().first;
    int len = Q.front().second;
    Q.pop();
    int nlen = (len + 1) % 3;
    for (int u : edge[v]) {
      if (dist[u][nlen] != INF)
        continue;
      dist[u][nlen] = dist[v][len] + 1;
      Q.push(make_pair(u, nlen));
    }
  }

  int ans = dist[T][0];
  if (ans == INF)
    ans = -3;
  cout << ans / 3 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()

const int INF = 1001001001;
int N, M, S, T;
int dist[100005][3];

signed main() {
  cin >> N >> M;
  vector<vector<int>> edge(N);
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    edge[x].push_back(y);
  }
  cin >> S >> T;
  --S;
  --T;

  rep(i, N) rep(j, 3) dist[i][j] = INF;
  queue<pair<int, int>> Q;
  Q.push(make_pair(S, 0));
  dist[S][0] = 0;

  while (!Q.empty()) {
    int v = Q.front().first;
    int len = Q.front().second;
    Q.pop();
    int nlen = (len + 1) % 3;
    for (int u : edge[v]) {
      if (dist[u][nlen] != INF)
        continue;
      dist[u][nlen] = dist[v][len] + 1;
      Q.push(make_pair(u, nlen));
    }
  }

  int ans = dist[T][0];
  if (ans == INF)
    ans = -3;
  cout << ans / 3 << endl;
}
Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
constexpr int INF = 1e9;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N);
  for (int i = 0; i < M; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    G[u].push_back(v);
  }
  int s, t;
  cin >> s >> t;
  --s, --t;
  vector<vector<int>> D(N, vector<int>(3, INF));
  D[s][0] = 0;
  queue<pair<int, int>> Q;
  Q.emplace(s, 0);
  while (Q.size()) {
    int v, d;
    tie(v, d) = Q.front();
    Q.pop();
    int c = D[v][d] + (d == 0);
    int d_ = (d + 1) % 3;
    for (auto v_ : G[v]) {
      if (D[v_][d_] < c)
        continue;
      D[v_][d_] = c;
      Q.emplace(v_, d_);
    }
  }
  if (D[t][0] >= INF) {
    cout << -1 << endl;
    return 0;
  }
  cout << D[t][0] << endl;
}


Fixed submission:
#include <iostream>
#include <queue>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
constexpr int INF = 1e9;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N);
  for (int i = 0; i < M; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    G[u].push_back(v);
  }
  int s, t;
  cin >> s >> t;
  --s, --t;
  vector<vector<int>> D(N, vector<int>(3, INF));
  D[s][0] = 0;
  queue<pair<int, int>> Q;
  Q.emplace(s, 0);
  while (Q.size()) {
    int v, d;
    tie(v, d) = Q.front();
    Q.pop();
    int c = D[v][d] + (d == 0);
    int d_ = (d + 1) % 3;
    for (auto v_ : G[v]) {
      if (D[v_][d_] <= c)
        continue;
      D[v_][d_] = c;
      Q.emplace(v_, d_);
    }
  }
  if (D[t][0] >= INF) {
    cout << -1 << endl;
    return 0;
  }
  cout << D[t][0] << endl;
}
Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
// const bool debug=true;
const bool debug = false;
#define DEBUG if (debug == true)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;

int main(void) {
  int n, m;
  cin >> n >> m;
  int ne = 3 * n;
  vector<int> edges[ne + 1];
  int s, t;
  rep(i, m) {
    int u, v;
    cin >> u >> v;
    rep(i, 3) { edges[u + i * n].push_back(v + ((i + 1) % 3) * n); }
  }
  cin >> s >> t;
  int d[ne + 1];
  rep(i, ne + 1) {
    if (i == s)
      d[i] = 0;
    else
      d[i] = 1LL << 30;
  }
  int value = s;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  do {
    if (!que.empty())
      que.pop();
    DEBUG { cout << "value " << value << endl; }
    for (auto x : edges[value]) {
      if (d[value] + 1 <= d[x]) {
        d[x] = d[value] + 1;
        que.push(make_pair(d[value] + 1, x));
      }
    }
    if (!que.empty()) {
      DEBUG { cout << "top " << que.top().second << endl; }
      value = que.top().second;
    }
  } while (!que.empty());
  cout << ((d[t] == 1LL << 30) ? -1 : d[t] / 3) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
// const bool debug=true;
const bool debug = false;
#define DEBUG if (debug == true)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;

int main(void) {
  int n, m;
  cin >> n >> m;
  int ne = 3 * n;
  vector<int> edges[ne + 1];
  int s, t;
  rep(i, m) {
    int u, v;
    cin >> u >> v;
    rep(i, 3) { edges[u + i * n].push_back(v + ((i + 1) % 3) * n); }
  }
  cin >> s >> t;
  int d[ne + 1];
  rep(i, ne + 1) {
    if (i == s)
      d[i] = 0;
    else
      d[i] = 1LL << 30;
  }
  int value = s;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  do {
    if (!que.empty())
      que.pop();
    DEBUG { cout << "value " << value << endl; }
    for (auto x : edges[value]) {
      if (d[value] + 1 < d[x]) {
        d[x] = d[value] + 1;
        que.push(make_pair(d[value] + 1, x));
      }
    }
    if (!que.empty()) {
      DEBUG { cout << "top " << que.top().second << endl; }
      value = que.top().second;
    }
  } while (!que.empty());
  cout << ((d[t] == 1LL << 30) ? -1 : d[t] / 3) << endl;
  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define rep(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int inf = 1LL << 60;
const int mod = 1e9 + 7;
const double eps = 1e-9;

/*{
}*/

int n, m;
int s, t;
vi g[100000];

signed main() {
  cin >> n >> m;
  rep(i, 0, m) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].pb(v);
  }
  cin >> s >> t;
  s--;
  t--;

  priority_queue<pair<int, pii>> que;
  vvi d(3, vi(n, inf));
  d[0][s] = 0;
  que.push(pair<int, pii>(0, pii(0, s)));
  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    int u = p.second.first;
    int v = p.second.second;
    if (p.first > d[u][v])
      continue;
    //    cout << u << ", " << v << endl;

    for (auto to : g[v]) {
      if (d[(u + 1) % 3][to] > d[u][v] + 1) {
        d[(u + 1) % 3][to] = d[u][v] + 1;
        que.push(pair<int, pii>(d[(u + 1) % 3][to], pii((u + 1) % 3, to)));
      }
    }
  }

  if (d[0][t] == inf)
    cout << -1 << endl;
  else
    cout << d[0][t] / 3 << endl;

  return 0;
}


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

#define int long long
#define rep(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int inf = 1LL << 60;
const int mod = 1e9 + 7;
const double eps = 1e-9;

/*{
}*/

int n, m;
int s, t;
vi g[100000];

signed main() {
  cin >> n >> m;
  rep(i, 0, m) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].pb(v);
  }
  cin >> s >> t;
  s--;
  t--;

  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      que;
  vvi d(3, vi(n, inf));
  d[0][s] = 0;
  que.push(pair<int, pii>(0, pii(0, s)));
  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    int u = p.second.first;
    int v = p.second.second;
    if (p.first > d[u][v])
      continue;
    //    cout << u << ", " << v << endl;

    for (auto to : g[v]) {
      if (d[(u + 1) % 3][to] > d[u][v] + 1) {
        d[(u + 1) % 3][to] = d[u][v] + 1;
        que.push(pair<int, pii>(d[(u + 1) % 3][to], pii((u + 1) % 3, to)));
      }
    }
  }

  if (d[0][t] == inf)
    cout << -1 << endl;
  else
    cout << d[0][t] / 3 << endl;

  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef priority_queue<int> HEAP;
typedef priority_queue<int, vector<int>, greater<int>> RHEAP;

const int N = 100010, M = 3 * N;

int n, m;
int h[N], e[M], ne[M], idx;
int dist[N], q[M];

void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }

int bfs(int st, int ed) {
  memset(dist, -1, sizeof dist);
  int hh = 0, tt = -1;
  dist[st * 3] = 0;
  q[++tt] = st * 3;

  while (hh <= tt) {
    int t = q[hh++];

    for (int i = h[t]; ~i; i = ne[i]) {
      int j = e[i];
      if (dist[j] != -1)
        continue;

      dist[j] = dist[t] + 1;
      q[++tt] = j;
    }
  }

  if (dist[ed * 3] == -1)
    return -1;
  else
    return dist[ed * 3] / 3;
}

int main() {
  memset(h, -1, sizeof h);
  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    add(3 * a, 3 * b + 1);
    add(3 * a + 1, 3 * b + 2);
    add(3 * a + 2, 3 * b);
  }

  int st, ed;
  scanf("%d%d", &st, &ed);
  printf("%d\n", bfs(st, ed));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef priority_queue<int> HEAP;
typedef priority_queue<int, vector<int>, greater<int>> RHEAP;

const int N = 300010, M = 3 * N;

int n, m;
int h[N], e[M], ne[M], idx;
int dist[N], q[M];

void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }

int bfs(int st, int ed) {
  memset(dist, -1, sizeof dist);
  int hh = 0, tt = -1;
  dist[st * 3] = 0;
  q[++tt] = st * 3;

  while (hh <= tt) {
    int t = q[hh++];

    for (int i = h[t]; ~i; i = ne[i]) {
      int j = e[i];
      if (dist[j] != -1)
        continue;

      dist[j] = dist[t] + 1;
      q[++tt] = j;
    }
  }

  if (dist[ed * 3] == -1)
    return -1;
  else
    return dist[ed * 3] / 3;
}

int main() {
  memset(h, -1, sizeof h);
  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    add(3 * a, 3 * b + 1);
    add(3 * a + 1, 3 * b + 2);
    add(3 * a + 2, 3 * b);
  }

  int st, ed;
  scanf("%d%d", &st, &ed);
  printf("%d\n", bfs(st, ed));
  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 1;

int n, m;
int f[3][N];
vector<int> g[N];
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;

    g[u].push_back(v);
  }

  int S, F;
  cin >> S >> F;
  --S, --F;

  for (int j = 0; j < 3; ++j)
    for (int i = 0; i < n; ++i)
      f[j][i] = 1e9;
  f[S][0] = 0;

  queue<pair<int, int>> q;
  q.push(make_pair(S, 0));

  while (q.size()) {
    int u = q.front().first, o = q.front().second;
    q.pop();

    for (int v : g[u]) {
      if (f[(o + 1) % 3][v] != 1e9)
        continue;
      f[(o + 1) % 3][v] = f[o][u] + 1;
      q.push(make_pair(v, (o + 1) % 3));
    }
  }

  int res = f[0][F];
  if (res == 1e9)
    res = -1;
  else
    res /= 3;

  cout << res << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 1;

int n, m;
int f[3][N];
vector<int> g[N];
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;

    g[u].push_back(v);
  }

  int S, F;
  cin >> S >> F;
  --S, --F;

  for (int j = 0; j < 3; ++j)
    for (int i = 0; i < n; ++i)
      f[j][i] = 1e9;
  f[0][S] = 0;

  queue<pair<int, int>> q;
  q.push(make_pair(S, 0));

  while (q.size()) {
    int u = q.front().first, o = q.front().second;
    q.pop();

    for (int v : g[u]) {
      if (f[(o + 1) % 3][v] != 1e9)
        continue;
      f[(o + 1) % 3][v] = f[o][u] + 1;
      q.push(make_pair(v, (o + 1) % 3));
    }
  }

  int res = f[0][F];
  if (res == 1e9)
    res = -1;
  else
    res /= 3;

  cout << res << endl;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>

#define NMAX 10000
#define MOD 1000000007
#define INF 2000000000

#define MIN(a, b) (a < b ? a : b)

using namespace std;

struct Node {
  int node;
  int level;
};

int n, m;
int s, t;
vector<int> graph[3 * NMAX];
queue<Node> Q;
bool vis[3 * NMAX];

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    graph[a].push_back(b + n);
    graph[a + n].push_back(b + 2 * n);
    graph[a + 2 * n].push_back(b);
  }
  cin >> s >> t;
  s--;
  t--;

  Node start = {s, 0};
  Q.push(start);
  int minPath = INF;
  while (!Q.empty()) {
    Node node = Q.front();
    if (node.node == t && node.level < minPath && node.level % 3 == 0)
      minPath = node.level;
    Q.pop();
    for (int i = 0; i < graph[node.node].size() && !vis[node.node]; i++) {
      Node other = {graph[node.node][i], node.level + 1};
      Q.push(other);
    }
    vis[node.node] = 1;
  }
  cout << (minPath != INF ? minPath / 3 : -1) << '\n';
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>

#define NMAX 100000
#define MOD 1000000007
#define INF 2000000000

#define MIN(a, b) (a < b ? a : b)

using namespace std;

struct Node {
  int node;
  int level;
};

int n, m;
int s, t;
vector<int> graph[3 * NMAX];
queue<Node> Q;
bool vis[3 * NMAX];

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    graph[a].push_back(b + n);
    graph[a + n].push_back(b + 2 * n);
    graph[a + 2 * n].push_back(b);
  }
  cin >> s >> t;
  s--;
  t--;

  Node start = {s, 0};
  Q.push(start);
  int minPath = INF;
  while (!Q.empty()) {
    Node node = Q.front();
    if (node.node == t && node.level < minPath && node.level % 3 == 0)
      minPath = node.level;
    Q.pop();
    for (int i = 0; i < graph[node.node].size() && !vis[node.node]; i++) {
      Node other = {graph[node.node][i], node.level + 1};
      Q.push(other);
    }
    vis[node.node] = 1;
  }
  cout << (minPath != INF ? minPath / 3 : -1) << '\n';
  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define M 100001
struct D {
  int x, step;
} nxt;
int n, m, st, ed;
vector<int> edge[M];
bool mark[M][3];
int spfa() {
  queue<D> Q;
  Q.push((D){st, 0});
  mark[st][0] = 1;
  while (!Q.empty()) {
    D now = Q.front();
    Q.pop();
    for (int i = 0; i < edge[now.x].size(); i++) {
      int y = edge[now.x][i];
      nxt.x = y, nxt.step = now.step + 1;
      if (mark[y][nxt.step % 3])
        continue;
      if (y == ed && nxt.step % 3 == 0)
        return nxt.step / 3;
      Q.push(nxt);
    }
  }
  return -1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, u, v; i <= m; i++) {
    scanf("%d%d", &u, &v);
    edge[u].push_back(v);
  }
  scanf("%d%d", &st, &ed);
  printf("%d", spfa());
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define M 100001
struct D {
  int x, step;
} nxt;
int n, m, st, ed;
vector<int> edge[M];
bool mark[M][3];
int spfa() {
  queue<D> Q;
  Q.push((D){st, 0});
  mark[st][0] = 1;
  while (!Q.empty()) {
    D now = Q.front();
    Q.pop();
    for (int i = 0; i < edge[now.x].size(); i++) {
      int y = edge[now.x][i];
      nxt.x = y, nxt.step = now.step + 1;
      if (mark[y][nxt.step % 3])
        continue;
      mark[y][nxt.step % 3] = 1;
      if (y == ed && nxt.step % 3 == 0)
        return nxt.step / 3;
      Q.push(nxt);
    }
  }
  return -1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, u, v; i <= m; i++) {
    scanf("%d%d", &u, &v);
    edge[u].push_back(v);
  }
  scanf("%d%d", &st, &ed);
  printf("%d", spfa());
  return 0;
}