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

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

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define debug(x) cerr << #x << ": " << x << endl

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;

const ll BASE = 100001LL;

int n;
ll threshold;
vector<ll> a;

class BIT {
public:
  int n;
  vector<ll> bit;

  BIT(int n) : n(n) {
    bit.resize(n);
    fill(all(bit), 0LL);
  }

  void add(int idx, ll x) {
    for (int i = idx; i <= n; i += i & -i)
      bit[i] += x;
  }

  ll sum(int end) {
    ll ret = 0LL;
    for (int i = end; i >= 1; i -= i & -i)
      ret += bit[i];
    return ret;
  }
};

bool judge(ll median) {
  vector<ll> b(n + 1);
  b[0] = BASE;
  reppp(i, 1, n + 1) { b[i] = b[i - 1] + (a[i - 1] >= median ? 1LL : -1LL); }

  ll cnt = 0LL;
  BIT bit = BIT(BASE + n);
  bit.add(b[0], 1LL);
  reppp(i, 1, n + 1) {
    cnt += bit.sum(b[i]);
    bit.add(b[i], 1LL);
  }
  return cnt >= threshold;
}

int main() {
  cin >> n;
  a.resize(n);
  rep(n) cin >> a[i];

  threshold = ll(n) * ll(n + 1) / 2LL;
  threshold = (threshold + 1LL) / 2LL;

  vector<ll> aa(a);
  sort(all(aa));
  int ok = 0; // 中央値はok以上
  int ng = n; // 中央値はng未満
  while (ng - ok > 1) {
    int mid = (ng + ok) / 2;

    if (judge(aa[mid])) {
      ok = mid;
    } else {
      ng = mid;
    }
  }

  cout << aa[ok] << endl;
}

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

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define debug(x) cerr << #x << ": " << x << endl

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;

const ll BASE = 200000LL;

int n;
ll threshold;
vector<ll> a;

class BIT {
public:
  int n;
  vector<ll> bit;

  BIT(int n) : n(n) {
    bit.resize(n);
    fill(all(bit), 0LL);
  }

  void add(int idx, ll x) {
    for (int i = idx; i <= n; i += i & -i)
      bit[i] += x;
  }

  ll sum(int end) {
    ll ret = 0LL;
    for (int i = end; i >= 1; i -= i & -i)
      ret += bit[i];
    return ret;
  }
};

bool judge(ll median) {
  vector<ll> b(n + 1);
  b[0] = BASE;
  reppp(i, 1, n + 1) { b[i] = b[i - 1] + (a[i - 1] >= median ? 1LL : -1LL); }

  ll cnt = 0LL;
  BIT bit = BIT(BASE + n);
  bit.add(b[0], 1LL);
  reppp(i, 1, n + 1) {
    cnt += bit.sum(b[i]);
    bit.add(b[i], 1LL);
  }
  return cnt >= threshold;
}

int main() {
  cin >> n;
  a.resize(n);
  rep(n) cin >> a[i];

  threshold = ll(n) * ll(n + 1) / 2LL;
  threshold = (threshold + 1LL) / 2LL;

  vector<ll> aa(a);
  sort(all(aa));
  int ok = 0; // 中央値はok以上
  int ng = n; // 中央値はng未満
  while (ng - ok > 1) {
    int mid = (ng + ok) / 2;

    if (judge(aa[mid])) {
      ok = mid;
    } else {
      ng = mid;
    }
  }

  cout << aa[ok] << endl;
}
Problem p03277 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 3e5 + 10;
int n, ans_fin, a[MAXN], f[MAXN], b[MAXN], s_max;
int tree[MAXN];
ll sum;
int lowbit(int x) { return x & (-x); }
void add(int x) {
  while (x <= s_max) {
    tree[x]++;
    x += lowbit(x);
  }
}
int find(int x) {
  int ans = 0;
  while (x) {
    ans += tree[x];
    x -= lowbit(x);
  }
  return ans;
}
int pan(int x) {
  for (int i = 1; i <= n; i++)
    if (a[i] <= x)
      f[i] = f[i - 1] + 1;
    else
      f[i] = f[i - 1];
  for (int i = 1; i <= n; i++)
    f[i] = f[i] * 2 - i + n + 1;
  memset(tree, 0, sizeof(tree));
  add(n + 1);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (ll)find(f[i] - 1);
    add(f[i]);
  }
  return ans >= sum;
}
void s(int l, int r) {
  if (l > r)
    return;
  int mid = (l + r) / 2;
  bool t = pan(b[mid]);
  if (t) {
    ans_fin = b[mid];
    s(l, mid - 1);
  } else {
    s(mid + 1, r);
  }
}
int main() {
  scanf("%d", &n);
  s_max = n * 3 + 1;
  sum = ((ll)n * (ll)(n - 1) / 2 + (ll)n) / 2 + 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++)
    s(1, n);
  printf("%d", ans_fin);
}

Fixed submission:
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 3e5 + 10;
int n, ans_fin, a[MAXN], f[MAXN], b[MAXN], s_max;
int tree[MAXN];
ll sum;
int lowbit(int x) { return x & (-x); }
void add(int x) {
  while (x <= s_max) {
    tree[x]++;
    x += lowbit(x);
  }
}
int find(int x) {
  int ans = 0;
  while (x) {
    ans += tree[x];
    x -= lowbit(x);
  }
  return ans;
}
int pan(int x) {
  for (int i = 1; i <= n; i++)
    if (a[i] <= x)
      f[i] = f[i - 1] + 1;
    else
      f[i] = f[i - 1];
  for (int i = 1; i <= n; i++)
    f[i] = f[i] * 2 - i + n + 1;
  memset(tree, 0, sizeof(tree));
  add(n + 1);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (ll)find(f[i] - 1);
    add(f[i]);
  }
  return ans >= sum;
}
void s(int l, int r) {
  if (l > r)
    return;
  int mid = (l + r) / 2;
  bool t = pan(b[mid]);
  if (t) {
    ans_fin = b[mid];
    s(l, mid - 1);
  } else {
    s(mid + 1, r);
  }
}
int main() {
  scanf("%d", &n);
  s_max = n * 3 + 1;
  sum = ((ll)n * (ll)(n - 1) / 2 + (ll)n) / 2 + 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  s(1, n);
  printf("%d", ans_fin);
}
Problem p03277 (C++)

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

struct BIT {
  int size;
  vector<ll> t;
  void re() {
    for (int i = 0; i < (int)t.size(); i++)
      t[i] = 0;
  }
  void init(int n) {
    size = n;
    t.resize(n, 0);
  }

  void add(int i, ll x) {
    i++;
    while (i < size) {
      t[i] += x;
      i += (i & -i);
    }
  }

  ll sum(int i) {
    i++;
    ll res = 0;
    while (i) {
      res += t[i];
      i -= (i & -i);
    }
    return res;
  }
};

ll N;
ll a[100005];

ll b[100005];

BIT T;

ll Count(ll x) {
  T.re();
  for (int i = 0; i < N; i++)
    b[i] = (a[i] <= x ? 1 : -1);

  ll res = 0;
  ll sum = 150000;
  T.add(sum, 1);

  for (int i = 0; i < N; i++) {
    sum += b[i];
    res += T.sum(sum - 1);
    T.add(sum, 1);
  }
  return res;
}

int main() {

  T.init(200005);

  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i];

  ll total = (N + 1) * N / 2;
  ll target = total / 2 + 1;

  ll left = 1, right = 1e9, mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (Count(mid) < target)
      left = mid + 1;
    else
      right = mid;
  }

  cout << left << endl;
  return 0;
}


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

struct BIT {
  int size;
  vector<ll> t;
  void re() {
    for (int i = 0; i < (int)t.size(); i++)
      t[i] = 0;
  }
  void init(int n) {
    size = n;
    t.resize(n, 0);
  }

  void add(int i, ll x) {
    i++;
    while (i < size) {
      t[i] += x;
      i += (i & -i);
    }
  }

  ll sum(int i) {
    i++;
    ll res = 0;
    while (i) {
      res += t[i];
      i -= (i & -i);
    }
    return res;
  }
};

ll N;
ll a[100005];

ll b[100005];

BIT T;

ll Count(ll x) {
  T.re();
  for (int i = 0; i < N; i++)
    b[i] = (a[i] <= x ? 1 : -1);

  ll res = 0;
  ll sum = 150000;
  T.add(sum, 1);

  for (int i = 0; i < N; i++) {
    sum += b[i];
    res += T.sum(sum - 1);
    T.add(sum, 1);
  }
  return res;
}

int main() {

  T.init(300005);

  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i];

  ll total = (N + 1) * N / 2;
  ll target = total / 2 + 1;

  ll left = 1, right = 1e9, mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (Count(mid) < target)
      left = mid + 1;
    else
      right = mid;
  }

  cout << left << endl;
  return 0;
}
Problem p03277 (C++)

Buggy submission (status: Runtime Error):
// Comment
//

#include <bits/stdc++.h>

using namespace std;

#define SZ(v) ((int)(v).size())
#define ALL(v) (v).begin(), (v).end()
#define one first
#define two second
using ll = long long;
using pi = pair<int, int>;
const int INF = 0x3f3f3f3f;
const ll LINF = 1ll * INF * INF;

const int MAX_N = 1e5 + 100;
const int CONST = MAX_N;

struct BIT {
  int P;
  vector<int> val;
  BIT(int n) {
    P = n + CONST + 100;
    val = vector<int>(P, 0);
  }
  void update(int v, int k) {
    for (v += CONST; v < P; v += v & -v)
      val[v] += k;
  }
  int getSum(int v) {
    int res = 0;
    for (v += CONST; v > 0; v -= v & -v)
      res += val[v];
    return res;
  }
  int getSum(int a, int b) { return getSum(b) - getSum(a); }
};

vector<int> Xs;
int N, Nr[MAX_N];

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    scanf("%d", &Nr[i]);
    Xs.push_back(Nr[i]);
  }
  sort(ALL(Xs));
  Xs.erase(Xs.end(), unique(ALL(Xs)));
  int ans = -1;
  ll all = 1ll * N * (N + 1) / 2;
  for (int l = 0, r = SZ(Xs) - 1; l <= r;) {
    int m = (l + r) >> 1;
    int nr = Xs[m];

    int sum = 0;
    ll cnt = 0;
    BIT bit = BIT(MAX_N);
    bit.update(0, 1);

    for (int i = 0; i < N; i++) {
      int v = (Nr[i] >= nr) * 2 - 1;
      sum += v;
      cnt += bit.getSum(sum);
      bit.update(sum, 1);
    }
    if (cnt >= all - (all / 2 + 1) + 1) {
      ans = m;
      l = m + 1;
    } else {
      r = m - 1;
    }
  }
  printf("%d\n", Xs[ans]);
  return 0;
}


Fixed submission:
// Comment
//

#include <bits/stdc++.h>

using namespace std;

#define SZ(v) ((int)(v).size())
#define ALL(v) (v).begin(), (v).end()
#define one first
#define two second
using ll = long long;
using pi = pair<int, int>;
const int INF = 0x3f3f3f3f;
const ll LINF = 1ll * INF * INF;

const int MAX_N = 1e5 + 100;
const int CONST = MAX_N;

struct BIT {
  int P;
  vector<int> val;
  BIT(int n) {
    P = n + CONST + 100;
    val = vector<int>(P, 0);
  }
  void update(int v, int k) {
    for (v += CONST; v < P; v += v & -v)
      val[v] += k;
  }
  int getSum(int v) {
    int res = 0;
    for (v += CONST; v > 0; v -= v & -v)
      res += val[v];
    return res;
  }
  int getSum(int a, int b) { return getSum(b) - getSum(a); }
};

vector<int> Xs;
int N, Nr[MAX_N];

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    scanf("%d", &Nr[i]);
    Xs.push_back(Nr[i]);
  }
  sort(ALL(Xs));
  Xs.erase(unique(ALL(Xs)), Xs.end());
  int ans = -1;
  ll all = 1ll * N * (N + 1) / 2;
  for (int l = 0, r = SZ(Xs) - 1; l <= r;) {
    int m = (l + r) >> 1;
    int nr = Xs[m];

    int sum = 0;
    ll cnt = 0;
    BIT bit = BIT(MAX_N);
    bit.update(0, 1);

    for (int i = 0; i < N; i++) {
      int v = (Nr[i] >= nr) * 2 - 1;
      sum += v;
      cnt += bit.getSum(sum);
      bit.update(sum, 1);
    }
    if (cnt >= all - (all / 2 + 1) + 1) {
      ans = m;
      l = m + 1;
    } else {
      r = m - 1;
    }
  }
  printf("%d\n", Xs[ans]);
  return 0;
}
Problem p03277 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MN 100000
int a[MN + 5], s[MN + 5], S[MN * 3 + 5];
inline void add(int x) {
  for (; x <= 3 * MN; x += x & -x)
    ++S[x];
}
inline int sum(int x) {
  int r = 0;
  for (; x; x -= x & -x)
    r += S[x];
  return r;
}
int main() {
  int n, i, l, r, mid, ans;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (l = 1, r = 1e9; l <= r;) {
    mid = l + r >> 1;
    long long ss = 0;
    memset(S, 0, sizeof(S));
    add(MN);
    for (i = 1; i <= n; ++i)
      s[i] = s[i - 1] + 2 * (a[i] <= mid) - 1, ss += sum(s[i] + MN - 1),
      add(s[i] + MN);
    if (ss >= 1LL * n * (n + 1) / 4 + 1)
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MN 100001
int a[MN + 5], s[MN + 5], S[MN * 3 + 5];
inline void add(int x) {
  for (; x <= 3 * MN; x += x & -x)
    ++S[x];
}
inline int sum(int x) {
  int r = 0;
  for (; x; x -= x & -x)
    r += S[x];
  return r;
}
int main() {
  int n, i, l, r, mid, ans;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (l = 1, r = 1e9; l <= r;) {
    mid = l + r >> 1;
    long long ss = 0;
    memset(S, 0, sizeof(S));
    add(MN);
    for (i = 1; i <= n; ++i)
      s[i] = s[i - 1] + 2 * (a[i] <= mid) - 1, ss += sum(s[i] + MN - 1),
      add(s[i] + MN);
    if (ss >= 1LL * n * (n + 1) / 4 + 1)
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d", ans);
}
Problem p03278 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
const ll N = 4100, XJQ = 1e9 + 7;
struct node {
  ll to, next;
} a[N * 2];
ll n, tot, ls[N], siz[N], f[N][N], ans, g[N], fac[N];
void addl(ll x, ll y) {
  a[++tot].to = y;
  a[tot].next = ls[x];
  ls[x] = tot;
}
void dp(ll x, ll fa) {
  siz[x] = f[x][1] = 1;
  for (ll i = ls[x]; i; i = a[i].next) {
    ll y = a[i].to;
    if (y == fa)
      continue;
    dp(y, x);
    for (ll i = 1; i <= siz[x]; i++)
      g[i] = f[x][i], f[x][i] = 0;
    for (ll j = 1; j <= siz[x]; j++) {
      for (ll k = 1; k <= siz[y]; k++) {
        (f[x][j + k] += f[y][k] * g[j] % XJQ) %= XJQ;
        if ((k & 1) == 0)
          (f[x][j] += XJQ - f[y][k] * g[j] % XJQ * fac[k - 1] % XJQ) %= XJQ;
      }
    }
    siz[x] += siz[y];
  }
  return;
}
int main() {
  scanf("%lld", &n);
  for (ll i = 1; i < n; i++) {
    ll x, y;
    scanf("%lld%lld", &x, &y);
    addl(x, y);
    addl(y, x);
  }
  fac[0] = fac[1] = 1;
  for (ll i = 2; i <= n; i++)
    fac[i] = fac[i - 2] * i % XJQ;
  dp(1, 1);
  for (ll i = 2; i <= n; i++)
    ans = (ans + f[1][i] * fac[i - 1] % XJQ) % XJQ;
  printf("%lld", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
const ll N = 5100, XJQ = 1e9 + 7;
struct node {
  ll to, next;
} a[N * 2];
ll n, tot, ls[N], siz[N], f[N][N], ans, g[N], fac[N];
void addl(ll x, ll y) {
  a[++tot].to = y;
  a[tot].next = ls[x];
  ls[x] = tot;
}
void dp(ll x, ll fa) {
  siz[x] = f[x][1] = 1;
  for (ll i = ls[x]; i; i = a[i].next) {
    ll y = a[i].to;
    if (y == fa)
      continue;
    dp(y, x);
    for (ll i = 1; i <= siz[x]; i++)
      g[i] = f[x][i], f[x][i] = 0;
    for (ll j = 1; j <= siz[x]; j++) {
      for (ll k = 1; k <= siz[y]; k++) {
        (f[x][j + k] += f[y][k] * g[j] % XJQ) %= XJQ;
        if ((k & 1) == 0)
          (f[x][j] += XJQ - f[y][k] * g[j] % XJQ * fac[k - 1] % XJQ) %= XJQ;
      }
    }
    siz[x] += siz[y];
  }
  return;
}
int main() {
  scanf("%lld", &n);
  for (ll i = 1; i < n; i++) {
    ll x, y;
    scanf("%lld%lld", &x, &y);
    addl(x, y);
    addl(y, x);
  }
  fac[0] = fac[1] = 1;
  for (ll i = 2; i <= n; i++)
    fac[i] = fac[i - 2] * i % XJQ;
  dp(1, 1);
  for (ll i = 2; i <= n; i++)
    ans = (ans + f[1][i] * fac[i - 1] % XJQ) % XJQ;
  printf("%lld", ans);
}
Problem p03278 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 5010
#define mod 1000000007
inline int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch))
    ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return x;
}
int tot = 1;
int fir[N], nex[N], got[N];
inline void AddEdge(int x, int y) {
  nex[++tot] = fir[x], fir[x] = tot, got[tot] = y;
}
int siz[N], g[N];
int f[N][N], _[N];
inline void dfs(int x, int fa) {
  f[x][siz[x] = 1] = 1;
  for (int i = fir[x]; i; i = nex[i]) {
    int y = got[i];
    if (y == fa)
      continue;
    dfs(y, x);
    for (int j = 1; j <= siz[x] + siz[y]; j++)
      _[j] = 0;
    for (int j = 1; j <= siz[x]; j++) {
      for (int k = 0; k <= siz[y]; k++) {
        (_[j + k] += 1LL * f[x][j] * f[y][k] % mod) %= mod;
      }
    }
    for (int j = 1; j <= siz[x] + siz[y]; j++)
      f[x][j] = _[j];
    siz[x] += siz[y];
  }
  for (int i = 2; i <= siz[x]; i += 2) {
    (f[x][0] += mod - 1LL * f[x][i] * g[i] % mod) %= mod;
  }
}
int main() {
  int n = read();
  for (int i = 1; i <= n - 1; i++) {
    int x = read(), y = read();
    AddEdge(x, y), AddEdge(y, x);
  }
  g[0] = 1;
  for (int i = 2; i <= n; i += 2) {
    g[i] = 1LL * g[i - 2] * (i - 1) % mod;
  }
  dfs(1, 0);
  cout << mod - f[1][0] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 5010
#define mod 1000000007
inline int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch))
    ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return x;
}
int tot = 1;
int fir[N << 1], nex[N << 1], got[N << 1];
inline void AddEdge(int x, int y) {
  nex[++tot] = fir[x], fir[x] = tot, got[tot] = y;
}
int siz[N], g[N];
int f[N][N], _[N];
inline void dfs(int x, int fa) {
  f[x][siz[x] = 1] = 1;
  for (int i = fir[x]; i; i = nex[i]) {
    int y = got[i];
    if (y == fa)
      continue;
    dfs(y, x);
    for (int j = 1; j <= siz[x] + siz[y]; j++)
      _[j] = 0;
    for (int j = 1; j <= siz[x]; j++) {
      for (int k = 0; k <= siz[y]; k++) {
        (_[j + k] += 1LL * f[x][j] * f[y][k] % mod) %= mod;
      }
    }
    for (int j = 1; j <= siz[x] + siz[y]; j++)
      f[x][j] = _[j];
    siz[x] += siz[y];
  }
  for (int i = 2; i <= siz[x]; i += 2) {
    (f[x][0] += mod - 1LL * f[x][i] * g[i] % mod) %= mod;
  }
}
int main() {
  int n = read();
  for (int i = 1; i <= n - 1; i++) {
    int x = read(), y = read();
    AddEdge(x, y), AddEdge(y, x);
  }
  g[0] = 1;
  for (int i = 2; i <= n; i += 2) {
    g[i] = 1LL * g[i - 2] * (i - 1) % mod;
  }
  dfs(1, 0);
  cout << mod - f[1][0] << endl;
  return 0;
}
Problem p03278 (C++)

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

using namespace std;

const int N = 3e3 + 5;
const int mod = 1e9 + 7;
int _w;

int pls(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int mns(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }

int f[N][N], g[N], h[N], n, head[N], eidx, siz[N];
struct Edge {
  int nxt, to;
} edge[N << 1];

void addedge(int u, int v) {
  edge[++eidx] = (Edge){head[u], v};
  head[u] = eidx;
  edge[++eidx] = (Edge){head[v], u};
  head[v] = eidx;
}

void dfs(int u, int fa = 0) {
  siz[u] = 1, f[u][1] = 1;
  for (int e = head[u], v; e; e = edge[e].nxt)
    if ((v = edge[e].to) != fa) {
      dfs(v, u);
      for (int i = 0; i <= siz[u]; ++i)
        for (int j = 0; j <= siz[v]; ++j)
          g[i + j] = pls(g[i + j], 1LL * f[u][i] * f[v][j] % mod);
      siz[u] += siz[v];
      for (int i = 0; i <= siz[u]; ++i)
        f[u][i] = g[i], g[i] = 0;
    }
  int tmp = 0;
  for (int i = 0; i <= siz[u]; i += 2)
    tmp = mns(tmp, 1LL * h[i] * f[u][i] % mod);
  f[u][0] = tmp;
}

int main(void) {
  _w = scanf("%d", &n);
  for (int i = 1, u, v; i < n; ++i) {
    _w = scanf("%d%d", &u, &v);
    addedge(u, v);
  }
  h[0] = 1;
  for (int i = 2; i <= n; i += 2)
    h[i] = 1LL * (i - 1) * h[i - 2] % mod;
  dfs(1);
  cout << mod - f[1][0] << endl;
  return 0;
}


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

using namespace std;

const int N = 5e3 + 5;
const int mod = 1e9 + 7;
int _w;

int pls(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int mns(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }

int f[N][N], g[N], h[N], n, head[N], eidx, siz[N];
struct Edge {
  int nxt, to;
} edge[N << 1];

void addedge(int u, int v) {
  edge[++eidx] = (Edge){head[u], v};
  head[u] = eidx;
  edge[++eidx] = (Edge){head[v], u};
  head[v] = eidx;
}

void dfs(int u, int fa = 0) {
  siz[u] = 1, f[u][1] = 1;
  for (int e = head[u], v; e; e = edge[e].nxt)
    if ((v = edge[e].to) != fa) {
      dfs(v, u);
      for (int i = 0; i <= siz[u]; ++i)
        for (int j = 0; j <= siz[v]; ++j)
          g[i + j] = pls(g[i + j], 1LL * f[u][i] * f[v][j] % mod);
      siz[u] += siz[v];
      for (int i = 0; i <= siz[u]; ++i)
        f[u][i] = g[i], g[i] = 0;
    }
  int tmp = 0;
  for (int i = 0; i <= siz[u]; i += 2)
    tmp = mns(tmp, 1LL * h[i] * f[u][i] % mod);
  f[u][0] = tmp;
}

int main(void) {
  _w = scanf("%d", &n);
  for (int i = 1, u, v; i < n; ++i) {
    _w = scanf("%d%d", &u, &v);
    addedge(u, v);
  }
  h[0] = 1;
  for (int i = 2; i <= n; i += 2)
    h[i] = 1LL * (i - 1) * h[i - 2] % mod;
  dfs(1);
  cout << mod - f[1][0] << endl;
  return 0;
}
Problem p03278 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 5003, M = 1000000007;
int n, to[N * 2], hd[N * 2], lk[N], cnt, f[N][N], sz[N], co[N];
inline void add(int u, int v) { to[++cnt] = v, hd[cnt] = lk[u], lk[u] = cnt; }
int u, v;
void dfs(int x, int y) {
  f[x][1] = sz[x] = 1;
  for (int s, i = lk[x]; i; i = hd[i])
    if ((s = to[i]) ^ y) {
      dfs(s, x);
      for (int j = sz[x] + sz[s]; j; j--) {
        f[x][j] = (M - 1ll * f[x][j] * f[s][0]) % M;
        for (int k = 1; k <= sz[s] && k <= j; k++)
          f[x][j] = (f[x][j] + 1ll * f[x][j - k] * f[s][k]) % M;
      }
      sz[x] += sz[s];
    }
  for (int i = 2; i <= sz[x]; i += 2)
    f[x][0] = (f[x][0] + 1ll * f[x][i] * co[i]) % M;
}
int main() {
  scanf("%d", &n);
  for (int i = 2; i <= n; i += 2) {
    co[i] = 1;
    for (int j = i; j > i >> 1; j--)
      co[i] = 1ll * co[i] * j % M * (M + 1 >> 1) % M;
  }
  for (int i = 1; i < n; i++)
    scanf("%d%d", &u, &v), add(u, v), add(v, u);
  dfs(1, 0);
  printf("%d", f[1][0]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 5003, M = 1000000007;
int n, to[N * 2], hd[N * 2], lk[N], cnt, f[N][N], sz[N], co[N];
inline void add(int u, int v) { to[++cnt] = v, hd[cnt] = lk[u], lk[u] = cnt; }
int u, v;
void dfs(int x, int y) {
  f[x][1] = sz[x] = 1;
  for (int s, i = lk[x]; i; i = hd[i])
    if ((s = to[i]) ^ y) {
      dfs(s, x);
      for (int j = sz[x] + sz[s]; j; j--) {
        f[x][j] = (M - 1ll * f[x][j] * f[s][0]) % M;
        for (int k = max(j - sz[x], 1); k <= sz[s] && k <= j; k++)
          f[x][j] = (f[x][j] + 1ll * f[x][j - k] * f[s][k]) % M;
      }
      sz[x] += sz[s];
    }
  for (int i = 2; i <= sz[x]; i += 2)
    f[x][0] = (f[x][0] + 1ll * f[x][i] * co[i]) % M;
}
int main() {
  scanf("%d", &n);
  for (int i = 2; i <= n; i += 2) {
    co[i] = 1;
    for (int j = i; j > i >> 1; j--)
      co[i] = 1ll * co[i] * j % M * (M + 1 >> 1) % M;
  }
  for (int i = 1; i < n; i++)
    scanf("%d%d", &u, &v), add(u, v), add(v, u);
  dfs(1, 0);
  printf("%d", f[1][0]);
}
Problem p03278 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#define N 5005
#define p 1000000007
#define ll long long
int a[N], b[N], c[N], d[N], f[N][N], n, s[N], t;
void dfs(int u, int v) {
  f[u][s[u] = 1] = 1;
  for (int i = a[u]; i; i = b[i])
    if (c[i] != v) {
      dfs(c[i], u);
      for (int j = s[u]; j; f[u][j] = (ll)f[u][j] * *f[c[i]] % p, j--)
        for (int k = s[c[i]]; k; k--)
          f[u][j + k] = (f[u][j + k] + (ll)f[u][j] * f[c[i]][k]) % p;
      s[u] += s[c[i]];
    }
  for (int i = 2; i <= s[u]; i += 2)
    *f[u] = (*f[u] + (ll)f[u][i] * (p - d[i >> 1])) % p;
}
int main() {
  scanf("%d", &n), *d = d[1] = 1;
  for (int i = 2; i << 1 <= n; i++)
    d[i] = (i - 1 << 1 | 1ll) * d[i - 1] % p;
  for (int i = 2, u, v; i <= n; i++)
    scanf("%d%d", &u, &v), b[++t] = a[u], c[a[u] = t] = v, b[++t] = a[v],
                           c[a[v] = t] = u;
  dfs(1, 0), printf("%d\n", (p - *f[1]) % p);
  return 0;
}

Fixed submission:
#include <cstdio>
#define N 5005
#define p 1000000007
#define ll long long
int a[N], b[N << 1], c[N << 1], d[N >> 1], f[N][N], n, s[N], t;
void dfs(int u, int v) {
  f[u][s[u] = 1] = 1;
  for (int i = a[u]; i; i = b[i])
    if (c[i] != v) {
      dfs(c[i], u);
      for (int j = s[u]; j; f[u][j] = (ll)f[u][j] * *f[c[i]] % p, j--)
        for (int k = s[c[i]]; k; k--)
          f[u][j + k] = (f[u][j + k] + (ll)f[u][j] * f[c[i]][k]) % p;
      s[u] += s[c[i]];
    }
  for (int i = 2; i <= s[u]; i += 2)
    *f[u] = (*f[u] + (ll)f[u][i] * (p - d[i >> 1])) % p;
}
int main() {
  scanf("%d", &n), *d = d[1] = 1;
  for (int i = 2; i << 1 <= n; i++)
    d[i] = (i - 1 << 1 | 1ll) * d[i - 1] % p;
  for (int i = 2, u, v; i <= n; i++)
    scanf("%d%d", &u, &v), b[++t] = a[u], c[a[u] = t] = v, b[++t] = a[v],
                           c[a[v] = t] = u;
  dfs(1, 0), printf("%d\n", (p - *f[1]) % p);
  return 0;
}
Problem p03278 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define reg register
#define maxn 5005
#define mod 1000000007
using namespace std;
inline int read() {
  reg int s = 0, t = 0;
  reg char ch = getchar();
  while (ch > '9' || ch < '0')
    t |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9')
    s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
  return t ? -s : s;
}
int n, f[maxn], dp[maxn][maxn], siz[maxn];
int nt[maxn], go[maxn], fi[maxn], cnt;
void add(int u, int v) {
  nt[++cnt] = fi[u];
  fi[u] = cnt;
  go[cnt] = v;
}
void dfs(int u, int fa) {
  siz[u] = 1;
  dp[u][1] = 1;
  for (int i = fi[u]; i; i = nt[i]) {
    int v = go[i];
    if (v == fa)
      continue;
    dfs(v, u);
    for (int i = siz[u]; i; --i) {
      int sum = 0;
      for (int j = 1; j <= siz[v]; ++j) {
        dp[u][i + j] = (dp[u][i + j] + 1ll * dp[u][i] * dp[v][j] % mod) % mod;
        sum = (sum - 1ll * dp[v][j] * f[j] % mod + mod) % mod;
      }
      dp[u][i] = 1ll * sum * dp[u][i] % mod;
    }
    siz[u] += siz[v];
  }
}

int main() {
  n = read();
  f[0] = 1;
  for (int i = 2; i <= n; ++i)
    f[i] = 1ll * (i - 1) * f[i - 2] % mod;
  for (int i = 1, u, v; i < n; ++i) {
    int x = read(), y = read();
    add(x, y);
    add(y, x);
  }
  dfs(1, 0);
  int sum = 0;
  for (int i = 1; i <= n; ++i)
    sum = (sum + 1ll * dp[1][i] * f[i] % mod) % mod;
  printf("%d\n", (sum + mod) % mod);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define reg register
#define maxn 5005
#define mod 1000000007
using namespace std;
inline int read() {
  reg int s = 0, t = 0;
  reg char ch = getchar();
  while (ch > '9' || ch < '0')
    t |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9')
    s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
  return t ? -s : s;
}
int n, f[maxn], dp[maxn][maxn], siz[maxn];
int nt[maxn << 1], go[maxn << 1], fi[maxn], cnt;
void add(int u, int v) {
  nt[++cnt] = fi[u];
  fi[u] = cnt;
  go[cnt] = v;
}
void dfs(int u, int fa) {
  siz[u] = 1;
  dp[u][1] = 1;
  for (int i = fi[u]; i; i = nt[i]) {
    int v = go[i];
    if (v == fa)
      continue;
    dfs(v, u);
    for (int i = siz[u]; i; --i) {
      int sum = 0;
      for (int j = 1; j <= siz[v]; ++j) {
        dp[u][i + j] = (dp[u][i + j] + 1ll * dp[u][i] * dp[v][j] % mod) % mod;
        sum = (sum - 1ll * dp[v][j] * f[j] % mod + mod) % mod;
      }
      dp[u][i] = 1ll * sum * dp[u][i] % mod;
    }
    siz[u] += siz[v];
  }
}

int main() {
  n = read();
  f[0] = 1;
  for (int i = 2; i <= n; ++i)
    f[i] = 1ll * (i - 1) * f[i - 2] % mod;
  for (int i = 1, u, v; i < n; ++i) {
    int x = read(), y = read();
    add(x, y);
    add(y, x);
  }
  dfs(1, 0);
  int sum = 0;
  for (int i = 1; i <= n; ++i)
    sum = (sum + 1ll * dp[1][i] * f[i] % mod) % mod;
  printf("%d\n", (sum + mod) % mod);
  return 0;
}
Problem p03278 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long i64;
inline int read(int f = 1, int x = 0, char ch = ' ') {
  while (!isdigit(ch = getchar()))
    if (ch == '-')
      f = -1;
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f * x;
}
const int N = 5e3 + 5, P = 1e9 + 7;
int n, m, nxt[N], to[N], h[N], tot, s[N];
i64 f[N][N], g[N];
void add(int x, int y) { nxt[++tot] = h[x], to[tot] = y, h[x] = tot; }
void dfs(int x, int fa) {
  f[x][s[x] = 1] = 1;
  for (int i = h[x], y; i; i = nxt[i])
    if ((y = to[i]) != fa) {
      dfs(y, x);
      static i64 g[N];
      for (int i = 1; i <= s[x]; ++i)
        g[i] = f[x][i], f[x][i] = 0;
      for (int i = 1; i <= s[x]; ++i)
        for (int j = 0; j <= s[y]; ++j)
          f[x][i + j] = (f[x][i + j] + g[i] * f[y][j] % P) % P;
      s[x] += s[y];
    }
  for (int i = 1; i <= s[x]; ++i)
    (f[x][0] += P - f[x][i] * g[i] % P) %= P;
}
int main() {
  n = read();
  g[0] = 1;
  for (int i = 2; i <= n; i += 2)
    g[i] = g[i - 2] * (i - 1) % P;
  for (int i = 1, x, y; i < n; ++i)
    x = read(), y = read(), add(x, y), add(y, x);
  dfs(1, 0), printf("%lld\n", (P - f[1][0]) % P);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long i64;
inline int read(int f = 1, int x = 0, char ch = ' ') {
  while (!isdigit(ch = getchar()))
    if (ch == '-')
      f = -1;
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f * x;
}
const int N = 5e3 + 5, P = 1e9 + 7;
int n, m, nxt[N << 1], to[N << 1], h[N], tot, s[N];
i64 f[N][N], g[N];
void add(int x, int y) { nxt[++tot] = h[x], to[tot] = y, h[x] = tot; }
void dfs(int x, int fa) {
  f[x][s[x] = 1] = 1;
  for (int i = h[x], y; i; i = nxt[i])
    if ((y = to[i]) != fa) {
      dfs(y, x);
      static i64 g[N];
      for (int i = 1; i <= s[x]; ++i)
        g[i] = f[x][i], f[x][i] = 0;
      for (int i = 1; i <= s[x]; ++i)
        for (int j = 0; j <= s[y]; ++j)
          f[x][i + j] = (f[x][i + j] + g[i] * f[y][j] % P) % P;
      s[x] += s[y];
    }
  for (int i = 1; i <= s[x]; ++i)
    (f[x][0] += P - f[x][i] * g[i] % P) %= P;
}
int main() {
  n = read();
  g[0] = 1;
  for (int i = 2; i <= n; i += 2)
    g[i] = g[i - 2] * (i - 1) % P;
  for (int i = 1, x, y; i < n; ++i)
    x = read(), y = read(), add(x, y), add(y, x);
  dfs(1, 0), printf("%lld\n", (P - f[1][0]) % P);
  return 0;
}
Problem p03278 (C++)

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

using namespace std;

typedef long long ll;

const int MAX_N = 5e3 + 10;
const ll MOD = 1e9 + 7;

int N, siz[MAX_N];
ll dp[MAX_N][MAX_N], fac2[MAX_N];
vector<int> edge[MAX_N];

void solve(int x, int y) {
  dp[x][siz[x] = 1] = 1;
  for (int i : edge[x]) {
    if (i == y)
      continue;
    solve(i, x);
    for (int j = siz[x] + siz[i]; j; j--) {
      ll t = 0;
      for (int k = max(0, j - siz[x]); k <= max(siz[i], j); k++) {
        (t += dp[i][k] * dp[x][j - k]) %= MOD;
      }
      dp[x][j] = t;
    }
    siz[x] += siz[i];
  }
  for (int i = 1; i <= siz[x]; i++) {
    (dp[x][0] += MOD - dp[x][i] * fac2[i - 1] % MOD) %= MOD;
  }
}

int main() {
  scanf("%d", &N);
  fac2[1] = 1;
  for (int i = 3; i <= N; i += 2)
    fac2[i] = fac2[i - 2] * i % MOD;
  for (int i = 1; i < N; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  solve(1, 0);
  ll ans = (MOD - dp[1][0]) % MOD;
  printf("%lld\n", ans);
}

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

using namespace std;

typedef long long ll;

const int MAX_N = 5e3 + 10;
const ll MOD = 1e9 + 7;

int N, siz[MAX_N];
ll dp[MAX_N][MAX_N], fac2[MAX_N];
vector<int> edge[MAX_N];

void solve(int x, int y) {
  dp[x][siz[x] = 1] = 1;
  for (int i : edge[x]) {
    if (i == y)
      continue;
    solve(i, x);
    for (int j = siz[x] + siz[i]; j; j--) {
      ll t = 0;
      for (int k = max(0, j - siz[x]); k <= min(siz[i], j); k++) {
        (t += dp[i][k] * dp[x][j - k]) %= MOD;
      }
      dp[x][j] = t;
    }
    siz[x] += siz[i];
  }
  for (int i = 1; i <= siz[x]; i++) {
    (dp[x][0] += MOD - dp[x][i] * fac2[i - 1] % MOD) %= MOD;
  }
}

int main() {
  scanf("%d", &N);
  fac2[1] = 1;
  for (int i = 3; i <= N; i += 2)
    fac2[i] = fac2[i - 2] * i % MOD;
  for (int i = 1; i < N; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  solve(1, 0);
  ll ans = (MOD - dp[1][0]) % MOD;
  printf("%lld\n", ans);
}
Problem p03278 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;

constexpr ll MOD = (ll)(1e9 + 7);
constexpr ll MAX = 5000;

ll N, dp[MAX + 1][MAX + 1][2], dp2[MAX + 1][2];
vector<vector<ll>> e;
vector<ll> g, sz;

void rec(ll v, ll p) {
  sz[v] = 1;
  dp[v][1][0] = 1;
  for (ll i : e[v])
    if (i != p) {
      rec(i, v);
      memset(dp2, 0, sizeof(dp2));
      for (ll x = 1; x <= sz[v]; x++)
        for (ll y = 1; y <= sz[i]; y++) {
          // v-i間の辺を取り除く場合
          (dp2[x][0] += dp[v][x][0] * dp[i][y][1] % MOD * g[y] +
                        dp[v][x][1] * dp[i][y][0] % MOD * g[y]) %= MOD;
          (dp2[x][1] += dp[v][x][0] * dp[i][y][0] % MOD * g[y] +
                        dp[v][x][1] * dp[i][y][1] % MOD * g[y]) %= MOD;
          // 取り除かない場合
          (dp2[x + y][0] +=
           dp[v][x][0] * dp[i][y][0] + dp[v][x][1] * dp[i][y][1]) %= MOD;
          (dp2[x + y][1] +=
           dp[v][x][0] * dp[i][y][1] + dp[v][x][1] * dp[i][y][0]) %= MOD;
        }
      swap(dp[v], dp2);
      sz[v] += sz[i];
    }
}

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

  cin >> N;

  sz.resize(N, 0);

  g.resize(N + 1, 0);
  g[2] = 1;
  for (ll i = 4; i <= MAX; i++) {
    g[i] = g[i - 2] * (i - 1) % MOD;
  }

  e.resize(N + 1);
  for (ll i = 0; i < N - 1; i++) {
    ll x, y;
    cin >> x >> y;
    x--, y--;
    e[x].push_back(y), e[y].push_back(x);
  }

  rec(0, -1);

  ll ans = 0;
  for (ll i = 1; i <= N; i++) {
    (ans += (dp[0][i][0] - dp[0][i][1] + MOD) % MOD * g[i]) %= MOD;
  }
  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;

constexpr ll MOD = (ll)(1e9 + 7);
constexpr ll MAX = 5000;

ll N, dp[MAX + 1][MAX + 1][2], dp2[MAX + 1][2];
vector<vector<ll>> e;
vector<ll> g, sz;

void rec(ll v, ll p) {
  sz[v] = 1;
  dp[v][1][0] = 1;
  for (ll i : e[v])
    if (i != p) {
      rec(i, v);
      memset(dp2, 0, sizeof(dp2));
      for (ll x = 1; x <= sz[v]; x++)
        for (ll y = 1; y <= sz[i]; y++) {
          // v-i間の辺を取り除く場合
          (dp2[x][0] += dp[v][x][0] * dp[i][y][1] % MOD * g[y] +
                        dp[v][x][1] * dp[i][y][0] % MOD * g[y]) %= MOD;
          (dp2[x][1] += dp[v][x][0] * dp[i][y][0] % MOD * g[y] +
                        dp[v][x][1] * dp[i][y][1] % MOD * g[y]) %= MOD;
          // 取り除かない場合
          (dp2[x + y][0] +=
           dp[v][x][0] * dp[i][y][0] + dp[v][x][1] * dp[i][y][1]) %= MOD;
          (dp2[x + y][1] +=
           dp[v][x][0] * dp[i][y][1] + dp[v][x][1] * dp[i][y][0]) %= MOD;
        }
      swap(dp[v], dp2);
      sz[v] += sz[i];
    }
}

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

  cin >> N;

  sz.resize(N, 0);

  g.resize(N + 1, 0);
  g[2] = 1;
  for (ll i = 4; i <= N; i++) {
    g[i] = g[i - 2] * (i - 1) % MOD;
  }

  e.resize(N + 1);
  for (ll i = 0; i < N - 1; i++) {
    ll x, y;
    cin >> x >> y;
    x--, y--;
    e[x].push_back(y), e[y].push_back(x);
  }

  rec(0, -1);

  ll ans = 0;
  for (ll i = 1; i <= N; i++) {
    (ans += (dp[0][i][0] - dp[0][i][1] + MOD) % MOD * g[i]) %= MOD;
  }
  cout << ans << endl;
}
Problem p03279 (C++)

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

const int64_t MOD = 1e9 + 7;
void add_mod(int64_t &a, int64_t b) { a = (a + b) % MOD; }

template <typename T> struct BIT {
  int n;
  vector<T> dat;

  BIT(int n = 0) : n(n) { initialize(n); }

  void initialize(int n) {
    dat.resize(n);
    for (int i = 0; i < n; i++)
      dat[i] = 0;
  }

  T sum(int i) {
    T s = 0;
    while (i >= 0) {
      add_mod(s, dat[i]);
      i = (i & (i + 1)) - 1;
    }
    return s;
  }

  void add(int i, T x) {
    while (i <= n) {
      add_mod(dat[i], x);
      i |= i + 1;
    }
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> X(N), Y(M);
  for (int i = 0; i < N; i++)
    cin >> X[i];
  for (int i = 0; i < M; i++)
    cin >> Y[i];

  vector<pair<int, int>> pts;
  vector<int> ys = {0};
  for (int i = 0, j = 0; i < N; i++) {
    if (X[i] < Y[0] || Y[M - 1] < X[i])
      continue;
    while (Y[j] < X[i])
      j++;
    pts.push_back({X[i] - Y[j - 1], -(Y[j] - X[i])});
    ys.push_back(Y[j] - X[i]);
  }
  if (pts.size() == 0) {
    cout << 1 << endl;
    return 0;
  }

  sort(pts.begin(), pts.end());
  sort(ys.begin(), ys.end());
  pts.erase(unique(pts.begin(), pts.end()), pts.end());
  ys.erase(unique(ys.begin(), ys.end()), ys.end());

  BIT<int64_t> bit(ys.size());
  bit.add(0, 1);

  for (auto p : pts) {
    int y = -p.second;
    int i = lower_bound(ys.begin(), ys.end(), y) - ys.begin();
    if (i > 0)
      bit.add(i, bit.sum(i - 1));
  }
  cout << bit.sum(ys.size() - 1) << endl;
  return 0;
}

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

const int64_t MOD = 1e9 + 7;
void add_mod(int64_t &a, int64_t b) { a = (a + b) % MOD; }

template <typename T> struct BIT {
  int n;
  vector<T> dat;

  BIT(int n = 0) : n(n) { initialize(n); }

  void initialize(int n) {
    dat.resize(n);
    for (int i = 0; i < n; i++)
      dat[i] = 0;
  }

  T sum(int i) {
    T s = 0;
    while (i >= 0) {
      add_mod(s, dat[i]);
      i = (i & (i + 1)) - 1;
    }
    return s;
  }

  void add(int i, T x) {
    while (i < n) {
      add_mod(dat[i], x);
      i |= i + 1;
    }
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> X(N), Y(M);
  for (int i = 0; i < N; i++)
    cin >> X[i];
  for (int i = 0; i < M; i++)
    cin >> Y[i];

  vector<pair<int, int>> pts;
  vector<int> ys = {0};
  for (int i = 0, j = 0; i < N; i++) {
    if (X[i] < Y[0] || Y[M - 1] < X[i])
      continue;
    while (Y[j] < X[i])
      j++;
    pts.push_back({X[i] - Y[j - 1], -(Y[j] - X[i])});
    ys.push_back(Y[j] - X[i]);
  }
  if (pts.size() == 0) {
    cout << 1 << endl;
    return 0;
  }

  sort(pts.begin(), pts.end());
  sort(ys.begin(), ys.end());
  pts.erase(unique(pts.begin(), pts.end()), pts.end());
  ys.erase(unique(ys.begin(), ys.end()), ys.end());

  BIT<int64_t> bit(ys.size());
  bit.add(0, 1);

  for (auto p : pts) {
    int y = -p.second;
    int i = lower_bound(ys.begin(), ys.end(), y) - ys.begin();
    if (i > 0)
      bit.add(i, bit.sum(i - 1));
  }
  cout << bit.sum(ys.size() - 1) << endl;
  return 0;
}
Problem p03280 (Python)

Buggy submission (status: Runtime Error):
a = int(input())
b = int(input())

ans = (a - 1) * (b - 1)
print(ans)


Fixed submission:
a, b = map(int, input().split())

ans = (a - 1) * (b - 1)
print(ans)
Problem p03280 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main() {
  int a, b;
  scanf("%d %d", a, b);
  printf("%d\n", (a - 1) * (b - 1));
  return 0;
}

Fixed submission:
#include <stdio.h>
int main() {
  int a, b;
  scanf("%d %d", &a, &b);
  printf("%d\n", (a - 1) * (b - 1));
  return 0;
}
Problem p03280 (C++)

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

int main() {
  int a, b;
  cin >> a >> b;

  return (a - 1) * (b - 1);

  return 0;
}

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

int main() {
  int a, b;
  cin >> a >> b;

  cout << (a - 1) * (b - 1) << endl;

  return 0;
}
Problem p03280 (Python)

Buggy submission (status: Runtime Error):
a = int(input())
b = int(input())

total = a * b - (a + b - 1)

print(total)


Fixed submission:
a, b = list(map(int, input().split()))

total = a * b - (a + b - 1)

print(total)
Problem p03281 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define ifaxb(a, x, b) if (a < x && x < b)
#define toInt(x) (x - '0')
#define vi vector<int>
#define vvi vector<vi>
#define vs vector<string>
#define vvs vector<vs>
#define all(x) x.begin(), x.end()
#define INF 2147483647
#define _INF -2147483647
#define pi 3.1415926536
#define MAXN 100004
using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  int ans = 0;
  for (int C = 1; C <= N; C += 2) {
    int div = 0;
    rep(X, C + 1) {
      if (C % X == 0) {
        div++;
      }
    }
    if (div == 8) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define ifaxb(a, x, b) if (a < x && x < b)
#define toInt(x) (x - '0')
#define vi vector<int>
#define vvi vector<vi>
#define vs vector<string>
#define vvs vector<vs>
#define all(x) x.begin(), x.end()
#define INF 2147483647
#define _INF -2147483647
#define pi 3.1415926536
#define MAXN 100004
using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  int ans = 0;
  for (int C = 1; C <= N; C += 2) {
    int div = 0;
    for (int X = 1; X <= C; ++X) {
      if (C % X == 0) {
        div++;
      }
    }
    if (div == 8) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03281 (C++)

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

int main() {
  int N;
  cin >> N;
  int ans = 0;
  for (int i = 1; i <= N; i += 2) {
    int sum = 0;
    for (int j = 1; j <= sqrt(i); i += 2) {
      if (i % j == 0) {
        sum++;
      }
    }
    if (sum == 8) {
      ans++;
    }
  }

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

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

int main() {
  int N;
  cin >> N;
  int ans = 0;
  for (int i = 1; i <= N; i += 2) {
    int sum = 0;
    for (int j = 1; j <= i; j += 2) {
      if (i % j == 0) {
        sum++;
      }
    }
    if (sum == 8) {
      ans++;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p03281 (C++)

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

int main() {

  int N;
  cin >> N;
  int ans = 0;
  for (int i = 1; i <= N; i += 2) {
    int sum = 0;
    for (int j = 1; j <= i; i += 2) {
      if (i % j == 0)
        sum++;
    }
    if (sum == 8)
      ans++;
  }

  cout << ans;
  return 0;
}

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

int main() {

  int N;
  cin >> N;
  int ans = 0;
  for (int i = 1; i <= N; i += 2) {
    int sum = 0;
    for (int j = 1; j <= i; j += 2) {
      if (i % j == 0)
        sum++;
    }
    if (sum == 8)
      ans++;
  }

  cout << ans;
  return 0;
}
Problem p03281 (C++)

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

int main() {
  int n;
  cin >> n;
  n++;
  int cnt = 0;
  for (int i = 0; (2 * i + 1) < n; i++) {
    int tmp = 0;
    for (int j = 0; j <= (2 * i + 1); j++) {
      if ((2 * i + 1) % j == 0)
        tmp++;
    }
    if (tmp == 8)
      cnt++;
  }
  cout << cnt << endl;
}


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

int main() {
  int n;
  cin >> n;
  n++;
  int cnt = 0;
  for (int i = 0; (2 * i + 1) < n; i++) {
    int tmp = 0;
    for (int j = 1; j <= (2 * i + 1); j++) {
      if ((2 * i + 1) % j == 0)
        tmp++;
    }
    if (tmp == 8)
      cnt++;
  }
  cout << cnt << endl;
}
Problem p03281 (C++)

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

int main() {
  int N;
  cin >> N;
  int ans = 0;
  for (int C = 1; C <= N; C += 2) {
    int yakusu = 0;
    for (int X = 0; X <= C; X++) {
      if (C % X == 0) {
        yakusu++;
      }
    }
    if (yakusu == 8) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


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

int main() {
  int N;
  cin >> N;
  int ans = 0;
  for (int C = 1; C <= N; C += 2) {
    int yakusu = 0;
    for (int X = 1; X <= C; X++) {
      if (C % X == 0) {
        yakusu++;
      }
    }
    if (yakusu == 8) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03281 (C++)

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

int main() {
  int N;
  cin >> N;
  int d = 0;

  for (int i = 1; i <= N; i++) {
    if (i % 2 == 0)
      continue;

    int dc = 0;
    for (int j = 1; i <= j; j++) {
      if (i % j == 0)
        dc++;
    }
    if (dc == 8)
      d++;
  }

  cout << d << endl;
}

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

int main() {
  int N;
  cin >> N;
  int d = 0;

  for (int i = 1; i <= N; i++) {
    if (i % 2 == 0)
      continue;

    int dc = 0;
    for (int j = 1; j <= i; j++) {
      if (i % j == 0)
        dc++;
    }
    if (dc == 8)
      d++;
  }

  cout << d << endl;
}
Problem p03281 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int main() {
  int n, s = 0, ans = 0;
  cin >> n;
  rep(i, (n + 1) / 2) {
    rep(j, n) {
      if ((2 * i + 1) % j == 0) {
        s++;
      }
    }
    if (s == 8) {
      ans++;
    }
    s = 0;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int main() {
  int n, s = 0, ans = 0;
  cin >> n;
  rep(i, ((n + 1) / 2)) {
    for (int j = 1; j <= n; j++) {
      if ((2 * i + 1) % j == 0) {
        s++;
      }
    }
    if (s == 8) {
      ans++;
    }
    s = 0;
  }
  cout << ans << endl;
}
Problem p03281 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define elif else if
#define INF 1000000007
using namespace std;
int a, b = 0, aa[110000], ab[110000], cnt = 0;
double d;
string s, ss;
signed main() {
  cin >> a;
  rep(i, a) {
    rep(j, i) {
      if (i % j == 0) {
        b++;
      }
    }
    if (b == 8) {
      cnt++;
    }
    b = 0;
  }
  cout << cnt;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define elif else if
#define INF 1000000007
using namespace std;
int a, b = 0, aa[110000], ab[110000], cnt = 0;
double d;
string s, ss;
signed main() {
  cin >> a;
  rep(i, (a + 1) / 2) {
    rep(j, i * 2 + 1) {
      if ((i * 2 + 1) % (j + 1) == 0) {
        b++;
      }
    }
    if (b == 8) {
      cnt++;
    }
    b = 0;
  }
  cout << cnt;
}
Problem p03281 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;

int main(void) {
  int n;
  cin >> n;
  int num = 0;
  int ans = 0;
  for (int i = 1; i <= n; n++) {
    num = 0;
    if (i % 2 == 1) {
      for (int j = 1; j <= i; j++) {
        if (i % j == 0) {
          num++;
        }
      }
    }
    if (num == 8) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;

int main(void) {
  int n;
  cin >> n;
  int num = 0;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    num = 0;
    if (i % 2 == 1) {
      for (int j = 1; j <= i; j++) {
        if (i % j == 0) {
          num++;
        }
      }
    }
    if (num == 8) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03281 (C++)

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

int yakusuu(int x) {
  int count = 0;
  int i = 1;
  while (i * i <= x) {
    if (x % i == 0) {
      if (i * i == x)
        count++;
      else
        count += 2;
    }
  }
  return count;
}

int main() {
  int N;
  cin >> N;
  int count = 0;
  for (int i = 1; i <= N; i++) {
    if (i % 2 == 0)
      continue;
    if (yakusuu(i) == 8)
      count++;
  }
  cout << count << endl;
}

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

int yakusuu(int x) {
  int count = 0;
  int i = 1;
  while (i * i <= x) {
    if (x % i == 0) {
      if (i * i == x)
        count++;
      else
        count += 2;
    }
    i++;
  }
  return count;
}

int main() {
  int N;
  cin >> N;
  int count = 0;
  for (int i = 1; i <= N; i++) {
    if (i % 2 == 0)
      continue;
    if (yakusuu(i) == 8)
      count++;
  }
  cout << count << endl;
}