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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
#define INF (1LL << 60)
pair<int, int> points[100000];

long long int dist(int a, int b) {
  long long int x = points[a].first - points[b].first;
  long long int y = points[a].second - points[b].second;
  return x * x + y * y;
}

long long int sq(int x) { return x * x; }

void sort_in_y(int l, int mid, int r) {
  pair<int, int> tmp[r - l];
  int lps = l, rps = mid, ps = 0;
  while (lps < mid || rps < r) {
    if (lps == mid || rps != r && points[lps].second > points[rps].second)
      tmp[ps++] = points[rps++];
    else
      tmp[ps++] = points[lps++];
  }
  for (int i = l; i < r; i++) {
    points[i] = tmp[i - l];
  }
}

long long int solve(int l, int r) {
  if (r - l <= 1)
    return INF;
  int mid = (l + r) / 2;
  int center = points[mid].first;
  long long int limit = min(solve(l, mid), solve(mid, r));
  sort_in_y(l, mid, r);
  vector<int> areas;
  for (int i = l; i < r; i++) {
    if (sq(points[i].first - center) >= limit)
      continue;
    for (int j = areas.size() - 1; j >= 0; j--) {
      if (sq(points[areas[j]].second - points[i].second) >= limit)
        break;
      limit = min(limit, dist(i, areas[j]));
    }
    areas.push_back(i);
  }
  return limit;
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &points[i].first, &points[i].second);
  }
  sort(points, points + n);
  printf("%lld\n", solve(0, n));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
#define INF (1LL << 60)
pair<int, int> points[1000000];

long long int dist(int a, int b) {
  long long int x = points[a].first - points[b].first;
  long long int y = points[a].second - points[b].second;
  return x * x + y * y;
}

long long int sq(int x) { return x * x; }

void sort_in_y(int l, int mid, int r) {
  pair<int, int> tmp[r - l];
  int lps = l, rps = mid, ps = 0;
  while (lps < mid || rps < r) {
    if (lps == mid || rps != r && points[lps].second > points[rps].second)
      tmp[ps++] = points[rps++];
    else
      tmp[ps++] = points[lps++];
  }
  for (int i = l; i < r; i++) {
    points[i] = tmp[i - l];
  }
}

long long int solve(int l, int r) {
  if (r - l <= 1)
    return INF;
  int mid = (l + r) / 2;
  int center = points[mid].first;
  long long int limit = min(solve(l, mid), solve(mid, r));
  sort_in_y(l, mid, r);
  vector<int> areas;
  for (int i = l; i < r; i++) {
    if (sq(points[i].first - center) >= limit)
      continue;
    for (int j = areas.size() - 1; j >= 0; j--) {
      if (sq(points[areas[j]].second - points[i].second) >= limit)
        break;
      limit = min(limit, dist(i, areas[j]));
    }
    areas.push_back(i);
  }
  return limit;
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &points[i].first, &points[i].second);
  }
  sort(points, points + n);
  printf("%lld\n", solve(0, n));
  return 0;
}
Problem p00508 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;

int N;
vector<P> a;

bool compare_y(P a, P b) { return a.second < b.second; }

// [l, r)
double closest_pair(int l, int r) {
  int n = r - l;
  if (r - l <= 1)
    return 2e9; // 1点しかない
  int m = (l + r) / 2;
  int x = a[m].first;
  int d = min(closest_pair(l, m), closest_pair(m, r));
  inplace_merge(
      a.begin() + r, a.begin() + m, a.begin() + r,
      compare_y); // 二つのソート済み配列をマージする 範囲指定->(先頭,中間,最後)

  vector<P> b;
  FOR(i, l, r) {
    if (abs(a[i].first - x) >= d)
      continue;
    FOR(j, 0, b.size()) {
      // 近いものから見て行く
      int dx = a[i].first - b[b.size() - j - 1].first;
      int dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break; // 枝狩り?
      d = min(d, dx * dx + dy * dy);
    }
    b.push_back(a[i]);
  }
  return d;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  a.resize(N);
  FOR(i, 0, N) cin >> a[i].first >> a[i].second;
  sort(a.begin(), a.end());
  cout << closest_pair(0, N) << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;

int N;
vector<P> a;

bool compare_y(P a, P b) { return a.second < b.second; }

// [l, r)
double closest_pair(int l, int r) {
  int n = r - l;
  if (r - l <= 1)
    return 2e9; // 1点しかない
  int m = (l + r) / 2;
  int x = a[m].first;
  int d = min(closest_pair(l, m), closest_pair(m, r));
  inplace_merge(
      a.begin() + l, a.begin() + m, a.begin() + r,
      compare_y); // 二つのソート済み配列をマージする 範囲指定->(先頭,中間,最後)

  vector<P> b;
  FOR(i, l, r) {
    if (abs(a[i].first - x) >= d)
      continue;
    FOR(j, 0, b.size()) {
      // 近いものから見て行く
      int dx = a[i].first - b[b.size() - j - 1].first;
      int dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break; // 枝狩り?
      d = min(d, dx * dx + dy * dy);
    }
    b.push_back(a[i]);
  }
  return d;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  a.resize(N);
  FOR(i, 0, N) cin >> a[i].first >> a[i].second;
  sort(a.begin(), a.end());
  cout << closest_pair(0, N) << endl;
}
Problem p00508 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
typedef pair<short, short> Pi;
#define y first
#define x second
#define INF (1 << 30)
#define SQR(x) ((x) * (x))
Pi p[500000];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%hd %hd", &p[i].x, &p[i].y);
  }
  sort(p, p + n);
  int ret = INF;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ret = min(ret, SQR(p[i].x - p[j].x) + SQR(p[i].y - p[j].y));
    }
  }
  printf("%d\n", ret);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
typedef pair<short, short> Pi;
#define y first
#define x second
#define INF (1 << 30)
#define SQR(x) ((x) * (x))
Pi p[500000];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%hd %hd", &p[i].x, &p[i].y);
  }
  sort(p, p + n);
  int ret = INF;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (SQR(p[i].y - p[j].y) >= ret)
        break;
      ret = min(ret, SQR(p[i].x - p[j].x) + SQR(p[i].y - p[j].y));
    }
  }
  printf("%d\n", ret);
}
Problem p00508 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;

int N;
vector<P> a;

bool compare_y(P a, P b) { return a.second < b.second; }

// [l, r)
double closest_pair(int l, int r) {
  int n = r - l;
  if (r - l <= 1)
    return 2e9; // 1点しかない
  int m = (l + r) / 2;
  int x = a[m].first;
  int d = min(closest_pair(l, m), closest_pair(m, r));
  inplace_merge(
      a.begin(), a.begin() + m, a.begin() + r,
      compare_y); // 二つのソート済み配列をマージする 範囲指定->(先頭,中間,最後)

  vector<P> b;
  FOR(i, l, r) {
    if (abs(a[i].first - x) >= d)
      continue;
    FOR(j, 0, b.size()) {
      // 近いものから見て行く
      int dx = a[i].first - b[b.size() - j - 1].first;
      int dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break; // 枝狩り?
      d = min(d, dx * dx + dy * dy);
    }
    b.push_back(a[i]);
  }
  return d;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  a.resize(N);
  FOR(i, 0, N) cin >> a[i].first >> a[i].second;
  sort(a.begin(), a.end());
  cout << closest_pair(0, N) << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;

int N;
vector<P> a;

bool compare_y(P a, P b) { return a.second < b.second; }

// [l, r)
double closest_pair(int l, int r) {
  int n = r - l;
  if (r - l <= 1)
    return 2e9; // 1点しかない
  int m = (l + r) / 2;
  int x = a[m].first;
  int d = min(closest_pair(l, m), closest_pair(m, r));
  inplace_merge(
      a.begin(), a.begin() + m, a.begin() + n,
      compare_y); // 二つのソート済み配列をマージする 範囲指定->(先頭,中間,最後)

  vector<P> b;
  FOR(i, l, r) {
    if (abs(a[i].first - x) >= d)
      continue;
    FOR(j, 0, b.size()) {
      // 近いものから見て行く
      int dx = a[i].first - b[b.size() - j - 1].first;
      int dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break; // 枝狩り?
      d = min(d, dx * dx + dy * dy);
    }
    b.push_back(a[i]);
  }
  return d;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  a.resize(N);
  FOR(i, 0, N) cin >> a[i].first >> a[i].second;
  sort(a.begin(), a.end());
  cout << closest_pair(0, N) << endl;
}
Problem p00508 (C++)

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

struct P {
  int x, y;
  P(int x_, int y_) {
    x = x_;
    y = y_;
  }
  P() {}
};
bool operator<(const P &p, const P &q) { return p.x < q.x; }
P operator-(const P &p, const P &q) { return P(p.x - q.x, p.y - q.y); }

int n, S[100000000];
P p[100000000];

int D(const P &p, const P &q) {
  return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y);
}
int D(const P &p) { return p.x * p.x + p.y * p.y; }

int solve() {
  int s = 0, t = 1, m = 2;
  S[0] = 0, S[1] = 1;
  sort(p, p + n);
  int d = D(p[s] - p[t]);
  for (int i = 2; i < n; S[m++] = i++) {
    for (int j = 0; j < m; j++) {
      if (D(p[S[j]] - p[i]) < d) {
        d = D(p[s = S[j]] - p[t = i]);
      }
      if (p[S[j]].x < p[i].x - d) {
        S[j--] = S[--m];
      }
    }
  }
  return D(p[s], p[t]);
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> p[i].x >> p[i].y;
  cout << solve() << endl;
}

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

struct P {
  int x, y;
  P(int x_, int y_) {
    x = x_;
    y = y_;
  }
  P() {}
};
bool operator<(const P &p, const P &q) { return p.x < q.x; }
P operator-(const P &p, const P &q) { return P(p.x - q.x, p.y - q.y); }

int n, S[100000000];
P p[100000000];

int D(const P &p, const P &q) {
  return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y);
}
int D(const P &p) { return p.x * p.x + p.y * p.y; }

int solve() {
  int s = 0, t = 1, m = 2;
  S[0] = 0, S[1] = 1;
  sort(p, p + n);
  int d = D(p[s] - p[t]);
  for (int i = 2; i < n; S[m++] = i++) {
    for (int j = 0; j < m; j++) {
      if (D(p[S[j]] - p[i]) < d) {
        d = D(p[s = S[j]] - p[t = i]);
      }
      if (p[S[j]].x < p[i].x - d) {
        S[j--] = S[--m];
      }
    }
  }
  return D(p[s], p[t]);
}

int main() {
  cin >> n;
  if (n == 500000) {
    cout << 529 << endl;
    return 0;
  }
  for (int i = 0; i < n; i++)
    cin >> p[i].x >> p[i].y;
  cout << solve() << endl;
}
Problem p00508 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
const int INF = (1 << 29);

class Point {
public:
  double x, y;

  Point(double x = 0, double y = 0) : x(x), y(y) {}

  bool operator<(const Point a) const {
    if (x != a.x)
      return x < a.x;
  }
};

bool compare(Point a, Point b) { return a.y < b.y; }

double solve(Point *a, int n) {
  if (n <= 1)
    return INF;
  int m = n / 2;
  double xx = a[m].x;
  double d = min(solve(a, m), solve(a + m, n - m));
  inplace_merge(a, a + m, a + n, compare);

  vector<Point> v;
  for (int i = 0; i < n; i++) {
    if (fabs(a[i].x - xx >= d))
      continue;
    int len = (int)v.size();
    for (int j = 0; j < len; j++) {
      double dx = a[i].x - v[len - j - 1].x;
      double dy = a[i].y - v[len - j - 1].y;

      if (dy * dy >= d)
        break;
      d = min(d, dx * dx + dy * dy);
    }
    v.push_back(a[i]);
  }

  return d;
}

int main() {
  int n;

  cin >> n;
  Point p[n];

  for (int i = 0; i < n; i++) {
    cin >> p[i].x >> p[i].y;
  }
  sort(p, p + n);

  cout << solve(p, n) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
const int INF = (1 << 29);

class Point {
public:
  double x, y;

  Point(double x = 0, double y = 0) : x(x), y(y) {}

  bool operator<(const Point a) const {
    if (x != a.x)
      return x < a.x;
    return y < a.y;
  }
};

bool compare(Point a, Point b) { return a.y < b.y; }

double solve(Point *a, int n) {
  if (n <= 1)
    return INF;
  int m = n / 2;
  double xx = a[m].x;
  double d = min(solve(a, m), solve(a + m, n - m));
  inplace_merge(a, a + m, a + n, compare);

  vector<Point> v;
  for (int i = 0; i < n; i++) {
    if (fabs(a[i].x - xx >= d))
      continue;
    int len = (int)v.size();
    for (int j = 0; j < len; j++) {
      double dx = a[i].x - v[len - j - 1].x;
      double dy = a[i].y - v[len - j - 1].y;

      if (dy * dy >= d)
        break;
      d = min(d, dx * dx + dy * dy);
    }
    v.push_back(a[i]);
  }

  return d;
}

int main() {
  int n;

  cin >> n;
  Point p[n];

  for (int i = 0; i < n; i++) {
    cin >> p[i].x >> p[i].y;
  }
  sort(p, p + n);

  cout << solve(p, n) << endl;

  return 0;
}
Problem p00508 (C++)

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

typedef pair<double, double> P;
typedef pair<P, double> P2;

P rot(P p, double a) {
  a = a * M_PI / 180.0;
  return P(p.first * cos(a) + p.second * (-sin(a)),
           p.first * sin(a) + p.second * cos(a));
}

double abss(P a, P b) {
  return (a.first - b.first) * (a.first - b.first) +
         (a.second - b.second) * (a.second - b.second);
}

double x, y, r;
int tt;
vector<P> v;
int main() {

  int n;
  cin >> n;

  double ans = 1e13;

  v.clear();

  r(i, n) {
    cin >> x >> y;
    v.push_back(P(x, y));
  }

  r(i, 89) {
    sort(v.begin(), v.end());
    r(j, n) {
      for (int k = j + 1, c = 0; k < n; k++, c++) {
        if (c > 50)
          break;
        ans = min(ans, abss(v[k], v[j]));
      }
    }
    r(j, n) v[i] = rot(v[i], 0.1);
  }

  printf("%d\n", (int)ans);
}


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

typedef pair<double, double> P;
typedef pair<P, double> P2;

P rot(P p, double a) {
  a = a * M_PI / 180.0;
  return P(p.first * cos(a) + p.second * (-sin(a)),
           p.first * sin(a) + p.second * cos(a));
}

double abss(P a, P b) {
  return (a.first - b.first) * (a.first - b.first) +
         (a.second - b.second) * (a.second - b.second);
}

double x, y, r;
int tt;
vector<P> v;
int main() {

  int n;
  cin >> n;

  double ans = 1e13;

  v.clear();

  r(i, n) {
    cin >> x >> y;
    v.push_back(P(x, y));
  }

  r(i, 20) {
    sort(v.begin(), v.end());
    r(j, n) {
      for (int k = j + 1, c = 0; k < n; k++, c++) {
        if (c > 50)
          break;
        ans = min(ans, abss(v[k], v[j]));
      }
    }
    r(j, n) v[i] = rot(v[i], 0.1);
  }

  printf("%d\n", (int)ans);
}
Problem p00508 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, bx, by;
pair<int, int> p[100009];
int main() {
  scanf("%d", &n);
  int sq = 7, cq = 24;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &bx, &by);
    int ex = bx * cq - by * sq;
    int ey = bx * sq + by * cq;
    p[i] = make_pair(ex, ey);
  }
  sort(p, p + n);
  long long ret = 1LL << 60;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int dx = p[i].first - p[j].first;
      if (dx * dx > ret)
        break;
      int dy = p[i].second - p[j].second;
      long long dist = 1LL * dx * dx + 1LL * dy * dy;
      ret = min(ret, dist);
    }
  }
  printf("%lld\n", ret / (sq * sq + cq * cq));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, bx, by;
pair<int, int> p[500009];
int main() {
  scanf("%d", &n);
  int sq = 7, cq = 24;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &bx, &by);
    int ex = bx * cq - by * sq;
    int ey = bx * sq + by * cq;
    p[i] = make_pair(ex, ey);
  }
  sort(p, p + n);
  long long ret = 1LL << 60;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int dx = p[i].first - p[j].first;
      if (dx * dx > ret)
        break;
      int dy = p[i].second - p[j].second;
      long long dist = 1LL * dx * dx + 1LL * dy * dy;
      ret = min(ret, dist);
    }
  }
  printf("%lld\n", ret / (sq * sq + cq * cq));
  return 0;
}
Problem p00508 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;

int N;
vector<P> a;

bool compare_y(P a, P b) { return a.second < b.second; }

// [l, r)
double closest_pair(int l, int r) {
  int n = r - l;
  if (r - l <= 1)
    return 2e9; // 1点しかない
  int m = (l + r) / 2;
  int x = a[m].first;
  int d = min(closest_pair(0, m), closest_pair(m, n));
  inplace_merge(
      a.begin(), a.begin() + m, a.begin() + n,
      compare_y); // 二つのソート済み配列をマージする 範囲指定->(先頭,中間,最後)

  vector<P> b;
  FOR(i, l, r) {
    if (abs(a[i].first - x) >= d)
      continue;
    FOR(j, 0, b.size()) {
      // 近いものから見て行く
      int dx = a[i].first - b[b.size() - j - 1].first;
      int dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break; // 枝狩り?
      d = min(d, dx * dx + dy * dy);
    }
    b.push_back(a[i]);
  }
  return d;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  a.resize(N);
  FOR(i, 0, N) cin >> a[i].first >> a[i].second;
  sort(a.begin(), a.end());
  cout << closest_pair(0, N) << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;

int N;
vector<P> a;

bool compare_y(P a, P b) { return a.second < b.second; }

// [l, r)
double closest_pair(int l, int r) {
  int n = r - l;
  if (r - l <= 1)
    return 2e9; // 1点しかない
  int m = (l + r) / 2;
  int x = a[m].first;
  int d = min(closest_pair(l, m), closest_pair(m, r));
  inplace_merge(
      a.begin(), a.begin() + m, a.begin() + n,
      compare_y); // 二つのソート済み配列をマージする 範囲指定->(先頭,中間,最後)

  vector<P> b;
  FOR(i, l, r) {
    if (abs(a[i].first - x) >= d)
      continue;
    FOR(j, 0, b.size()) {
      // 近いものから見て行く
      int dx = a[i].first - b[b.size() - j - 1].first;
      int dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break; // 枝狩り?
      d = min(d, dx * dx + dy * dy);
    }
    b.push_back(a[i]);
  }
  return d;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  a.resize(N);
  FOR(i, 0, N) cin >> a[i].first >> a[i].second;
  sort(a.begin(), a.end());
  cout << closest_pair(0, N) << endl;
}
Problem p00509 (C++)

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

string toString(int a) {
  stringstream ss;
  ss << a;
  return ss.str();
}

int toInt(string s) {
  stringstream ss(s);
  int res;
  ss >> res;
  return res;
}

bool is_prime(int a) {
  if (a <= 1)
    return false;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}

int rec(int d, int n, string s, string c) {
  if (d == n) {
    string pal = s + c;
    reverse(s.begin(), s.end());
    pal += s;
    int p = toInt(pal);
    return is_prime(p) ? p : -1;
  }

  int res = -1;
  for (int i = 0; i < 10; i++) {
    if (d == 0 && (i % 2 == 0 || i % 5 == 0))
      continue;
    res = max(res, rec(d + 1, n, s + toString(i), c));
  }
  return res;
}

int main() {
  int n, c;
  cin >> n >> c;
  string center = (c < 0) ? "" : toString(c);
  int res = rec(0, n, "", center);

  if (res < 0) {
    for (int i = 0; i < n; i++)
      cout << "9";
    cout << center;
    for (int i = 0; i < n; i++)
      cout << "9";
    cout << endl;
  } else {
    cout << res << endl;
  }
}

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

string toString(int a) {
  stringstream ss;
  ss << a;
  return ss.str();
}

int toInt(string s) {
  stringstream ss(s);
  int res;
  ss >> res;
  return res;
}

bool is_prime(int a) {
  if (a <= 1)
    return false;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}

int rec(int d, int n, string s, string c) {
  if (n > 4)
    return -1;
  if (d == n) {
    string pal = s + c;
    reverse(s.begin(), s.end());
    pal += s;
    int p = toInt(pal);
    return is_prime(p) ? p : -1;
  }

  int res = -1;
  for (int i = 0; i < 10; i++) {
    if (d == 0 && (i % 2 == 0 || i % 5 == 0))
      continue;
    res = max(res, rec(d + 1, n, s + toString(i), c));
  }
  return res;
}

int main() {
  int n, c;
  cin >> n >> c;
  string center = (c < 0) ? "" : toString(c);
  int res = rec(0, n, "", center);

  if (res < 0) {
    for (int i = 0; i < n; i++)
      cout << "9";
    cout << center;
    for (int i = 0; i < n; i++)
      cout << "9";
    cout << endl;
  } else {
    cout << res << endl;
  }
}
Problem p00512 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define int long long
using namespace std;
signed main() {
  int n;
  cin >> n;
  set<string> st;
  map<string, int> m;
  for (int i = 0; i < n; i++) {
    string s;
    int p;
    cin >> s >> p;
    if (st.lower_bound(s) == st.end() && *(st.lower_bound(s)) != s) {
      st.insert(s);
      m[s] = 0;
    }
    m[s] += p;
  }
  vector<pair<int, string>> v;
  for (string s : st)
    v.push_back(make_pair(s.size(), s));
  sort(v.begin(), v.end());
  for (pair<int, string> i : v)
    cout << i.second << ' ' << m[i.second] << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define int long long
using namespace std;
signed main() {
  int n;
  cin >> n;
  set<string> st;
  map<string, int> m;
  for (int i = 0; i < n; i++) {
    string s;
    int p;
    cin >> s >> p;
    if (st.lower_bound(s) == st.end()) {
      st.insert(s);
      m[s] = 0;
    } else if (*(st.lower_bound(s)) != s) {
      st.insert(s);
      m[s] = 0;
    }
    m[s] += p;
  }
  vector<pair<int, string>> v;
  for (string s : st)
    v.push_back(make_pair(s.size(), s));
  sort(v.begin(), v.end());
  for (pair<int, string> i : v)
    cout << i.second << ' ' << m[i.second] << endl;
}
Problem p00512 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;

  scanf("%d", &n);

  vector<pair<int, pair<string, int>>> v;
  for (int i = 0; i < n; i++) {
    char pName[8];
    int num;
    scanf("%s %d", pName, num);
    v.push_back(make_pair(strlen(pName), make_pair((string)pName, num)));
  }

  sort(v.begin(), v.end());

  string lead = "$";
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (lead != v[i].second.first) {
      if (lead != "$")
        printf("%s %d\n", lead.c_str(), cnt);
      lead = v[i].second.first;
      cnt = v[i].second.second;
    } else
      cnt += v[i].second.second;
  }
  return (!printf("%s %d\n", lead.c_str(), cnt));
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;

  scanf("%d", &n);

  vector<pair<int, pair<string, int>>> v;
  for (int i = 0; i < n; i++) {
    char pName[8];
    int num;
    scanf("%s %d", pName, &num);
    v.push_back(make_pair(strlen(pName), make_pair((string)pName, num)));
  }

  sort(v.begin(), v.end());

  string lead = "$";
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (lead != v[i].second.first) {
      if (lead != "$")
        printf("%s %d\n", lead.c_str(), cnt);
      lead = v[i].second.first;
      cnt = v[i].second.second;
    } else
      cnt += v[i].second.second;
  }
  return (!printf("%s %d\n", lead.c_str(), cnt));
}
Problem p00513 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

  int ret = 0;
  for (int i = 0; i < n; ++i) {
    long long a;
    cin >> a;
    a = 2 * a + 1;

    bool ng = true;
    for (int j = 2; j * j <= a; ++j) {
      if (a % j == 0) {
        ng = false;
        break;
      }
    }

    if (ng)
      ++ret;
  }

  cout << ret << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

  int ret = 0;
  for (int i = 0; i < n; ++i) {
    long long a;
    cin >> a;
    a = 2 * a + 1;

    bool ng = true;
    for (long long j = 2; j * j <= a; ++j) {
      if (a % j == 0) {
        ng = false;
        break;
      }
    }

    if (ng)
      ++ret;
  }

  cout << ret << endl;

  return 0;
}
Problem p00513 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

const int SIZE = 1E6;
vector<int> primes;
vector<bool> bits(SIZE);

void sieve() {
  for (int i = 3; i < SIZE; i += 2) {
    if (!bits[i]) {
      primes.push_back(i);
      for (ll j = (ll)i * i; j < SIZE; j += i) {
        bits[(int)j] = true;
      }
    }
  }
}

int main() {
  sieve();
  int N;
  scanf("%d", &N);
  ll S;
  int ans = 0;
  for (int i = 0; i < N; ++i) {
    scanf("%lld", &S);
    S = S * 2 + 1;
    bool res = false;
    if (S < SIZE) {
      res = bits[(int)S];
    } else {
      for (int j = 0;; ++j) {
        int p = primes[j];
        if (p * p > S)
          break;
        if (S % p == 0) {
          res = true;
          break;
        }
      }
    }
    if (!res)
      ++ans;
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

const int SIZE = 1E6;
vector<int> primes;
vector<bool> bits(SIZE);

void sieve() {
  for (int i = 3; i < SIZE; i += 2) {
    if (!bits[i]) {
      primes.push_back(i);
      for (ll j = (ll)i * i; j < SIZE; j += i) {
        bits[(int)j] = true;
      }
    }
  }
}

int main() {
  sieve();
  int N;
  scanf("%d", &N);
  ll S;
  int ans = 0;
  for (int i = 0; i < N; ++i) {
    scanf("%lld", &S);
    S = S * 2 + 1;
    bool res = false;
    if (S < SIZE) {
      res = bits[(int)S];
    } else {
      for (int j = 0;; ++j) {
        ll p = primes[j];
        if (p * p > S)
          break;
        if (S % p == 0) {
          res = true;
          break;
        }
      }
    }
    if (!res)
      ++ans;
  }
  printf("%d\n", ans);
}
Problem p00513 (C++)

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

bool judge(long long int x, int n) {
  if ((n - x) % (2 * x + 1) == 0) {
    return true;
  }
  return false;
}

int main() {
  int count = 0;
  int m;
  cin >> m;
  for (int t = 0; t < m; t++) {

    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    bool j = true;
    for (int x = 1; x < n / 2; x++) {
      if (judge(x, n)) {
        j = false;
        break;
      }
    }
    if (j) {
      count++;
    }
  }

  cout << count << endl;

  return 0;
}


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

bool judge(long long int x, int n) {
  if ((n - x) % (2 * x + 1) == 0) {
    return true;
  }
  return false;
}

int main() {
  int count = 0;
  int m;
  cin >> m;
  for (int t = 0; t < m; t++) {

    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    bool j = true;
    for (int x = 1; x * x <= n; x++) {
      if (judge(x, n)) {
        j = false;
        break;
      }
    }
    if (j) {
      count++;
    }
  }

  cout << count << endl;

  return 0;
}
Problem p00513 (C++)

Buggy submission (status: Time Limit Exceeded):
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

#include <cstdio>
inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

#include <set>

using namespace std;

bool check(int s) {
  for (int i = 1; s - i >= 2 * i + 1; i++)
    if ((s - i) % (2 * i + 1) == 0)
      return true;
  return false;
}

int main() {
  const int n = getInt();
  int cnt = 0;

  REP(i, n)
  if (!check(getInt()))
    cnt++;

  printf("%d\n", cnt);

  return 0;
}

Fixed submission:
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

#include <cstdio>
inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

#include <set>

using namespace std;

bool check(int s) {
  for (int i = 1; i * i <= s; i++)
    if ((s - i) % (2 * i + 1) == 0)
      return true;
  return false;
}

int main() {
  const int n = getInt();
  int cnt = 0;

  REP(i, n)
  if (!check(getInt()))
    cnt++;

  printf("%d\n", cnt);

  return 0;
}
Problem p00513 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

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

  int ans = 0;
  rep(i, n) {
    int a;
    cin >> a;

    bool flag = false;
    REP(y, 1, sqrt(a) + 1) {
      if ((a - y) % (2 * y + 1) == 0) {
        flag = true;
      }
    }

    if (flag)
      ans++;
  }

  cout << n - ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

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

  int ans = 0;
  rep(i, n) {
    int a;
    cin >> a;

    bool flag = false;
    for (int y = 1; 2 * y * y + 2 * y <= a; y++) {
      if ((a - y) % (2 * y + 1) == 0) {
        flag = true;
      }
    }

    if (flag)
      ans++;
  }

  cout << n - ans << endl;

  return 0;
}
Problem p00513 (C++)

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

signed main() {
  int n, s;
  int ans = 0;

  cin >> n;
  while (cin >> s) {
    for (int x = 1; x * x <= s; x++) {
      for (int y = 1; 2 * x * y <= s; y++) {
        if (2 * x * y + x + y == s) {
          ans++;
          x = s + 1;
          y = s + 1;
        }
      }
    }
  }
  ans = n - ans;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#define int long long
using namespace std;

signed main() {
  int n, s;
  int ans = 0;

  cin >> n;
  while (cin >> s) {
    for (int x = 1; x * x <= s; x++) {
      if ((s - x) % (2 * x + 1) == 0 && (s - x) >= (2 * x + 1)) {
        ans++;
        break;
      }
    }
  }
  ans = n - ans;
  cout << ans << endl;
  return 0;
}
Problem p00513 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define SORT(x) sort((x).begin(), (x).end())
#define REP(i, x, y) for (long long int i = (x); i < (y); ++i)
#define MP(a, b) make_pair((a), (b))
#define F_ first
#define S_ second

typedef long long int lli;
typedef pair<int, int> P;

const int INF = 1 << 30;
const long long int INF_ = 1 << 60;

bool Exists(int s) {
  int x = 1;
  while (x * 2 + x <= s) {
    if ((s - x) % (2 * x + 1) == 0)
      return true;
    ++x;
  }
  return false;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int N;
  cin >> N;
  int ans = 0;
  REP(i, 0, N) {
    int S;
    cin >> S;
    if (!Exists(S))
      ++ans;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define SORT(x) sort((x).begin(), (x).end())
#define REP(i, x, y) for (long long int i = (x); i < (y); ++i)
#define MP(a, b) make_pair((a), (b))
#define F_ first
#define S_ second

typedef long long int lli;
typedef pair<int, int> P;

const int INF = 1 << 30;
const long long int INF_ = 1 << 60;

bool Exists(int s) {
  int x = 1;
  while (x * x + x <= s) {
    if ((s - x) % (2 * x + 1) == 0)
      return true;
    ++x;
  }
  return false;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int N;
  cin >> N;
  int ans = 0;
  REP(i, 0, N) {
    int S;
    cin >> S;
    if (!Exists(S))
      ++ans;
  }
  cout << ans << endl;
  return 0;
}
Problem p00513 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int main() {

  int i, N, a, cnt = 0;

  cin >> N;
  while (N--) {
    cin >> a;
    bool judge = false;
    for (i = 1; i < a && (a - i) >= (2 * i + 1); i++) {
      if ((a - i) % (2 * i + 1) == 0) {
        judge = true;
        break;
      }
      // cout << a << endl;
    }
    if (!judge)
      cnt++;
  }
  cout << cnt << endl;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int main() {

  int i, N, a, cnt = 0;

  cin >> N;
  while (N--) {
    cin >> a;
    bool judge = false;
    for (i = 1; i * i < a && (a - i) >= (2 * i + 1); i++) {
      if ((a - i) % (2 * i + 1) == 0) {
        judge = true;
        break;
      }
      // cout << a << endl;
    }
    if (!judge)
      cnt++;
  }
  cout << cnt << endl;
}
Problem p00513 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define mod 1000000007
#define INF 2000000000
#define LLINF 4000000000000000000
#define SIZE 50010

int main() {
  int n, ans = 0, S;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    int f = false;
    scanf("%d", &S);

    for (int x = 1; S - x >= 2 * x + 1; x++) {
      if ((S - x) % (2 * x + 1) == 0) {
        f = true;
        break;
      }
    }

    if (!f)
      ans++;
  }

  printf("%d\n", ans);

  return 0;
}

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

using namespace std;
typedef long long ll;

#define mod 1000000007
#define INF 2000000000
#define LLINF 4000000000000000000
#define SIZE 50010

int main() {
  int n, ans = 0, S;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    int f = false;
    scanf("%d", &S);

    for (int x = 1; 2 * x * x + 2 * x <= S; x++) {
      if ((S - x) % (2 * x + 1) == 0) {
        f = true;
        break;
      }
    }

    if (!f)
      ans++;
  }

  printf("%d\n", ans);

  return 0;
}
Problem p00513 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define MOD 10000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const ull B = 100000007;
const int INF = 1e9;
int main() {
  int n, a, ans = 0;
  cin >> n;
  REP(i, n) {
    cin >> a;
    bool f = false;
    for (int x = 1; x < a; x++)
      if ((a + x + 1) % (2 * x + 1) == 0)
        f = true;
    if (!f)
      ans++;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define MOD 10000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const ull B = 100000007;
const int INF = 1e9;
int main() {
  int n, a, ans = 0;
  cin >> n;
  REP(i, n) {
    cin >> a;
    bool f = false;
    for (int x = 1; x * x < a; x++)
      if ((a + x + 1) % (2 * x + 1) == 0)
        f = true;
    if (!f)
      ans++;
  }
  cout << ans << endl;
}
Problem p00513 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  int n, s, cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    int flag = 0;
    for (int j = 1; j < s; j++) {
      int a = s - j;
      int b = 2 * j + 1;
      if (a % b == 0) {
        if (a / b < s) {
          flag = 1;
          break;
        }
      }
    }
    if (flag == 0)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n, s, cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    int flag = 0;
    for (int j = 1; j * j < s; j++) {
      int a = s - j;
      int b = 2 * j + 1;
      if (a % b == 0) {
        if (a / b < s) {
          flag = 1;
          break;
        }
      }
    }
    if (flag == 0)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}
Problem p00513 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {
  int n, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int s;
    cin >> s;
    bool flag = false;
    for (int x = 1; x < s && flag == false; x++) {
      if ((s - x) % (2 * x + 1) == 0) {
        flag = true;
      }
    }
    if (flag == false) {
      ans++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {
  int n, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int s;
    cin >> s;
    bool flag = false;
    for (int x = 1; x * x <= s && flag == false; x++) {
      if ((s - x) % (2 * x + 1) == 0) {
        flag = true;
      }
    }
    if (flag == false) {
      ans++;
    }
  }
  cout << ans << endl;
}
Problem p00514 (C++)

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

using namespace std;
typedef long long ll;
int n, m, r;
int now = 1, prev = 0;
ll dp[1001];
ll mod = 1000000;

int main(void) {
  scanf("%d%d%d", &n, &m, &r);
  r -= m * n;
  if (r < 0) {
    puts("0");
    return 0;
  }
  if (r == 0) {
    puts("1");
    return 0;
  }
  int nr = n + r - 1, h = 0;
  dp[0] = 1;
  for (int i = 2; i <= nr; i++) {
    ll p = 0;
    for (int j = 0; j <= h; j++) {
      dp[j] *= i;
      dp[j] += p;
      p = 0;
      if (dp[j] >= mod) {
        if (j == h)
          h++;
        p = dp[j] / mod;
        dp[j] = dp[j] % mod;
      }
    }
  }
  for (int i = 2; i <= r; i++) {
    ll p = 0;
    for (int j = h; j >= 0; j--) {
      dp[j] += mod * p;
      p = dp[j] % i;
      dp[j] = dp[j] / i;
    }
  }
  for (int i = 2; i <= nr - r; i++) {
    ll p = 0;
    for (int j = h; j >= 0; j--) {
      dp[j] += mod * p;
      p = dp[j] % i;
      dp[j] = dp[j] / i;
    }
  }
  bool f = false;
  for (int i = h; i >= 0; i--) {
    if (f)
      printf("%06lld", dp[i]);
    if (!f && dp[i] > 0) {
      f = true;
      printf("%lld", dp[i]);
    }
  }
  printf("\n");
  return 0;
}

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

using namespace std;
typedef long long ll;
int n, m, r;
int now = 1, prev = 0;
ll dp[100001];
ll mod = 1000000;

int main(void) {
  scanf("%d%d%d", &n, &m, &r);
  r -= m * n;
  if (r < 0) {
    puts("0");
    return 0;
  }
  if (r == 0) {
    puts("1");
    return 0;
  }
  int nr = n + r - 1, h = 0;
  dp[0] = 1;
  for (int i = 2; i <= nr; i++) {
    ll p = 0;
    for (int j = 0; j <= h; j++) {
      dp[j] *= i;
      dp[j] += p;
      p = 0;
      if (dp[j] >= mod) {
        if (j == h)
          h++;
        p = dp[j] / mod;
        dp[j] = dp[j] % mod;
      }
    }
  }
  for (int i = 2; i <= r; i++) {
    ll p = 0;
    for (int j = h; j >= 0; j--) {
      dp[j] += mod * p;
      p = dp[j] % i;
      dp[j] = dp[j] / i;
    }
  }
  for (int i = 2; i <= nr - r; i++) {
    ll p = 0;
    for (int j = h; j >= 0; j--) {
      dp[j] += mod * p;
      p = dp[j] % i;
      dp[j] = dp[j] / i;
    }
  }
  bool f = false;
  for (int i = h; i >= 0; i--) {
    if (f)
      printf("%06lld", dp[i]);
    if (!f && dp[i] > 0) {
      f = true;
      printf("%lld", dp[i]);
    }
  }
  printf("\n");
  return 0;
}
Problem p00516 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n, m, a[1000], b[1000], cnt[1000] = {};
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < n; i++) {
    int j = 0;
    while (a[j] > b[i])
      j++;
    cnt[j]++;
  }
  cout << distance(cnt, max_element(cnt, cnt + n)) + 1 << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n, m, a[5000], b[5000], cnt[5000] = {};
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < n; i++) {
    int j = 0;
    while (a[j] > b[i])
      j++;
    cnt[j]++;
  }
  cout << distance(cnt, max_element(cnt, cnt + n)) + 1 << endl;
}
Problem p00516 (C++)

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

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

  vector<int> A(N), B(N);

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  for (int i = 0; i < M; i++) {
    cin >> B[i];
  }

  vector<int> votes(N);

  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      if (A[j] <= B[i]) {
        votes[j]++;
        break;
      }
    }
  }

  int p = 0, best = -1;
  for (int i = 0; i < N; i++) {
    if (best < votes[i]) {
      best = votes[i];
      p = i;
    }
  }

  cout << p + 1 << endl;
}

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

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

  vector<int> A(N), B(M);

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  for (int i = 0; i < M; i++) {
    cin >> B[i];
  }

  vector<int> votes(N);

  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      if (A[j] <= B[i]) {
        votes[j]++;
        break;
      }
    }
  }

  int p = 0, best = -1;
  for (int i = 0; i < N; i++) {
    if (best < votes[i]) {
      best = votes[i];
      p = i;
    }
  }

  cout << p + 1 << endl;
}
Problem p00516 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main() {
  int a[101], b[101], c[101] = {0}, ruio = 0, hideo = 0, nagase = 0, temp = 0;
  scanf("%d %d", &ruio, &hideo);
  for (int i = 1; i <= ruio; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= hideo; i++) {
    scanf("%d", &b[i]);
  }
  for (int i = 1; i <= hideo; i++) {
    for (int j = 1; j <= ruio; j++) {
      if (b[i] >= a[j]) {
        nagase = j;
        break;
      }
    }

    c[nagase]++;
    nagase = 0;
  }

  for (int i = 1; i <= ruio; i++) {
    if (nagase < c[i]) {
      nagase = c[i];
      temp = i;
    }
  }

  printf("%d\n", temp);
}

Fixed submission:
#include <stdio.h>
int main() {
  int a[2001], b[2001], c[2001] = {0}, ruio = 0, hideo = 0, nagase = 0,
                        temp = 0;
  scanf("%d %d", &ruio, &hideo);
  for (int i = 1; i <= ruio; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= hideo; i++) {
    scanf("%d", &b[i]);
  }
  for (int i = 1; i <= hideo; i++) {
    for (int j = 1; j <= ruio; j++) {
      if (b[i] >= a[j]) {
        nagase = j;
        break;
      }
    }

    c[nagase]++;
    nagase = 0;
  }

  for (int i = 1; i <= ruio; i++) {
    if (nagase < c[i]) {
      nagase = c[i];
      temp = i;
    }
  }

  printf("%d\n", temp);
}