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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100009;
int N, M;

struct unionfind {
  int par[MAX_N];
  int myrank[MAX_N];
  int size[MAX_N];
  void init(int n) {
    for (int i = 1; i <= n; i++) {
      par[i] = i;
      myrank[i] = 0;
      size[i] = 1;
    }
  }
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) {
      return;
    }
    if (myrank[x] < myrank[y]) {
      par[x] = y;
      size[y] += size[x];
    } else {
      par[y] = x;
      size[x] += size[y];
    }
    if (myrank[x] == myrank[y]) {
      myrank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
  cin >> N >> M;
  unionfind T;
  T.init(N + M);
  for (int i = 1; i <= N; i++) {
    int K;
    cin >> K;
    for (int j = 0; j < K; j++) {
      int L;
      cin >> L;
      T.unite(i, N + L);
    }
  }
  string ans = "YES";
  int memo = T.find(1);
  for (int i = 2; i <= N; i++) {
    if (memo != T.find(i))
      ans = "NO";
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200009;
int N, M;

struct unionfind {
  int par[MAX_N];
  int myrank[MAX_N];
  int size[MAX_N];
  void init(int n) {
    for (int i = 1; i <= n; i++) {
      par[i] = i;
      myrank[i] = 0;
      size[i] = 1;
    }
  }
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) {
      return;
    }
    if (myrank[x] < myrank[y]) {
      par[x] = y;
      size[y] += size[x];
    } else {
      par[y] = x;
      size[x] += size[y];
    }
    if (myrank[x] == myrank[y]) {
      myrank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
  cin >> N >> M;
  unionfind T;
  T.init(N + M);
  for (int i = 1; i <= N; i++) {
    int K;
    cin >> K;
    for (int j = 0; j < K; j++) {
      int L;
      cin >> L;
      T.unite(i, N + L);
    }
  }
  string ans = "YES";
  int memo = T.find(1);
  for (int i = 2; i <= N; i++) {
    if (memo != T.find(i))
      ans = "NO";
  }
  cout << ans << endl;
}
Problem p03923 (C++)

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

using namespace std;

llint N, A;

int main(void) {
  cin >> N >> A;

  llint ans = 1e18;
  for (int k = 1; k < 50; k++) {
    llint ub = N + 1, lb = 0, mid;
    while (ub - lb > 1) {
      mid = (ub + lb) / 2;
      llint mul = 1;
      for (int i = 0; i < k; i++) {
        if (mul >= (N + mid - 1) / mid) {
          mul = N;
          break;
        }
        mul *= mid;
      }
      if (mul >= N)
        ub = mid;
      else
        lb = mid;
    }
    llint s = ub;

    ub = k, lb = 0, mid;
    while (ub - lb > 1) {
      mid = (ub + lb) / 2;
      llint mul = 1;
      for (int i = 0; i < k - mid; i++) {
        if (mul >= (N + s - 1) / s) {
          mul = N;
          break;
        }
        mul *= s;
      }
      for (int i = k - mid; i < k; i++) {
        if (mul >= (N + s - 2) / (s - 1)) {
          mul = N;
          break;
        }
        mul *= s - 1;
      }
      if (mul >= N)
        lb = mid;
      else
        ub = mid;
    }
    llint x = lb;

    // cout << k << " " << s << " " << x << endl;
    ans = min(ans, s * k - x + A * (k - 1));
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#define llint long long

using namespace std;

llint N, A;

int main(void) {
  cin >> N >> A;

  if (N == 1) {
    cout << 1 << endl;
    return 0;
  }

  llint ans = 1e18;
  for (int k = 1; k < 50; k++) {
    llint ub = N + 1, lb = 0, mid;
    while (ub - lb > 1) {
      mid = (ub + lb) / 2;
      llint mul = 1;
      for (int i = 0; i < k; i++) {
        if (mul >= (N + mid - 1) / mid) {
          mul = N;
          break;
        }
        mul *= mid;
      }
      if (mul >= N)
        ub = mid;
      else
        lb = mid;
    }
    llint s = ub;

    ub = k, lb = 0, mid;
    while (ub - lb > 1) {
      mid = (ub + lb) / 2;
      llint mul = 1;
      for (int i = 0; i < k - mid; i++) {
        if (mul >= (N + s - 1) / s) {
          mul = N;
          break;
        }
        mul *= s;
      }
      for (int i = k - mid; i < k; i++) {
        if (mul >= (N + s - 2) / (s - 1)) {
          mul = N;
          break;
        }
        mul *= s - 1;
      }
      if (mul >= N)
        lb = mid;
      else
        ub = mid;
    }
    llint x = lb;

    // cout << k << " " << s << " " << x << endl;
    ans = min(ans, s * k - x + A * (k - 1));
  }
  cout << ans << endl;
  return 0;
}
Problem p03923 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>

using namespace std;
typedef long long ll;
typedef pair<int, int> Pi;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()

ll N, A;

int chk(ll a, ll b, ll c) {
  // return a^b >= c
  if (pow(a, b) > c + c)
    return 1;
  ll res = 1;
  rep(i, b) res = res * a;
  return res >= c;
}

void solve() {
  scanf("%lld%lld", &N, &A);
  ll ans = N;
  for (int i = 2; i <= 40; i++) {
    ll low = 2, high = N, res = 1;
    while (low <= high) {
      ll mid = (low + high) >> 1;
      if (chk(mid, i, N))
        res = mid, high = mid - 1;
      else
        low = mid + 1;
    }
    ll cnt = A * (i - 1) + res * i;
    ll N2 = N;
    for (int j = 1; j <= i; j++) {
      ll N3 = (N2 + res - 2) / (res - 1);
      if (chk(res, i - j, N3)) {
        N2 = N3;
        cnt--;
      } else
        break;
    }
    ans = min(ans, cnt);
  }
  printf("%lld", ans);
}

int main() {
  int Tc = 1; // scanf("%d\n", &Tc);
  for (int tc = 1; tc <= Tc; tc++) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>

using namespace std;
typedef long long ll;
typedef pair<int, int> Pi;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()

ll N, A;

int chk(ll a, ll b, ll c) {
  // return a^b >= c
  if (pow(a, b) > c + c)
    return 1;
  ll res = 1;
  rep(i, b) res = res * a;
  return res >= c;
}

void solve() {
  scanf("%lld%lld", &N, &A);
  ll ans = N;
  for (int i = 2; i <= 40; i++) {
    ll low = 2, high = N, res = 2;
    while (low <= high) {
      ll mid = (low + high) >> 1;
      if (chk(mid, i, N))
        res = mid, high = mid - 1;
      else
        low = mid + 1;
    }
    ll cnt = A * (i - 1) + res * i;
    ll N2 = N;
    for (int j = 1; j <= i; j++) {
      ll N3 = (N2 + res - 2) / (res - 1);
      if (chk(res, i - j, N3)) {
        N2 = N3;
        cnt--;
      } else
        break;
    }
    ans = min(ans, cnt);
  }
  printf("%lld", ans);
}

int main() {
  int Tc = 1; // scanf("%d\n", &Tc);
  for (int tc = 1; tc <= Tc; tc++) {
    solve();
  }
  return 0;
}
Problem p03924 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _repargs(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T> inline void output(T a, int p = 0) {
  if (p)
    cout << fixed << setprecision(p) << a << "\n";
  else
    cout << a << "\n";
}
// end of template

// i日目, 訪れた頂点数j, 頂点1に戻れる頂点数k
ll dp[301][301][301];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  // source code
  int N, M;
  cin >> N >> M;
  dp[0][1][1] = 1;
  rep(i, M) rep(j, N + 1) rep(k, N + 1) {
    (dp[i + 1][j + 1][k] += dp[i][j][k] * (N - j)) %= mod;
    if (j >= k)
      (dp[i + 1][j][k] += dp[i][j][k] * (j - k)) %= mod;
    (dp[i + 1][j][j] += dp[i][j][k] * k) %= mod;
  }

  output(dp[M][N][N]);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _repargs(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T> inline void output(T a, int p = 0) {
  if (p)
    cout << fixed << setprecision(p) << a << "\n";
  else
    cout << a << "\n";
}
// end of template

// i日目, 訪れた頂点数j, 頂点1に戻れる頂点数k
ll dp[301][302][301];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  // source code
  int N, M;
  cin >> N >> M;
  dp[0][1][1] = 1;
  rep(i, M) rep(j, N + 1) rep(k, N + 1) {
    (dp[i + 1][j + 1][k] += dp[i][j][k] * (N - j)) %= mod;
    if (j >= k)
      (dp[i + 1][j][k] += dp[i][j][k] * (j - k)) %= mod;
    (dp[i + 1][j][j] += dp[i][j][k] * k) %= mod;
  }

  output(dp[M][N][N]);

  return 0;
}
Problem p03924 (C++)

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

using namespace std;

const int MOD = 1000000007;

int main() {
  int N, M;
  static long long dp[301][301][301];
  while (cin >> N >> M) {
    memset(dp, 0, sizeof(dp));
    dp[0][1][0] = 1;
    for (int day = 0; day < M; day++) {
      for (int ok = 0; ok < N; ok++) {
        for (int ng = 0; ng < N; ng++) {
          dp[day + 1][ok][ng] += dp[day][ok][ng] * ng;
          dp[day + 1][ok][ng] %= MOD;
          dp[day + 1][ok + ng][0] += dp[day][ok][ng] * ok;
          dp[day + 1][ok + ng][0] %= MOD;
          dp[day + 1][ok][ng + 1] += dp[day][ok][ng] * (N - ok - ng);
          dp[day + 1][ok][ng + 1] %= MOD;
        }
      }
      dp[day + 1][N][0] += dp[day][N][0] * N;
      dp[day + 1][N][0] %= MOD;
    }
    cout << dp[M][N][0] << endl;
  }
}


Fixed submission:
#include <cstring>
#include <iostream>

using namespace std;

const int MOD = 1000000007;

int main() {
  int N, M;
  static long long dp[301][301][301];
  while (cin >> N >> M) {
    memset(dp, 0, sizeof(dp));
    dp[0][1][0] = 1;
    for (int day = 0; day < M; day++) {
      for (int ok = 0; ok < N; ok++) {
        for (int ng = 0; ng < N; ng++) {
          if (dp[day][ok][ng] == 0)
            continue;
          dp[day + 1][ok][ng] += dp[day][ok][ng] * ng;
          dp[day + 1][ok][ng] %= MOD;
          dp[day + 1][ok + ng][0] += dp[day][ok][ng] * ok;
          dp[day + 1][ok + ng][0] %= MOD;
          dp[day + 1][ok][ng + 1] += dp[day][ok][ng] * (N - ok - ng);
          dp[day + 1][ok][ng + 1] %= MOD;
        }
      }
      dp[day + 1][N][0] += dp[day][N][0] * N;
      dp[day + 1][N][0] %= MOD;
    }
    cout << dp[M][N][0] << endl;
  }
}
Problem p03924 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
long long dp[301][301][301] = {};

void add(long long &a, long long b) { a = (a + b) % MOD; }

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

  dp[0][1][1] = 1;
  for (int n = 0; n < M; ++n) {
    for (int x = 1; x <= N; ++x) {
      for (int y = 1; y <= N; ++y) {
        add(dp[n + 1][x + 1][y], dp[n][x][y] * (N - x));
        add(dp[n + 1][x][y], dp[n][x][y] * (x - y));
        add(dp[n + 1][x][x], dp[n][x][y] * y);
      }
    }
  }
  cout << dp[M][N][N] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
long long dp[301][301][301] = {};

void add(long long &a, long long b) { a = (a + b) % MOD; }

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

  dp[0][1][1] = 1;
  for (int n = 0; n < M; ++n) {
    for (int x = 1; x <= N; ++x) {
      for (int y = 1; y <= N; ++y) {
        if (x < N)
          add(dp[n + 1][x + 1][y], dp[n][x][y] * (N - x));
        add(dp[n + 1][x][y], dp[n][x][y] * (x - y));
        add(dp[n + 1][x][x], dp[n][x][y] * y);
      }
    }
  }
  cout << dp[M][N][N] << endl;
}
Problem p03924 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

ll dp[334][334][334];

int main(void) {
  int n, m, i, j, k;
  cin >> n >> m;
  dp[0][n - 1][1] = 1ll;
  for (i = 0; i < m; ++i) {
    for (j = 0; j < n; ++j) {
      for (k = 0; k < n; ++k) {
        if (j + k + 1 > n) {
          continue;
        }
        add_mod(dp[i + 1][j][k + 1], dp[i][j + 1][k + 1] * (j + 1) % MOD);
        add_mod(dp[i + 1][j][k + 1], dp[i][j][k + 1] * (n - j - k - 1) % MOD);
        add_mod(dp[i + 1][j][n - j], dp[i][j][k + 1] * (k + 1) % MOD);
      }
    }
  }
  cout << dp[m][0][n] << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

ll dp[301][301][301];

int main(void) {
  int n, m, i, j, k;
  cin >> n >> m;
  dp[0][n - 1][1] = 1ll;
  for (i = 0; i < m; ++i) {
    for (j = 0; j < n; ++j) {
      for (k = 0; k < n; ++k) {
        if (j + k + 1 > n) {
          continue;
        }
        add_mod(dp[i + 1][j][k + 1], dp[i][j + 1][k + 1] * (j + 1) % MOD);
        add_mod(dp[i + 1][j][k + 1], dp[i][j][k + 1] * (n - j - k - 1) % MOD);
        add_mod(dp[i + 1][j][n - j], dp[i][j][k + 1] * (k + 1) % MOD);
      }
    }
  }
  cout << dp[m][0][n] << endl;
  return 0;
}
Problem p03924 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

long long int result[301][301][301] = {};

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

  // result[i][j][k] : 頂点1からの長さiのパスで、
  // パスに含まれるノード数はj個で
  // 頂点1と強連結しているものはk個
  result[0][1][1] = 1;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= j; k++) {
        result[i + 1][j + 1][k] += result[i][j][k] * (n - j);
        result[i + 1][j + 1][k] = result[i + 1][j + 1][k] % mod;
        result[i + 1][j][k] += result[i][j][k] * (j - k);
        result[i + 1][j][k] = result[i + 1][j][k] % mod;
        result[i + 1][j][j] += result[i][j][k] * k;
        result[i + 1][j][j] = result[i + 1][j][j] % mod;
      }
    }
  }
  cout << result[m][n][n] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

long long int result[310][310][310] = {};

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

  // result[i][j][k] : 頂点1からの長さiのパスで、
  // パスに含まれるノード数はj個で
  // 頂点1と強連結しているものはk個
  result[0][1][1] = 1;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= j; k++) {
        result[i + 1][j + 1][k] += result[i][j][k] * (n - j);
        result[i + 1][j + 1][k] = result[i + 1][j + 1][k] % mod;
        result[i + 1][j][k] += result[i][j][k] * (j - k);
        result[i + 1][j][k] = result[i + 1][j][k] % mod;
        result[i + 1][j][j] += result[i][j][k] * k;
        result[i + 1][j][j] = result[i + 1][j][j] % mod;
      }
    }
  }
  cout << result[m][n][n] << endl;
  return 0;
}
Problem p03925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;

int main() {
  srand(time(0));

  ios_base ::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;

  vector<int> a(m, 0), b(m, 0), c(m, 0);
  vector<int> v(n, inf);
  vector<pair<int, int>> edges;

  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i] >> c[i];
    v[a[i]] = min(v[a[i]], c[i] + 1);
    v[b[i]] = min(v[b[i]], c[i] + 2);
  }

  int best = inf;

  for (int i = 0; i < 3 * n; ++i) {
    best = min(best + 2, v[i % n]);
    v[i % n] = min(best, v[i % n]);
  }

  for (int i = 0; i < n; ++i) {
    a.push_back(i);
    b.push_back((i + 1) % n);
    c.push_back(v[i]);
  }

  for (int i = 0; i < n + m; ++i) {
    edges.push_back({c[i], i});
  }

  sort(edges.begin(), edges.end());
  long long ans = 0;

  vector<int> dad(n, 0);
  for (int i = 0; i < n; ++i)
    dad[i] = i;

  auto f = [&](int x) {
    int root = x;
    while (root != dad[root])
      root = dad[root];
    return root;
  };

  auto unite = [&](int a, int b) {
    if (rand() & 1)
      dad[a] = b;
    else
      dad[b] = a;
  };

  for (auto edge : edges) {
    int x = a[edge.second];
    int y = b[edge.second];
    if (f(x) != f(y)) {
      unite(f(x), f(y));
      ans += edge.first;
    }
  }

  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;

int main() {
  srand(time(0));

  ios_base ::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;

  vector<int> a(m, 0), b(m, 0), c(m, 0);
  vector<int> v(n, inf);
  vector<pair<int, int>> edges;

  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i] >> c[i];
    v[a[i]] = min(v[a[i]], c[i] + 1);
    v[b[i]] = min(v[b[i]], c[i] + 2);
  }

  int best = inf;

  for (int i = 0; i < 3 * n; ++i) {
    best = min(best + 2, v[i % n]);
    v[i % n] = min(best, v[i % n]);
  }

  for (int i = 0; i < n; ++i) {
    a.push_back(i);
    b.push_back((i + 1) % n);
    c.push_back(v[i]);
  }

  for (int i = 0; i < n + m; ++i) {
    edges.push_back({c[i], i});
  }

  sort(edges.begin(), edges.end());
  long long ans = 0;

  vector<int> dad(n, 0);
  for (int i = 0; i < n; ++i)
    dad[i] = i;

  auto f = [&](int x) {
    int root = x;
    while (root != dad[root])
      root = dad[root];
    while (x != root) {
      int temp = dad[x];
      dad[x] = root;
      x = temp;
    }
    return root;
  };

  auto unite = [&](int a, int b) {
    if (rand() & 1)
      dad[a] = b;
    else
      dad[b] = a;
  };

  for (auto edge : edges) {
    int x = a[edge.second];
    int y = b[edge.second];
    if (f(x) != f(y)) {
      unite(f(x), f(y));
      ans += edge.first;
    }
  }

  cout << ans << "\n";
}
Problem p03925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;

int main() {
  srand(time(0));

  ios_base ::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;

  vector<int> a(m, 0), b(m, 0), c(m, 0);
  vector<int> v(n, inf);
  vector<pair<int, int>> edges;

  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i] >> c[i];
    v[a[i]] = min(v[a[i]], c[i] + 1);
    v[b[i]] = min(v[b[i]], c[i] + 2);
  }

  int best = inf;

  for (int i = 0; i < 3 * n; ++i) {
    best = min(best + 2, v[i % n]);
    v[i % n] = min(best, v[i % n]);
  }

  for (int i = 0; i < n; ++i) {
    a.push_back(i);
    b.push_back((i + 1) % n);
    c.push_back(v[i]);
  }

  for (int i = 0; i < n + m; ++i) {
    edges.push_back({c[i], i});
  }

  sort(edges.begin(), edges.end());
  long long ans = 0;

  vector<int> dad(n, 0), sz(n, 0);
  for (int i = 0; i < n; ++i)
    dad[i] = i;

  auto f = [&](int x) {
    int root = x;
    int steps = 0;
    while (root != dad[root]) {
      root = dad[root];
      ++steps;
    }
    return root;
  };

  auto unite = [&](int a, int b) {
    if (sz[a] < sz[b])
      dad[a] = b;
    else
      dad[b] = a;
    if (sz[a] == sz[b])
      sz[b]++;
    assert(sz[a] <= 5);
    assert(sz[b] <= 5);
  };

  for (auto edge : edges) {
    int x = a[edge.second];
    int y = b[edge.second];
    if (f(x) != f(y)) {
      unite(f(x), f(y));
      ans += edge.first;
    }
  }

  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;

int main() {
  srand(time(0));

  ios_base ::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;

  vector<int> a(m, 0), b(m, 0), c(m, 0);
  vector<int> v(n, inf);
  vector<pair<int, int>> edges;

  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i] >> c[i];
    v[a[i]] = min(v[a[i]], c[i] + 1);
    v[b[i]] = min(v[b[i]], c[i] + 2);
  }

  int best = inf;

  for (int i = 0; i < 3 * n; ++i) {
    best = min(best + 2, v[i % n]);
    v[i % n] = min(best, v[i % n]);
  }

  for (int i = 0; i < n; ++i) {
    a.push_back(i);
    b.push_back((i + 1) % n);
    c.push_back(v[i]);
  }

  for (int i = 0; i < n + m; ++i) {
    edges.push_back({c[i], i});
  }

  sort(edges.begin(), edges.end());
  long long ans = 0;

  vector<int> dad(n, 0), sz(n, 0);
  for (int i = 0; i < n; ++i)
    dad[i] = i;

  auto f = [&](int x) {
    int root = x;
    int steps = 0;
    while (root != dad[root]) {
      root = dad[root];
      ++steps;
    }
    return root;
  };

  auto unite = [&](int a, int b) {
    if (sz[a] < sz[b])
      dad[a] = b;
    else
      dad[b] = a;
    if (sz[a] == sz[b])
      sz[a]++;
  };

  for (auto edge : edges) {
    int x = a[edge.second];
    int y = b[edge.second];
    if (f(x) != f(y)) {
      unite(f(x), f(y));
      ans += edge.first;
    }
  }

  cout << ans << "\n";
}
Problem p03925 (C++)

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

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
struct edge {
  int u, v, w;
} vs[N << 1];
int n, q, tA[N], tB[N], ee, st[N], cnt, F1[N], F2[N], fa[N], m;
inline bool cmp(const edge &a, const edge &b) { return a.w <= b.w; }
inline int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); }
int main() {
  // freopen("read.in","r",stdin);
  n = read();
  m = q = read();
  memset(tA, 0x3f, sizeof tA);
  memset(F1, 0x3f, sizeof F1);
  while (q--) {
    int u = read(), v = read(), w = read();
    vs[++cnt] = (edge){u + 1, v + 1, w};
    tA[u] = min(tA[u], w + 1);
    tA[v] = min(tA[v], w + 2);
  }
  for (int i = 0; i < n; i++)
    F1[i] = min(F1[((i - 1) + n) % n] + 2, tA[i]);
  for (int i = 0; i < n; i++)
    F1[i] = min(F1[((i - 1) + n) % n] + 2, tA[i]);
  for (int i = 0; i < n; i++)
    vs[++cnt] = (edge){i + 1, (i + 1) % n + 1, F1[i]};
  // cout << cnt << endl;
  sort(vs + 1, vs + 1 + cnt, cmp);
  // assert(0);
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  long long Ans = 0;
  for (int i = 1; i <= cnt; i++) {
    int f1 = getfa(vs[i].u), f2 = getfa(vs[i].v);
    if (f1 != f2) {
      Ans += vs[i].w;
      fa[f1] = f2;
      n--;
    }
    if (n == 1)
      break;
  }
  cout << Ans << endl;
  return 0;
}

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

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
struct edge {
  int u, v, w;
} vs[N << 1];
int n, q, tA[N], tB[N], ee, st[N], cnt, F1[N], F2[N], fa[N], m;
inline bool cmp(const edge &a, const edge &b) {
  return a.w < b.w; /*not a.w<=b.w or you'll get a TLE result*/
} // stdsort's compelxity depends on its operator ?
inline int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); }
int main() {
  // freopen("read.in","r",stdin);
  n = read();
  m = q = read();
  memset(tA, 0x3f, sizeof tA);
  memset(F1, 0x3f, sizeof F1);
  while (q--) {
    int u = read(), v = read(), w = read();
    vs[++cnt] = (edge){u + 1, v + 1, w};
    tA[u] = min(tA[u], w + 1);
    tA[v] = min(tA[v], w + 2);
  }
  for (int i = 0; i < n; i++)
    F1[i] = min(F1[((i - 1) + n) % n] + 2, tA[i]);
  for (int i = 0; i < n; i++)
    F1[i] = min(F1[((i - 1) + n) % n] + 2, tA[i]);
  for (int i = 0; i < n; i++)
    vs[++cnt] = (edge){i + 1, (i + 1) % n + 1, F1[i]};
  // cout << cnt << endl;
  sort(vs + 1, vs + 1 + cnt, cmp);
  // assert(0);
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  long long Ans = 0;
  for (int i = 1; i <= cnt; i++) {
    int f1 = getfa(vs[i].u), f2 = getfa(vs[i].v);
    if (f1 != f2) {
      Ans += vs[i].w;
      fa[f1] = f2;
      n--;
    }
    if (n == 1)
      break;
  }
  cout << Ans << endl;
  return 0;
}
Problem p03929 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define FI first
#define SE second
#define PF push_front
#define PB push_back
#define PPF pop_front
#define PPB pop_back
#define MA make_pair
#define ll long long
#define PA pair<int, int>
#define VE vector<int>
#define VP vector<PA>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ROF(i, a, b) for (int i = b - 1; i >= a; i--)
#define YES(i) cout << (i ? "YES" : "NO") << endl
#define Yes(i) cout << (i ? "Yes" : "No") << endl
using namespace std;
//
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
//
//
struct poi {
  int X;
  int Y;
  int Z;
  bool operator<(const poi &R) const {
    return X == R.X ? Y == R.Y ? Z < R.Z : Y < R.Y : X < R.X;
  }
};
//
//
int main() {
  int N, K;
  cin >> N >> K;
  int ans = 0;
  FOR(i, 1, N - 1) {
    FOR(j, 2, 7) {
      if ((7 * i + j) * 9 % 11 == K) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define FI first
#define SE second
#define PF push_front
#define PB push_back
#define PPF pop_front
#define PPB pop_back
#define MA make_pair
#define ll long long
#define PA pair<int, int>
#define VE vector<int>
#define VP vector<PA>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ROF(i, a, b) for (int i = b - 1; i >= a; i--)
#define YES(i) cout << (i ? "YES" : "NO") << endl
#define Yes(i) cout << (i ? "Yes" : "No") << endl
using namespace std;
//
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
//
//
struct poi {
  int X;
  int Y;
  int Z;
  bool operator<(const poi &R) const {
    return X == R.X ? Y == R.Y ? Z < R.Z : Y < R.Y : X < R.X;
  }
};
//
//
int main() {
  int N, K;
  cin >> N >> K;
  int ans = 0;
  N -= 2;
  ans += N / 11 * 5;
  N %= 11;
  if (N >= 1) {
    if (K == 0 || K == 2 || K == 4 || K == 7 || K == 9) {
      ans++;
    }
  }
  if (N >= 2) {
    if (K == 1 || K == 4 || K == 6 || K == 8 || K == 10) {
      ans++;
    }
  }
  if (N >= 3) {
    if (K == 1 || K == 3 || K == 5 || K == 7 || K == 9) {
      ans++;
    }
  }
  if (N >= 4) {
    if (K == 0 || K == 2 || K == 4 || K == 6 || K == 9) {
      ans++;
    }
  }
  if (N >= 5) {
    if (K == 1 || K == 3 || K == 6 || K == 8 || K == 10) {
      ans++;
    }
  }
  if (N >= 6) {
    if (K == 0 || K == 3 || K == 5 || K == 7 || K == 9) {
      ans++;
    }
  }
  if (N >= 7) {
    if (K == 0 || K == 2 || K == 4 || K == 6 || K == 8) {
      ans++;
    }
  }
  if (N >= 8) {
    if (K == 1 || K == 3 || K == 5 || K == 8 || K == 10) {
      ans++;
    }
  }
  if (N >= 9) {
    if (K == 0 || K == 2 || K == 4 || K == 7 || K == 9) {
      ans++;
    }
  }
  if (N >= 10) {
    if (K == 2 || K == 4 || K == 6 || K == 8 || K == 10) {
      ans++;
    }
  }
  if (N >= 11) {
    if (K == 1 || K == 3 || K == 5 || K == 7 || K == 10) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03929 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  long long ans = 0;
  for (long long i = 1; i <= n - 2; i++) {
    for (long long j = 1; j <= 5; j++) {
      long long sum = 9 * (7 * i + j + 1);
      if (sum % 11 == k) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  long long ans = 0;
  ans += 5 * ((n - 2) / 11);
  for (long long i = 1; i <= (n - 2) % 11; i++) {
    for (long long j = 1; j <= 5; j++) {
      long long sum = 9 * (7 * i + j + 1);
      if (sum % 11 == k) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03929 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

int main() {
  int n;
  int k;
  int cnt = 0;

  vector<vector<int>> vvi;

  vvi.resize(11);

  vvi[0] = {0, 2, 4, 7, 9};
  vvi[1] = {1, 4, 6, 8, 10};
  vvi[2] = {1, 3, 5, 7, 9};
  vvi[3] = {0, 2, 4, 6, 9};
  vvi[4] = {1, 3, 6, 8, 10};
  vvi[5] = {0, 3, 5, 7, 9};
  vvi[6] = {0, 2, 4, 6, 8};
  vvi[7] = {1, 3, 5, 8, 10};
  vvi[8] = {0, 2, 5, 7, 9};
  vvi[9] = {2, 4, 6, 8, 10};
  vvi[10] = {1, 3, 5, 7, 10};

  if (scanf("%d %d", &n, &k) < 2)
    return 0;

  for (int i = 1; i < n - 1; i++) {
    if (binary_search(vvi[(i - 1) % 11].begin(), vvi[(i - 1) % 11].end(), k)) {
      cnt++;
    }
  }

  printf("%d\n", cnt);
  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

int main() {
  int n;
  int k;
  int cnt = 0;

  vector<vector<int>> vvi;

  vvi.resize(11);

  vvi[0] = {0, 2, 4, 7, 9};
  vvi[1] = {1, 4, 6, 8, 10};
  vvi[2] = {1, 3, 5, 7, 9};
  vvi[3] = {0, 2, 4, 6, 9};
  vvi[4] = {1, 3, 6, 8, 10};
  vvi[5] = {0, 3, 5, 7, 9};
  vvi[6] = {0, 2, 4, 6, 8};
  vvi[7] = {1, 3, 5, 8, 10};
  vvi[8] = {0, 2, 5, 7, 9};
  vvi[9] = {2, 4, 6, 8, 10};
  vvi[10] = {1, 3, 5, 7, 10};

  if (scanf("%d %d", &n, &k) < 2)
    return 0;

  int div = 0;
  int mod = 0;

  div = (n - 2) / 11;
  mod = (n - 2) % 11;

  cnt = div * 5;

  int start = 0;
  start = div * 11 + 1;

  for (int i = start; i < start + mod; i++) {
    if (binary_search(vvi[(i - 1) % 11].begin(), vvi[(i - 1) % 11].end(), k)) {
      cnt++;
    }
  }

  printf("%d\n", cnt);
  return 0;
}
Problem p03929 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
int main() {
  ll n, k, cnt = 0, num;
  cin >> n >> k;
  for (ll i = 1; i <= n - 2; i++) {
    num = 63 * i;
    for (ll j = 1; j <= 5; j++) {
      if ((num + 9 * j + 9) % 11 == k)
        cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
int main() {
  ll n, k, cnt = 0, num;
  cin >> n >> k;
  if (n >= 11) {
    cnt = ((n / 11) - 1) * 5;
    for (ll i = n - (n % 11) - 10; i <= n - 2; i++) {
      num = 63 * i;
      for (ll j = 1; j <= 5; j++) {
        if ((num + 9 * j + 9) % 11 == k) {
          cnt++;
          // cout << i << ' ' << j << endl;
        }
      }
    }
  } else {
    for (ll i = n - (n % 11) + 1; i <= n - 2; i++) {
      num = 63 * i;
      for (ll j = 1; j <= 5; j++) {
        if ((num + 9 * j + 9) % 11 == k) {
          cnt++;
          // cout << i << ' ' << j << endl;
        }
      }
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p03929 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep0(i, N) for (int i = 0; i < N; i++)
#define rep1(i, a, N) for (int i = 0 + a; i < N; i++)
#define lint long long int
#define SIZE 100005

using namespace std;

int main() {

  static lint a, b;
  cin >> a >> b;

  if (a == 1 || a == 2) {
    cout << 0;
    return 0;
  }
  a = a - 2;
  int num[8][14];
  rep1(i, 1, 14) {
    rep1(j, 1, 8) { num[i][j] = i * 7 + j - 7; }
  }
  lint cal[12] = {};
  lint sum = 0, all = 0;
  rep1(i, 1, 12) {
    rep1(j, 1, 6) {
      sum = (num[i][j] + 8) * 9;
      if (sum % 11 == b) {
        cal[i]++;
      }
    }
    all += cal[i];
  }
  lint c = 0, cc;
  cc = a % 11;
  c = (a - cc) / 11;
  lint d = 0;
  rep1(i, 1, cc + 1) { d += cal[i]; }
  cout << all * c + d << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep0(i, N) for (int i = 0; i < N; i++)
#define rep1(i, a, N) for (int i = 0 + a; i < N; i++)
#define lint long long int
#define SIZE 100005

using namespace std;

int main() {

  static lint a, b;
  cin >> a >> b;

  if (a == 1 || a == 2) {
    cout << 0;
    return 0;
  }
  a = a - 2;
  lint num[14][8];
  rep1(i, 1, 14) {
    rep1(j, 1, 8) { num[i][j] = i * 7 + j - 7; }
  }
  lint cal[12] = {};
  lint sum = 0, all = 0;
  rep1(i, 1, 12) {
    rep1(j, 1, 6) {
      sum = (num[i][j] + 8) * 9;
      if (sum % 11 == b) {
        cal[i]++;
      }
    }
    all += cal[i];
  }
  lint c = 0, cc;
  cc = a % 11;
  c = (a - cc) / 11;
  lint d = 0;
  rep1(i, 1, cc + 1) { d += cal[i]; }
  cout << all * c + d << endl;
  return 0;
}
Problem p03929 (Python)

Buggy submission (status: Time Limit Exceeded):
n, k = map(int, input().split())
d = [11, 16, 10, 25, 9, 24, 19, 13, 18, 12, 17]
print(len([x for x in range(d[k], 7 * (n - 1), 11) if x % 7 > 1]))


Fixed submission:
n, k = map(int, input().split())
d = [11, 16, 10, 25, 9, 24, 19, 13, 18, 12, 17]
e = 7 * (n - 1)
r = (e - d[k] - 1) // 77
print(5 * r + len([x for x in range(d[k] + 77 * r, e, 11) if x % 7 > 1]))
Problem p03931 (C++)

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

long long n, k;
long long a[100];

const long long mod = 1e9 + 7;
long long dp[100][100][256][2];
long long po[101];

long long dfs(long long i = 0, long long j = 0, long long s = 0,
              bool f = false) {
  long long &ret = dp[i][j][s][f];

  if (ret != -1)
    return ret;
  ret = 0;
  if (s == k && !f) {
    ret += po[j];
  }

  if (i == n)
    return ret;

  ret += dfs(i + 1, j, s, s == k) % mod;
  ret += dfs(i + 1, j + 1, s ^ a[i]) % mod;

  ret %= mod;

  return ret;
}

signed main() {
  memset(dp, -1, sizeof(dp));
  cin >> n >> k;
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
  }

  po[0] = 1;
  for (long long i = 1; i <= 100; ++i) {
    po[i] = (po[i - 1] * i) % mod;
  }

  cout << dfs() << endl;
}


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

long long n, k;
long long a[100];

const long long mod = 1e9 + 7;
long long dp[101][101][256][2];
long long po[101];

long long dfs(long long i = 0, long long j = 0, long long s = 0,
              bool f = false) {
  long long &ret = dp[i][j][s][f];

  if (ret != -1)
    return ret;
  ret = 0;
  if (s == k && !f) {
    ret += po[j];
  }

  if (i == n)
    return ret;

  ret += dfs(i + 1, j, s, s == k) % mod;
  ret += dfs(i + 1, j + 1, s ^ a[i]) % mod;

  ret %= mod;

  return ret;
}

signed main() {
  memset(dp, -1, sizeof(dp));
  cin >> n >> k;
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
  }

  po[0] = 1;
  for (long long i = 1; i <= 100; ++i) {
    po[i] = (po[i - 1] * i) % mod;
  }

  cout << dfs() << endl;
}
Problem p03931 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000 * 1000 * 1000;
const int MOD = 1000000007;

int main() {

  // cout << "a" << endl;
  int n, K;
  cin >> n >> K;
  vector<long long> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i + 1];

  long long f[110]; // 階乗！
  f[0] = 1;
  f[1] = 1;

  for (int i = 2; i < 110; i++) {
    (f[i] = i * f[i - 1]) %= MOD;
  }

  // cout << f[4] << endl;

  long long dp[102][257][102] =
      {}; // dp[i][j][k] := i番目までを使って
          // 値がjになるとき（aからk個つかっている）の組み合わせ
  dp[0][0][0] = 1;
  // cout << "ok" << endl;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 257; j++) {
      for (int k = 0; k <= n; k++) {

        // cout << i << " " << j << " " << k << endl;

        // 選ばない
        (dp[i][j][k] += dp[i - 1][j][k]) %= MOD;

        // if(i == 3) cout << j << " " (j^a[i]) << " " << endl;
        // 選ぶ
        if (k)
          (dp[i][j][k] += dp[i - 1][j ^ a[i]][k - 1]) %= MOD;

        // cout << i << " " << j << " " << k << endl;
      }
    }
  }

  // cout << "ok" << endl;

  long long ans = 0;
  for (int i = 0; i <= n; i++) {
    // cout << dp[n][K][i] << endl;
    (ans += (dp[n][K][i] * f[i])) %= MOD;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000 * 1000 * 1000;
const int MOD = 1000000007;

int main() {

  // cout << "a" << endl;
  int n, K;
  cin >> n >> K;
  vector<long long> a(n + 1);
  for (int i = 0; i < n; i++)
    cin >> a[i + 1];

  long long f[110]; // 階乗！
  f[0] = 1;
  f[1] = 1;

  for (int i = 2; i < 110; i++) {
    (f[i] = i * f[i - 1]) %= MOD;
  }

  // cout << f[4] << endl;

  long long dp[102][257][102] =
      {}; // dp[i][j][k] := i番目までを使って
          // 値がjになるとき（aからk個つかっている）の組み合わせ
  dp[0][0][0] = 1;
  // cout << "ok" << endl;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 257; j++) {
      for (int k = 0; k <= n; k++) {

        // cout << i << " " << j << " " << k << endl;

        // 選ばない
        (dp[i][j][k] += dp[i - 1][j][k]) %= MOD;

        // if(i == 3) cout << j << " " (j^a[i]) << " " << endl;
        // 選ぶ
        if (k)
          (dp[i][j][k] += dp[i - 1][j ^ a[i]][k - 1]) %= MOD;

        // cout << i << " " << j << " " << k << endl;
      }
    }
  }

  // cout << "ok" << endl;

  long long ans = 0;
  for (int i = 0; i <= n; i++) {
    // cout << dp[n][K][i] << endl;
    (ans += (dp[n][K][i] * f[i])) %= MOD;
  }

  cout << ans << endl;

  return 0;
}
Problem p03931 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int n, k;
  long long b[101][101][256];
  long long f[104], p = 1e9 + 7, c = 0;
  cin >> n >> k;
  f[0] = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int l = 0; l < 256; l++) {
        b[i][j][l] = 0;
      }
    }
    f[i + 1] = ((i + 1) * f[i]) % p;
  }

  b[0][0][0] = 1;

  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    for (int j = 0; j <= i; j++) {
      for (int l = 0; l < 256; l++) {
        b[i][j][l] += b[i - 1][j][l];
        b[i][j + 1][l ^ a] += b[i - 1][j][l];
        b[i][j][l] %= p;
        b[i][j + 1][l ^ a] %= p;
      }
    }
  }
  for (int j = 0; j <= n; j++) {
    c += b[n][j][k] * f[j];
    c %= p;
  }
  cout << c << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int n, k;
  long long b[101][101][256];
  long long f[104], p = 1e9 + 7, c = 0;
  cin >> n >> k;
  f[0] = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int l = 0; l < 256; l++) {
        b[i][j][l] = 0;
      }
    }
    f[i + 1] = ((i + 1) * f[i]) % p;
  }

  b[0][0][0] = 1;

  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    for (int j = 0; j < i; j++) {
      for (int l = 0; l < 256; l++) {
        b[i][j][l] += b[i - 1][j][l];
        b[i][j + 1][l ^ a] += b[i - 1][j][l];
        b[i][j][l] %= p;
        b[i][j + 1][l ^ a] %= p;
      }
    }
  }
  for (int j = 0; j <= n; j++) {
    c += b[n][j][k] * f[j];
    c %= p;
  }
  cout << c << endl;
  return 0;
}
Problem p03932 (C++)

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

#define int long long

#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int H, W;
int A[222][222];

int dp[222][222][222];

signed main() {
  cin >> H >> W;
  rep(i, H) rep(j, W) cin >> A[i][j];

  for (int i = 0; i < H + W - 2; i++) {
    for (int j = 0; j <= i && j < W; j++) {
      for (int k = 0; k <= i && k < W; k++) {
        int x1 = j, y1 = i - j;
        int x2 = k, y2 = i - k;

        rep(b, 4) {
          int nx1 = x1, ny1 = y1;
          int nx2 = x2, ny2 = y2;

          if (b & 1)
            nx1++;
          else
            ny1++;

          if (b >> 1 & 1)
            nx2++;
          else
            ny2++;

          if (nx1 >= W || ny1 >= H || nx2 >= W || ny2 >= H)
            continue;
          if (nx1 != nx2 || ny1 != ny2)
            chmax(dp[i + 1][nx1][nx2], dp[i][j][k] + A[ny1][nx1] + A[ny2][nx2]);
          else
            chmax(dp[i + 1][nx1][nx2], dp[i][j][k] + A[ny1][nx1]);
        }
      }
    }
  }

  cout << dp[H + W - 2][W - 1][W - 1] + A[0][0] << endl;
  return 0;
}


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

#define int long long

#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int H, W;
int A[222][222];

int dp[444][222][222];

signed main() {
  cin >> H >> W;
  rep(i, H) rep(j, W) cin >> A[i][j];

  for (int i = 0; i < H + W - 2; i++) {
    for (int j = 0; j <= i && j < W; j++) {
      for (int k = 0; k <= i && k < W; k++) {
        int x1 = j, y1 = i - j;
        int x2 = k, y2 = i - k;

        rep(b, 4) {
          int nx1 = x1, ny1 = y1;
          int nx2 = x2, ny2 = y2;

          if (b & 1)
            nx1++;
          else
            ny1++;

          if (b >> 1 & 1)
            nx2++;
          else
            ny2++;

          if (nx1 >= W || ny1 >= H || nx2 >= W || ny2 >= H)
            continue;
          if (nx1 != nx2 || ny1 != ny2)
            chmax(dp[i + 1][nx1][nx2], dp[i][j][k] + A[ny1][nx1] + A[ny2][nx2]);
          else
            chmax(dp[i + 1][nx1][nx2], dp[i][j][k] + A[ny1][nx1]);
        }
      }
    }
  }

  cout << dp[H + W - 2][W - 1][W - 1] + A[0][0] << endl;
  return 0;
}
Problem p03932 (C++)

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

using namespace std;

long dp[200][200][200];

int main() {
  int H, W;
  long a[200][200];
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> a[i][j];
    }
  }
  dp[0][0][0] = a[0][0];
  for (int i = 1; i <= H + W - 2; i++) {
    for (int j = 0; j < W; j++) {
      if (i - j < 0 || i - j >= H)
        continue;
      for (int k = 0; k < W; k++) {
        if (i - k < 0 || i - k >= H)
          continue;
        if (j > 0 && k > 0)
          dp[i][j][k] = dp[i - 1][j - 1][k - 1];
        if (i - j - 1 >= 0 && k > 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][k - 1]);
        if (i - k - 1 >= 0 && j > 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j - 1][k]);
        if (i - j - 1 >= 0 && i - k - 1 >= 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][k]);

        if (j == k)
          dp[i][j][k] += a[i - j][j];
        else
          dp[i][j][k] += (a[i - j][j] + a[i - k][k]);
        // cout << i <<  ' ' << j <<  ' ' << k << ' ' << dp[i][j][k] << endl;
      }
    }
  }
  cout << dp[H + W - 2][W - 1][W - 1] << endl;
}

Fixed submission:
#include <iostream>

using namespace std;

long dp[400][200][200];

int main() {
  int H, W;
  long a[200][200];
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> a[i][j];
    }
  }
  dp[0][0][0] = a[0][0];
  for (int i = 1; i <= H + W - 2; i++) {
    for (int j = 0; j < W; j++) {
      if (i - j < 0 || i - j >= H)
        continue;
      for (int k = 0; k < W; k++) {
        if (i - k < 0 || i - k >= H)
          continue;
        if (j > 0 && k > 0)
          dp[i][j][k] = dp[i - 1][j - 1][k - 1];
        if (i - j - 1 >= 0 && k > 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][k - 1]);
        if (i - k - 1 >= 0 && j > 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j - 1][k]);
        if (i - j - 1 >= 0 && i - k - 1 >= 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][k]);

        if (j == k)
          dp[i][j][k] += a[i - j][j];
        else
          dp[i][j][k] += (a[i - j][j] + a[i - k][k]);
        // cout << i <<  ' ' << j <<  ' ' << k << ' ' << dp[i][j][k] << endl;
      }
    }
  }
  cout << dp[H + W - 2][W - 1][W - 1] << endl;
}
Problem p03932 (C++)

Buggy submission (status: Runtime Error):
/*
 * 16-11-20_chu2_D.cpp
 *
 *  Created on: 2016/11/29
 *      Author: ryoma
 */
#include <algorithm>
#include <iostream>

using namespace std;

int field[210][210];
int H, W;

int dp[210][210][410];

int rs[4] = {0, 0, 1, 1}, re[4] = {0, 1, 0, 1};

int dfs(int sj, int ej, int d) {
  if (sj > W || ej > W || d > H + W)
    return 0;
  if (dp[sj][ej][d] > 0) {
    return dp[sj][ej][d] - 1;
  }
  int res = 0;
  for (int i = 0; i < 4; i++) {
    res = max(res, dfs(sj + rs[i], ej + re[i], d + 1));
  }
  res += field[d - sj][sj] + field[d - ej][ej];
  if (sj == ej)
    res -= field[d - sj][sj];
  dp[sj][ej][d] = res + 1;
  return res;
}

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> field[i][j];
    }
  }
  cout << dfs(0, 0, 0) << endl;

  return 0;
}


Fixed submission:
/*
 * 16-11-20_chu2_D.cpp
 *
 *  Created on: 2016/11/29
 *      Author: ryoma
 */
#include <algorithm>
#include <iostream>

using namespace std;

int field[210][210];
int H, W;

int dp[210][210][410];

int rs[4] = {0, 0, 1, 1}, re[4] = {0, 1, 0, 1};

int dfs(int sj, int ej, int d) {
  if (sj > W || ej > W || d - sj > H || d - ej > H || d > H + W)
    return 0;
  if (dp[sj][ej][d] > 0) {
    return dp[sj][ej][d] - 1;
  }
  int res = 0;
  for (int i = 0; i < 4; i++) {
    res = max(res, dfs(sj + rs[i], ej + re[i], d + 1));
  }
  res += field[d - sj][sj] + field[d - ej][ej];
  if (sj == ej)
    res -= field[d - sj][sj];
  dp[sj][ej][d] = res + 1;
  return res;
}

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> field[i][j];
    }
  }
  cout << dfs(0, 0, 0) << endl;

  return 0;
}